Notes: Enable Apache2, PHP5 and Virtual Hosts in Mac OS X

Mac OS X came with Apache2 web server and PHP5 out of the box. All I had to do was enable them.

Enable PHP5:

I edited the Apache configuration file httpd.conf, but first, I made a backup.

$ cd /etc/apache2 
$ sudo cp httpd.conf httpd.conf.bak 
$ sudo nano httpd.conf

I removed the comment symbol “#” from the following line in httpd.conf.

LoadModule php5_module libexec/apache2/libphp5.so

Configure PHP5:

The configuration file for PHP is found in /private/etc/php.ini. I left it untouched and just made a backup.

$ cd /private/etc 
$ sudo cp php.ini php.ini.bak

Enable Virtual Hosts:

I proceeded to remove the comment symbol “#” from the following line in httpd.conf.

Include /private/etc/apache2/extra/httpd-vhosts.conf

Start the Apache Server:

I entered this command from the terminal.

$ sudo apachectl restart

Create a PHP script for testing:

The ~/Sites/ folder is available by default for user specific web sites. I created the following PHP script and saved it as ~/Sites/test.php.

<?php phpinfo(); ?>

Testing:

I launched the Safari web browser and entered the following in the address bar.

http://localhost/~darwin/test.php

It worked!