Enable Apache and PHP – Mac Kung Fu, 2nd Edition

Tip 171Enable Apache and PHP

Here’s a corollary for the sidebar OS X for Admins and Software Developers. Although the Apache 2 web server and PHP 5 scripting language are both included with a standard desktop installation of OS X Mountain Lion, neither is configured to work out of the box. If you’re a web developer, you’ll most likely want to change this; the following instructions explain how.

Starting and Configuring Apache

To start Apache, open a Terminal window, and type sudo apachectl start. Type your password when prompted, and note that you’ll need to do this several times when prompted during the following steps. To stop Apache, type sudo apachectl stop. You can test Apache by visiting http://localhost in your browser.

Document root is located at /Library/WebServer/Documents/. To enable user-level root (that is, something like http://localhost/~keir), you’ll have to create a Sites folder in your home folder, although this might already exist if you’ve upgraded from a previous release of OS X. This is where you should place your site files in the future.

You’ll also need to add a text .conf file within the /etc/apache2/users folder named after your short Unix username (that is, something like keirthomas.conf; to find your short Unix username, type whoami at the prompt).

To do so, type sudo nano /etc/apache2/users/USERNAME.conf, replacing USERNAME with your short Unix username. Then paste the following into the Terminal window, again replacing USERNAME with your username:

<Directory "/Users/USERNAME/Sites/">
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all

Save the file, and quit the text editor by pressing Ctrl+O, Return, then Ctrl+X.

Finally, set the correct permissions on the file by typing sudo chmod 644 /etc/apache2/users/USERNAME.conf, again replacing USERNAME with your Unix username, and restart Apache: sudo apachectl restart. To have Apache start automatically as a background service each time the computer boots, type the following into a Terminal window:

sudo defaults write /System/Library/LaunchDaemons/org.apache.httpd Disabled
-bool FALSE

If you ever want to disable automatic starting of Apache on boot, again open a Terminal window, and type the following:

sudo defaults write /System/Library/LaunchDaemons/org.apache.httpd Disabled
-bool TRUE

Enabling PHP

To enable PHP, open a Terminal window, and open the httpd.conf file for editing by typing sudo nano /etc/apache2/httpd.conf. You can navigate within the text editor using the cursor keys. Look for the line that begins #LoadModule php5_module, and remove the hash at the start of the line. Then save the file and quit the editor by hitting Ctrl+O, Return, and then Ctrl+X. Restart Apache by typing sudo apachectl restart.

Note that MySQL is not installed by default but can be downloaded from the MySQL website.[16] Alongside the main MySQL package are additional components that allow the starting and stopping of the MySQL server from within System Preferences.