How To Enable PHP & MySQL in Snow Leopard
In previous versions of Mac OS X, Apple included PHP, but usually an older version that what was available at the time of the operating systems release. In Snow Leopard, Apple has included the just released PHP 5.3. Compared with other Mac OS X releases, Apple has made getting PHP and MySQL up and running much easier than before. Follow the steps below.
1. Open your favorite text editor. Mine happens to be TextMate, but any good text editor will do.
2. Navigate to /etc/apache2. It’s a hidden directory, so to get there, you will need to use the Command – Shift – G shortcut, either in the Finder, or in your text editors Open File window. This invokes the “Go To Location” option, allowing you to enter the exact path you want to navigate to. Once in the /etc/apache2 folder, look for the httpd.conf file. Open it.
3. Do a search for “php”. You should see the following line:
#LoadModule php5_module libexec/apache2/libphp5.so
Remove the “#”, which uncomments that line, enabling the php5 module in Apache.
4. Save the file. You may need to authenticate upon saving.
5. Start Web Sharing by opening System Preferences, clicking on “Sharing”, and checking “Web Sharing”.
If all you wish to do is to enable PHP, congratulations – you are done. If you would like to install MySQL and enable virtual hosts, read on.
6. First download MySQL from http://dev.mysql.com/downloads/mysql/5.1.html#macosx-dmg . If you have a 64-bit capable Mac (Intel Core Duo 2 or Xenon CPU), choose the x86_64-bit version. Otherwise, choose the x86 version.
7. The disk image will mount automatically. Double click the MySQL installer and follow the instructions. When the installer is complete, double click the installer for the MySQL Startup Item. When the Startup Item is installed, double click on the MySQL Pref Pane. When the Pref Pane loads, select “Start MySQL Server”. Unfortunately, the MySQL Pref Pane is still a 32 bit application, which means that System Preferences will need to quit and restart in 32-bit mode if you are running a 64-bit Mac. Congratulations – MySQL is now installed. Previous PHP installations in Mac OS X required you to configure where PHP looked for the mysql.sock file. Apple has fixed this in Snow Leopard.
8. Now, all that is left is to enable Virtual Hosts. To do so, first open up httpd.conf again, located at /etc/apache2/httpd.conf. Search for “virtual” in your text editor. You should see the following lines:
# Virtual hosts #Include /private/etc/apache2/extra/httpd-vhosts.conf
Uncomment the second line that starts with “#Include” by removing the “#”. We now need to modify httpd-vhosts.conf file to add our virtual hosts.
9. Open “/etc/apache2/extra/httpd-vhosts.conf” in your text editor. We are going to add an entry for our Virtual Host. For the sake of this demonstration, we will assume our site is located in a Folder called “Website” in our ~/Sites/ folder. You can store your website wherever you wish, but this is a logical location. In the httpd-vhosts.conf file, add the following entry:
DocumentRoot "/Users/username/Sites/Website" ServerName yourwebsitename
Please note that “username” will be the short name of your user account folder, and “yourwebsitename” can be whatever you like. To make it easy for me to remember I am working locally, I usually add a “.dev” or “.local” to the Servername. For example – for “yourwebsitename”, I would usually use “yourwebsitename.local”.
10. We’re almost done. To make the Servername available to you in your browser, you need to modify your hosts file, located at /etc/hosts. Open this file in your text editor, and add the following line:
127.0.0.1 yourwebsitename
This basically sets up the web address “yourwebsitename” to resolve to localhost, or 127.0.0.1. Remember, if you set “yourwebsitename” to have a “.local” or “.dev” extension in your httpd-vhosts file, you need to have it listed the same way in your hosts file.
11. Restart Apache by opening System Preferences > Sharing, and unchecking “Web Sharing”, and then checking it again to re-enable it. Test that your virtual host is active by loading up “yourwebsitename” in your browser.
Category: Tutorial,Web Development