
I’ve upgraded both my Macs to Snow Leopard, and a few things have changed with the built in Apache webserver.
As a web developer it’s important for me to have a fully functional development environment so the first thing I had to do with the new install is get Apache set up and running as I need it.
This post shows how to set up PHP, mySql, Virtual Hosts & .htaccess
PHP
By default PHP is disabled so let’s turn that on.
In the finder navigate to the /etc folder, this houses all the files we need to configure. This is a hidden system level folder so to access it press “apple shift G” and type /etc.

Find the folder Apache 2 and open httpd.conf
This is the main config file for Apache, I use textMate to edit these files but you can use any text editor.
Go to line #115 and uncomment (remove the starting #) “LoadModule php5_module”
Save the file, you will be asked to enter your password.
After every change you make to the Apache or PHP config files you will need to restart the webserver. This can be done in the Terminal with “sudo apachectl graceful” or go to System Preferences and untick then tick Web Sharing in the Sharing Pane.
Now PHP is running.
Error Messages when using date() function
PHP has been upgraded to V5.3 and the Snow Leopard version doesn’t have a timezone set, this will give you errors when you use the date() function. To fix this go back to the “/etc” folder and open “PHP.ini.default”
Go to Line #997 and enter your timezone, here in the UK it’s Europe/London find your timezone.
Remove the semicolon and the line should read “date.timezone = yourtimezone”
Save this file as PHP.ini in the etc folder, restart the webserver and PHP is now all good to go.
mySQL
mySQL doesn’t come as standard on OS X so we need to install it luckily this is nice and easy because we can get a dmg file. I choose the 64bit version, head over and grab it scroll down to “Mac OS X (TAR packages) downloads” and choose “Mac OS X 10.5 (x86_64)”.
Open the dmg and run the installer. I also installed the prefPane, double click the “MySQL.prefPane” It’s a good idea to use this as you can use System Preferences to start/stop mySQL.
Setting the password
The mySQL install doesn’t set a password so we need to set it.
For this task we need to use the Terminal. Open Terminal located in the “Applications/Utilities” folder and type “/usr/local/mysql/bin/mysqladmin -u root password yourpassword” (where yourpassword is the password you want.) hit enter and the password is set.

Fixing the PHP socket
Before we can use mySQL we need to point PHP to the correct place. Open the PHP.ini file as before and change line #1213 to mysql.default_socket = /tmp/mysql.sock
Save this file and restart the server.
phpMyAdmin
I use phpMyAdmin to manage mySQL. So here’s how to do that.
Head over to the phpMyAdmin Site and download the latest version.
Unzip the downloaded file and place in either your Sites folder or the servers root (Library/WebServer/Documents) I use the root as I class this as a management tool and like to keep my Sites folder for projects.
This needs configuring too. It does come with a documentation file so you could go through that and set it up as you like. I keep mine nice and simple and have it so I don’t need to log in each time.
Open “config.sample.inc.php” and add after “$i++” the lines:
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'yourpassword';
$cfg['Servers'][$i]['auth_type'] = 'config';
Remove the line “$cfg['Servers'][$i]['auth_type'] = ‘cookie’;”
Save this file as config.inc.php.
In a browser go to “http://localhost/phpMyAdmin/” if you moved the folder to the root as I did or if you placed into your Sites folder go to “http://localhost/~yourusername/phpMyAdmin”.
Virtual Hosts
Open the “httpd.conf” and uncomment line #461 which reads “Include /private/etc/apache2/extra/httpd-vhosts.conf”, then save.
Now open “/etc/apache2/extra/httpd-vhost.conf”
I commented out each VirtualHost block and made my own underneath, You really do need this one:
<VirtualHost *:80>
DocumentRoot /Library/WebServer/Documents
ServerName localhost
</VirtualHost>
After this block you can add your own
<VirtualHost *:80>
DocumentRoot /Users/youruser/Sites/yoursitefolder
ServerName yoursitesname
</VirtualHost>
You will need to add a new VirtualHost block for each VirtualHost you wish to set up.
Save this file and we’re almost done. Open “/etc/hosts” and add “127.0.0.1 yoursitename” you will need to add a new line for each Virtual Host you have set up where “yoursitename” matches the name chosen name in the VitrualHost block in the previous step.
Save this file and restart the webserver. Now in a browser type the VirtualHost name and you will see the corresponding site. A link to “/” within this site will take you to the root of that site as if it was on a remote server.
.htaccess
Open the “httpd.conf” file and change line #210 to read “AllowOverride All”.
Open “/etc/Apache2/Users/youruser.conf”
Change AllowOverride to All
Add a new Directory Block
<Directory "/Users/youruser/Sites/*/">
Options Indexes MultiViews FollowSymLinks
</Directory>
Save and restart the server. Everything should now be all set up and you can use .htaccess and mod_rewrite.
