Mark Kirby

Set up PHP 5, Apache 2 and MySQL 5 on OS X Leopard

The aim of this tutorial

You have a new mac, with leopard installed. You want to be able to test your PHP websites on your mac (in my case, my wordpress blog), so you can make sure things work before uploading to your webserver. You, like me, are unsure how to go about this.

By the end of this tutorial you will type the address localsite into your browser, and be able to see your PHP database driven website. You can edit files, and see the changes locally, without ever having to go online.

Step 1 – Apache and PHP

Leopard comes with Apache 2 and PHP 5 preinstalled. All we need to do is activate it.

1a - Change the apache conf file to activate PHP

  1. Using a text editor such as smultron, TextMate or command line tool Pico, which lets you open hidden files. Open Apache’s conf file on your system at /private/etc/apache2/httpd.conf
    On Pico open terminal and type

    sudo pico /private/etc/apache2/httpd.conf

    For TextMate

    mate /private/etc/apache2/httpd.conf
  2. Search for this line –
    #LoadModule php5_module libexec/apache2/libphp5.so
  3. Remove the hash and you will have php 5 available for use -
    LoadModule php5_module libexec/apache2/libphp5.so
  4. Save and authenticate

1b - Turn on web sharing in Leopard

  1. On OS X Go to system preferences > sharing
  2. Tick the web sharing box

You should now be online!

Enter localhost in your browser, you should see the apache webpage.

You then put your files in /Library/WebServer/Documents

e.g. i put a folder called website inside the Documents folder. I can access this by typing localhost/website in my browser.

Easy!

1c - Optional - changing the location of your websites to the Sites folder in your home directory

Some people prefer not to keep their development files in /Library/WebServer/Documents. Fortunately its very easy to set up Apache so that it gets the files from your Sites folder.

Using a text editor such as smultron, TextMate or command line tool Pico, which lets you open hidden files, open Apache’s conf file on your system at /private/etc/apache2/httpd.conf

On Pico open terminal and type

sudo pico /private/etc/apache2/httpd.conf

For TextMate

mate /private/etc/apache2/httpd.conf

Find this:

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot “/Library/WebServer/Documents”

and replace with the path to your sites folder, mine is “/Users/myname/Sites”, myname is the name of my home directory.

e.g.

DocumentRoot “/Users/myname/Sites”

Then find the code:

#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory “/Library/WebServer/Documents”>

And change there as well, e.g.

<Directory “/Users/
myname
/Sites”>

Now restart web sharing (go back into system preferences and uncheck, then recheck the box).

Now when you type localhost into your browser, it will give you the contents of your Sites folder.

Step 2 – MySQL

MySQL provides us with the database element of the package.

2a - Install MySQL

MySQL does not come with Leopard, and the package for Tiger will not work without some modifications. These are described here, but do take care to look for a Leopard package first over at mysql.com as this is the best thing to use.

  1. Open this webpage - if its moved, search for mysql 5 downloads
  2. Find the mac os x package (.dmg files) section. If 10.5 is still unavailable, pick the one for 10.4, x86 if your on an intel mac, otherwise Power PC.
  3. Install the MYSQL package, leaving the prefpane and startup item if your using the 10.4 installer.

2b - Fix MySQL - ONLY complete this section if you downloaded the 10.4 Tiger package

The problem with the Tiger mySQL package is that it doesn’t use the same settings as PHP so PHP can’t find it. Its easy enough to fix - we just build a symbolic link from the expected location to the actual location as follows:

Open terminal (Applications > Utilities > Terminal)

In Terminal, type

sudo /usr/local/mysql/bin/safe_mysqld

Which starts up the new server

Then enter

sudo mkdir /var/mysql/

Which creates the directory PHP looks for the MySQL connection in.

Now enter

sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

To create a symbolic link to the ports file.

Now you can startup mySQL with

sudo /usr/local/mysql/bin/safe_mysqld

and connect using the settings:

  • Address - localhost or 127.0.0.1
  • Username - root
  • Password - none (thats no password, not the word none!)

2c - Create a startup item so MySQL starts up whenever the Mac does

