Journal

Setting Up a Local Web Development Environment on Snow Leopard

By default Snow Leopard comes bundled with Apache, PHP 5.3 and common libraries like GD that you’re likely to need for local web development. Since it’s quite easy to forget the location of the edits required I’m documenting the setup process for my own needs but hopefully it’ll help a few of you tearing your hair out over getting a stable local development environment working. Let’s get started.

Make sure Apache is running

First of all you need to make sure Apache is actually running. Open up System Preferences > Sharing and enable Web Sharing by checking the box. Or if you prefer using Terminal you can type the following (you’ll be prompted for your password):

sudo apachectl start

Now if you visit http://localhost in your browser you should see a default holding page.

Enable Virtual Hosts

First of all you’ll need to enable virtual hosts since they are disabled by default. I’m assuming you have TextMate installed (if you haven’t you should) otherwise you can substitute ‘mate’ for ‘pico’, ‘vi’, ‘mvim’ or whichever editor you prefer for the following commands.

sudo mate /etc/apache2/httpd.conf

Find the following line:

#Include /private/etc/apache2/extra/httpd-vhosts.conf

and remove (uncomment) the # from the beginning so it now reads:

Include /private/etc/apache2/extra/httpd-vhosts.conf

Enable PHP5

While you have httpd.conf open it’s a good time to enable PHP5 too.

Find the following line:

#LoadModule php5_module        /opt/local/apache2/modules/libphp5.so

and remove (uncomment) the # from the beginning so it now reads:

LoadModule php5_module        /opt/local/apache2/modules/libphp5.so

Now save and close httpd.conf as we’re done with it for now. In the future if you ever need to enable or disable additional modules this is the place to do it.

Adding the Virtual Hosts

Next you’ll want to add your virtual hosts so Apache knows where to find your local sites.

sudo mate /etc/apache2/extra/httpd-vhosts.conf

You can safely remove the two existing virtual host examples in there and add in the following:

<VirtualHost *:80>
  <Directory /Users/davebrookes/Sites/e2_davebrookes/www>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
  </Directory>
  DocumentRoot /Users/davebrookes/Sites/e2_davebrookes/www
  ServerName mysite.local
</VirtualHost>

You’ll need to add a virtual host for every local site you want to setup. My local sites are stored in my Sites folder but you can change this if you wish. Make sure you set your short username in both paths to your site root, the directory name of your site and the server name to something relevant to you. Remember your server names, you’ll need these in the next step.

Save and close httpd-vhosts.conf

Adding the local hosts

sudo mate /etc/hosts

Add in the following. You’ll of course need to replace mysite.local with whatever you called your site in the previous step. Make sure you add each site host on a new line.

127.0.0.1    mysite.local

At this point you can check to see if virtual hosts are working as expected, once you’ve saved your hosts file you’ll need to restart Apache so it picks up the changes you’ve just made.

sudo apachectl restart

All going well Apache should restart without error. If you now visit mysite.local in a web browser you should be presented with the root of your site. If you’ve no content you can always drop a phpinfo.php file in there and check things are working as expected.

If Apache hasn’t restarted successfully check to ensure everything is correct and that you don’t have any strange invisible characters in your httpd-vhosts.conf file from copying and pasting examples. If you’re still having troubles open up Console (Applications > Utilities > Console) and check your Apache error log for clues.

Installing MySQL

If you’re setting up MySQL driven sites then you’re probably looking at a database error right now. Let’s sort that out.

First of all go and download MySQL for Mac OS 10.6 – you’ll most likely be needing the 64bit DMG version.

Install MySQL first, then the startup item and finally the preference pane. When you’re ready open up system preferences and start the MySql server from the preference pane.

Next you’ll need to make sure Apache knows about our newly installed MySQL server and that brings us to…

Fixing php.ini

You need to rename the php.ini.default to php.ini so that Apache loads it up and honors any changes you make to it.

cd /etc
sudo mv php.ini.default php.ini
sudo mate php.ini

You then need to replace three occurrences of:

/var/mysql/mysql.sock

to:

/tmp/mysql.sock

While you’re here you can also fix the timezone error that will occur if you’re using the PHP date() function as there is no timezone set by default.

Find the following line:

date.timezone =

and add in your timezone as specified in the PHP documentation like this:

date.timezone = "Europe/London"

If for some reason you need to run software which requires short tags then you can enable this here too. Simply change:

short_open_tag = Off

to:

short_open_tag = On

You can also change other useful settings in here to meet your requirements. When you’re done in php.ini save the file and restart Apache again:

sudo apachectl restart

Managing Local Databases

Now that you have a database connection you’ll need some way of creating and managing databases. Of course you can use the command line at this point to do that but at a minimum you should change the default root password:

/usr/local/mysql mysqladmin -u root password 'newpassword'

You’re then free to install PHPMyAdmin (set it up as a local site) or use a free application like Sequel Pro to connect to your MySQL server with your new credentials where you can then import your databases and setup relevant users. Personally I prefer Sequel Pro as it’s easier to connect to remote servers.

Apple Updates

Be careful of updating Snow Leopard though Software Update as Apple have a habit of applying security patches to Apache that can sometimes overwrite your changes without listing them in the main change log (you’ll have to hunt through the logs on the Apple site). Because of this I’d at least backup your httpd-vhosts.conf and changes to php.ini just in case.

Still Having Troubles?

Take a look at MAMP, especially if you need PHP 5.2 support as there will come a time when you need PHP 5.3 as well so this gives you the flexibility to easily switch between versions.