Installing PHP on OS X

As many of you may have noticed, one of the purposes of this blog is to act as my own personal notebook on how to configure things, but I’m glad to see it has provided benefit to others as well. That being said, it has been far too long since I’ve posted any information on this blog. Setting up my new laptop really through my routine out of whack. So as part of my New Year’s resolution, I resolve to do better, and post articles on a more frequent basis.

Speaking of my new laptop, I am now able to run OS X 10.6 or better, so all of my posts from now on will be discussing the newer OS.

But this post is about how to get PHP up and running on OS X, so let’s dig in.

In the past, everyone from Apple to the PHP website itself recommended that OS X users download and install the PHP package supplied by Marc Liyanage over at Entropy.ch. Marc did a really excellent job with his package, and the installer was just excellent. However, it’s been a long time since his PHP package has received an update. It took me a while to realize why that is.

Apple has actually been very good about keeping the PHP that is included in OS X up to date lately. Currently the version on OS X is PHP 5.3.3, which considering that 5.3.4 just came out on Dec 10, 2010 isn’t that bad. And I would expect this to be updated with the next security patch for OS X.

In recent months, Apple has decided to leave it up to the Adobe and Oracle to keep Flash and Java up to date on OS X, instead of handling it themselves. If this happens with PHP, then hopefully Marc will again start doing his custom builds. If not, then we’ll have to find some place else to turn, or start rolling our own.

So let’s get started with actually getting the Apple installed PHP turned on. As usual, you’ll need to perform these steps using an account with administrative rights.

Step 1: Editing the Apache configuration

We’ll begin by editing the main Apache configuration file to enable PHP. Open a Terminal window, and type the following:

$ cd /etc/apache2
$ ls -l

You should see a list of files in this directory. The file we need to edit is httpd.conf. It’s a good idea to make a copy of this file before editing it, so we’ll start by doing that.

$ sudo cp httpd.conf httpd_original.conf
$ sudo nano httpd.conf

The line we need to modify is near the beginning of this file, toward the end of the DSO Support section. We’re looking for the line with php5_module. We’ll enable this line by removing the # in front of it, to look like:

LoadModule php5_module         libexec/apache2/libphp5.so

Save and close the httpd.conf file.

Step 2: Enabling the php.ini file

Next, we need to enable the main PHP configuration file. Using the Terminal window we already have open, type the following:

$ cd /etc
$ ls -l php*

You should see one file listed, php.ini.default. The web server won’t read this file until we rename it to php.ini. You can do so by typing:

$ sudo cp php.ini.default php.ini

Now we need to edit the php.ini file so that it accepts the default database sockets.

$ sudo chmod u+w php.ini
$ sudo nano php.ini

You need to find the lines which list the default sockets, and remove the text from three lines that say /var/mysql/mysql.sock. So the lines should look as follows:

pdo_mysql.default_socket =
mysql.default_socket = 
mysqli.default_socket =

Close and save the file. Now, we’re ready to continue.

Step 3: Restart the web server

Open System Preferences, select Sharing, and cycle Web Sharing from On, to Off, to On, as necessary.

Step 4: Test the installation

To verify that PHP is working and enabled, we’ll create a test PHP file in our user’s Sites folder.

$ cd ~/Sites
$ nano test.php

Enter the following code into this file, then save and close the file.

<?php 
    echo phpinfo();
?>

To test this file by open your web browser, and enter to the following in the address bar:

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

If all went well you should see a page with a purple banner, which displays a whole lot of information about your PHP installation. You need to look at the output on this screen, to make sure the the php.ini file is being read correctly. Under the Core section, look for the line that has display_errors, and make sure the setting reads Off. If so, the server is reading the php.ini file correctly.

If everything looks ok, you can now start building and testing PHP based pages.

Leave a Reply

Your email address will not be published. Required fields are marked *