Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Mac OS X includes PHP, along with the Apache HTTP server. Version 10.5 of the operating system, Leopard, includes PHP 5.2, while versions 10.6, Snow Leopard, and 10.7, Lion, both include PHP 5.3. PHP isn't enabled by default, though. You have to activate it. I'll show you how to do that. First, go to your Web Sharing dialog and for the moment, turn Web Sharing off. Then you'll go to Terminal and open a command window.
If you've never used Terminal before, you can find it easily by going to Spotlight in the upper-right corner, typing "terminal," and pressing Return. Now to make it easy to get to Terminal in the future, go to your Dock, right-click on Terminal and choose Options > Keep in Dock. Now, you're going to edit the HTTP configuration file. I'm going to use an editor that's included with Mac OS X called pico, or P-I-C-O. I'll start with the command "sudo." That stands for superuser do and allows me to do anything I want on this computer.
Then I'll type pico the name of the editor and then the location and name of the HTTP configuration file, /etc/apache2/httpd.conf. Make sure you type this in all lowercase. Press Return. If you're prompted for your administrative password, type it in and press Return, and that will open the HTTP configuration file in pico.
Now locate the line of code that activates PHP. Pico is completely a keyboard-oriented editor; you won't be able to use the mouse. You'll see keyboard shortcuts listed at the bottom. Press Ctrl+W for where is, or find, type "php," and press Return. This should take you to a LoadModule command that references PHP 5. Notice there's a hash mark, or pound sign, at the beginning of the line. That's a comment, and it's deactivating PHP right now.
Using your arrow keys, move over to the left and then backspace over the pound sign, and now PHP is activated. Exit and save the file. Press Ctrl+X, Y for yes, and Return. And now you've made the required change to the configuration file. The next step is to create a PHP initialization file. This goes into a folder called /private/etc, and this file needs to be named php.ini.
It doesn't already exist on your system, but there is a beginning file there that you can copy. Type the following command to change to the right directory: cd /private/etc. And then list all files that start with the letters php. Type "ls php*" and press Return. You should see a file named php.ini.default. You might see other files there, depending on how your computer was configured, but the default file is the only one you need.
Now you'll copy that and create the new file named php.ini. Type "sudo cp" for copy, and type the name of the existing file, "php.ini.default" and then after a space, the name of the file you want to create "php.ini" and press Return. Once again list the php files with ls php* and you should see the new file is there. Now, that's all you have to do, but if you want to make any changes to the ini file, you can again use the pico editor or any text editor that you prefer.
I'll type "sudo pico ./php.ini." Now I'm going to make one change that I always make to my PHP configuration when I'm doing local development. If I encounter a PHP error, I want the error to print in the HTML that's returned to the browser. This capability is turned off by default. To find it, press Ctrl+W and type "display_errors" and press Return.
The first time you search you'll get to some documentation. Press Ctrl+W and Return again and now you get to the actual configuration. Change the value of display_errors from Off to On. Once again, exit and save. Press Ctrl+X, Y for yes, and Return. Now, you'll turn Apache back on again. Go back to your Sharing dialog and check Web Sharing and Apache should start correctly. Now to test PHP, I've provided a file in the Free Exercise Files that you can copy to your web root.
Go to the Free Exercise Files and copy the file phpinfo.ini to the clipboard. Then navigate to your root folder. That's under the Macintosh root folder, Library/WebServer/Documents, and paste the file into that location. To test this, go to any browser and then type in the following URL, "http://localhost/phpinfo.php." If everything is working correctly, you should see this generated HTML page, which tells you which version of PHP you're using and, as you scroll down, all sorts of other important information about your PHP environment.
One of the most important bits of information you should see is the name and location of your loaded configuration file, which is /private/etc/php.ini when you're using the versions of Apache and PHP that are included with Mac OS X.
Get unlimited access to all courses for just $25/month.Become a member