How to Enable PHP in OS X 10.8 Mountain Lion


Last time, we looked at how to use the Terminal to start and stop the Apache web server that comes bundled with OS X 10.8 Mountain Lion now that Apple has removed its checkbox from the Sharing pane in System Preferences. This is fine and dandy for all your static HTML files, but if you are going to use your Mac as a serious web development environment, you will almost surely want to enable PHP.

Enabling PHP is as simple as deleting one character from the default Apache configuration file located at /etc/apache2/httpd.conf

To edit this file, we need two things: a text editor and a way to elevate our permissions up to root power. The Terminal is a natural choice for this task, as it comes with (at least) three command-line text editors: emacs, nano, and vim. And, as we also talked about last time, prefixing a Terminal command with sudo does the trick for root-power elevation.

There are also some GUI text editors that can authenticate as root, and the first two that come to the top of my head are both from Bare Bones Software: their flagship editing powerhouse BBEdit (which costs $99 and is worth every penny), and its very capable no-cost cousin, TextWrangler. But unless you have one of those apps already around, I’d recommend you just dive back into the Terminal, as this edit can be done quickly and easily with the built-in Unix apps accessed in the Terminal.

If you’re a Unix neophyte, nano is probably the best choice for you amongst the default text editors. At the bottom of a nano window, you’ll find handy control-key shortcuts for searching, saving the file, and more. So start with this command to get started editing (replace nano with the editor of your choice, if you have another preference):

sudo nano /etc/apache2/httpd.conf

And search for the string php5. (If you are using nano, look for the key-combo that enables searching; if you are up on vim or emacs, use their search function). We’re looking for the line that says this:

#LoadModule php5_module libexec/apache2/

All we need to do is delete the # at the beginning of the line, then save and close the file (once again, look at nano’s helpful key-combo list). What we are doing here is un-commenting the line – Apache ignores all lines that begin with # as “comments” – and once the # gone, Apache will load the PHP module the next time it is started. If you will recall from last time, we restart Apache with this command:

sudo apachectl restart

Now PHP should be up and running! To test it out, open up your favorite plaintext editor – in this case, you can use a GUI text editor just fine, including OS X’s bundled TextEdit, as long as it is set to save in plaintext mode. The file just needs these few lines:


Save the file in Apache’s document root, which you’ll recall from last time is /Library/WebServer/Documents/. You can call the file whatever you like, as long as it ends with the extension .php. I would suggest info.php. Now open up your web browser and go to http://localhost/info.php, and you should be presented with a nicely formatted page chock full of information about your PHP installation. This also confirms that PHP is up and running successfully.

PHP information

Now that we’ve got Apache and PHP going, you might find that you like having it around on your Mac, whether for development or play purposes. If you think you’ll use it regularly and don’t want to have to enter the Apache start command in the Terminal after each boot, there’s a way to keep it up and running through reboots, using launchd, Apple’s file launching and scheduling utility.

In /System/Library/LaunchDaemons/, there’s a file that can be used to start Apache at boot. It just needs to be loaded into launchd’s list of tasks. This is accomplished with a one-time Terminal command:

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

Once this command is run, the Apache web server will load at boot time, without you having to enter any commands. If you decide later on that this is not the behavior you want, just unload it from launchd’s schedule of jobs with this command:

sudo launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist

PHP will always load with Apache, as long as the “Load Module” directive for php5 is un-commented in Apache’s config file.

I had planned to also show how to set up PHP’s best friend, the MySQL relational database server, but this column has run a little long. There are quite a few (one-time only, thankfully) steps involved in this process, so next time we’ll learn all about that.

A great as Apache is at serving up web pages, and as capable as PHP is when it comes to developing web apps, this setup really shines once PHP has a world-class database (like MySQL) to pull its data from.

Keywords: #enablephp #osxmountainlion

Short link:

searchword: enablephp


Leave a Reply