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
- 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 typesudo pico /private/etc/apache2/httpd.confFor TextMate
mate /private/etc/apache2/httpd.conf - Search for this line –
#LoadModule php5_module libexec/apache2/libphp5.so
- Remove the hash and you will have php 5 available for use -
LoadModule php5_module libexec/apache2/libphp5.so
- Save and authenticate
1b - Turn on web sharing in Leopard
- On OS X Go to system preferences > sharing
- 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
For TextMate
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.
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.
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.
- Open this webpage - if its moved, search for mysql 5 downloads
- 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.
- 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
Which starts up the new server
Then enter
Which creates the directory PHP looks for the MySQL connection in.
Now enter
To create a symbolic link to the ports file.
Now you can startup mySQL with
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:
<!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
inside
Then open terminal and run the following:
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.
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!
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?
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
> 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.
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.
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”
[...] you are installing this on a mac, and you have set up a webserver you are ready to [...]
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
[...] 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. [...]