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 :)

Using inbuilt Lion web server for web development and testing

This is the first in, what I hope will be, a series of blogs about Web Development on your Mac.

If you are a Mac user like me, you may already know that Mac OS X has an inbuilt web server (based on Apache) that can be used to host personal web sites.

By default, the inbuilt web server is turned off.  To turn it on go to System Preferences > Sharing and check the "Web Sharing" option.  The information displayed will tell you the address of your web server's home page.  There are usually two: one for the machine e.g. http://[your Mac's name]]  and  one for your user name e.g.  http://[your Mac's name]/~[YourUserName]

Click on the links to see the home page for each addressor on the "open..." buttons to see the Folder on your Mac where the root directory is for each site.

For your personal site you should see something like this (see below right)

I am going to use my personal site e.g. the one for my user name (rather than the one for my Mac) as the focus for this blog and the ones to follow.

To make it easier to work with the files in your web site's Folder, open the appropriate Folder and drag it to your Finder's favourites.  Now you'll be able to access the root folder easily any time you need it.

If you have a Web development tool e.g. RapidWeaver, you can now publish web sites to your personal web server.

I recommend creating sub folders in the root directory of your personal web site e.g. http://[your Mac's name]/~[YourUserName]/[someSiteName]

That way you can publish and test multiple web sites at the same time and they won't interfere with each other.

That's a start! In the next blog I will tell you how to tweak your underlying Apache web server settings to install things like PHP and MySQL.

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 :)