This might not be needed with the 10.5 MySQL release, it should come with a package to do this. If it does not, or you are using the 10.4 release, read on.

Make the following file using your preferred text editor:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.mysql.mysqld</string>
<key>Program</key>
<string>/usr/local/mysql/bin/mysqld_safe</string>
<key>RunAtLoad</key>
<true/>
</dict></plist>

Save this as

com.mysql.mysqld.plist

inside

/Library/LaunchDaemons

Then open terminal and run the following:

sudo chown root /Library/LaunchDaemons/com.mysql.mysqld.plist
sudo launchctl load /Library/LaunchDaemons/com.mysql.mysqld.plist

Which sets the owner as root and ensures it auto loads each time

3 - Your done!

Thats it! Now PHP should simply work with the default connections.

To add databases and edit your mySQL setup, simply download mySQL admin to access your server.

Log in initially with mysql admin using the following settings:

  • server hostname - localhost
  • Username - root
  • Password - leave blank

Using mysql server admin you can now change the root password, build databases, and lots more.

Thanks to Red91 whose blog post Installing MySQL with Leopard helped me to set up the MySQL side, mymacinations whose blog post helped me to get PHP started.

9 Responses to “Set up PHP 5, Apache 2 and MySQL 5 on OS X Leopard”

  1. On January 24th, 2008 at 3:41 pm, Tom Sagona said

    Great tutorial, very comprehensive and easy to use. One thing you might change is at the beginning. The user doesn’t have to install smultron to open the hidden file. Using sudo pico in terminal will open it just fine. But other than that it was extremely helpful. Thanks!

  2. On February 20th, 2008 at 10:40 am, Adam said

    I’ve followed this and its a great tutorial. I’ve managed to define my ‘Sites’ folder as the location. This works fine and Apache is turned on. I’ve followed the steps and removed the # to turn on PHP but when i’ve tested it in a browser it doesn’t seem to be working. Anything i’m missing?

  3. On April 4th, 2008 at 7:34 pm, Simon Bailey said

    Thanks for this, worked a treat form me, just what I needed, now got php, mysql and ColdFusion 8 all running out of my Sites folder, wicked thanks :)

    Simon

  4. On April 11th, 2008 at 10:42 pm, Nick Wright said

    > I’ve followed the steps and removed the # to turn on PHP
    > but when i’ve tested it in a browser it doesn’t seem to be working.

    Enter:
    sudo apachectl restart
    to restart Apache. Alternatively go to System Preferences / Sharing and turn Web Sharing off and on again to achieve the same affect.

    Thanks for the help Mark, very useful.

  5. On April 21st, 2008 at 10:10 am, Marco Randon said

    Great tutorial, very easy. But I have a problem, Apache2 can’t follow symlinks (or alias as well). I want to put a link to my website dir on /Library/WebServer/Documents so it acts like a subdirectory, but it does’t work.
    ln -s ~/Documents/websitedir /Library/WebServer/Documents/websitedir
    Any suggestions are appreciated.

  6. On April 21st, 2008 at 10:44 am, Marco Randon said

    Finally i’ve got it.
    Make you sure that ALL the path to the directory you want to link have the right permissions.
    In case:
    chmod 755 “directory to change permissions”

  7. On May 23rd, 2008 at 12:08 pm, Mark Kirby » Blog Archive » How to install wordpress using ssh and svn said

    [...] you are installing this on a mac, and you have set up a webserver you are ready to [...]

  8. On May 26th, 2008 at 3:06 pm, Jo Dimbleby said

    Bless you, Mark.

    Complete newby to PHP and MySql. Have been struggling with the darn mysql.sock location all this morning. Now solved thanks to you and…

    sudo mkdir /var/mysql/
    sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

  9. On August 14th, 2008 at 7:10 pm, A Digital Notepad » Bookmarks for August 12th through August 14th said

    [...] Set up PHP 5, Apache 2 and MySQL 5 on OS X Leopard - Nice simple walk through when setting up a new Mac for web development. [...]

Leave a Reply

This site was created with Wordpress, using my own template.