How to Enable PHP in 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, but 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/libphp5.so
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:
<?php phpinfo(); ?>
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.
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.
Recent Plays Well with Others articles
- How to Install and Configure MySQL in Mountain Lion, 2012.08.22. MySQL doesn't come with OS X, but it's free, easy to install, and very powerful.
- Steve Jobs in His Own Words, 2011.10.06. Steve Jobs spoke his mind, carefully choosing his words for maximum impact.
- End of G4 models points to unprecedented value for used G4 Macs, 2006.06.02. The PowerPC G4 may no longer have a place in Apple's product line, but that's a far cry from saying G4-based Macs are obsolete. If anything, there value is going to increase.
- More in the Plays Well with Others index.
Links for the Day
- Mac of the Day: Mac LC II, introduced 1992.03.23. The LC gets 4 MB base RAM, gains virtual memory thanks to 68030 CPU.
- May 19 in LEM history: 99: Student Bill of Rights - 01: First Apple Stores - 03: Upgraded beige G3 problems - 04: The Mac legacy - More for Mac users in superstores than many realize - 06: Smart design make the MacBook a winner - 08: Top 10 freeware - My first iMac
- Support Low End Mac
Recent Content on Low End Mac
- World Book Encyclopedia 2012 DVD, Tommy Thomas, Reviews, 2013.03.05. "You may be asking yourself, in an age of Wikipedia and instant information, is World Book still relevant?"
- Vintage Computer Festival SouthEast, April 20-21, 2013, Simon Royal, Mac Spectrum, 2013.02.25. Old Apple gear and old PCs.
- iMessage: The Ultimate Messaging Service?, Simon Royal, Mac Spectrum, 2013.02.21. In most ways, Apple's iMessage is far superior to BlackBerry Messenger.
- More links in our archive.
- Best Mac mini Deals
- Best 13" MacBook Pro Deals
- Best Intel iMac Deals
- Best iPod touch Deals
- Best iPhone Deals
- Best iPod nano Deals
- Best iPod classic Deals
- Best Apple TV Prices
- More deals in our archive.
Low End Mac Reader Specials
Cult of Mac
Shrine of Apple
The Mac Observer
Accelerate Your Mac
The Vintage Mac Museum
Mac Driver Museum
System 6 Heaven
System 7 Today
the pickle's Low-End Mac FAQ