Wednesday, August 31, 2011

Turning PHP on in your Mac Web server

In the previous blog, I introduced you to the built in web server in your Mac's OS X operating system.

I use my personal web server to publish and test web sites I am developing, using my favourite Web development tool RapidWeaver from RealMacSoftware.

Some of the sites I develop need PHP and so in this blog I'm going to tell you how to turn on PHP in your Mac's web server.  It comes pre installed but, by default, PHP is not enabled and you have to 'turn it on' by changing some settings in your Mac's inbuilt Apache web server.

You'll need a special editor for this job, something like TextMate (from Macromates.com) or Text Wrangler (from Bare Bones software).  I have both tools on my Mac but for this blog I'm going to use Text Mate.

Some of the files you need to modify for a job like this are hidden or system files and these two tools have the ability to access files like that and help you change them (using the appropriate system permissions).

Fire up Text Mate and click File > Open   Make sure the 'Show Hidden Files' option is selected and navigate your way to [YourHardDrive]:private:etc:apache2:httpd.conf

You will probably need to supply the name/password of the root user or a super user on your system to edit and change the file so be prepared and supply it when requested.

Find the line in the file like this...
#LoadModule php5_module libexec/apache2/libphp5.so

and remove the '#' from the front of the line.  The '#' means its a comment and removing it means you want this statement to be activated in the configuration settings for your web server.  The statement tells your system to load the PHP module at startup.

Save your changes and its on to the next task.  When we restart your web server, it will now load PHP 5 support.  This is the current version pre installed with Mac OS X (or at least it is on my 10.7 'Lion' system).

One other thing to do before we restart the web server and that is to create a php.ini file to hold the PHP settings you wish to use.

For this task, we are going to open a Terminal window and type in the following:

cd /private/etc 
sudo cp php.ini.default php.ini

The /private/etc directory is where the system will expect to find your PHP .ini file and the 'sudo' tells the system you want to copy the .default php.ini file provided by Apple to the php.ini file your system will use to configure how PHP works on your system.

We will just use the default PHP options at this stage but if you need to you can come back and edit specific PHP options to suit your needs.

You can now restart your inbuilt Apache web server by going to System Preferences > Sharing and uncheck the checkmark next to 'Web sharing' and then recheck the checkmark.  This stops and then restarts the web server.  All going well, it should now read your new configuration settings.

To test PHP is running, and to see the settings it is using, create a file called phpinfo.php with the following in it:


save the file in root directory of your personal web site (hint: did you remember to store the folder in your Finder favourites as indicated in my last blog?)  If you didn't, you should find the right directory in the Sites folder of your Home directory.

Now, if you type the following into your browser  http://[your Mac's name]/~[YourUserName]/phpinfo.php  you should see something that looks like this...

On my system, I am running PHP version 5.3.6, the one that came installed with Mac OS X 10.7 (Lion).

In the information provided, you will be able to see all of your important PHP options.

If you want to modify any, you just need to edit the php.ini file we created earlier.

Now, whenever you create a web site that runs on your Mac's web server, you will have access to PHP.


That's about all for this blog post! 

In the next blog I will tell you a bit about setting RapidWeaver to publish web sites to your personal web server on your Mac.

Thanks for reading! Please feel free to post any questions or comments you may have and I'll try to get back to you as soon as I can :)

1 comment:

  1. Good luck with your Blog Glenn. Thank you for the link.

    ReplyDelete