Apache, PHP, MySQL and VHosts on Snow Leopard

In an effort to save myself (and anyone else) some time in case I need to set this up again later, I’ve compiled some notes on how to get a really quick development setup working on Snow Leopard (OS X 10.6)

Note: Anything in a monospaced font is a terminal command, or something you should be editing in a file.

Apache

sudo apachectl start

That’s it. Check http://localhost/ to see if it works.

Virtual Hosts (VHosts)

Edit the Apache2 Configuration

sudo vi /etc/apache2/httpd.conf

Search for “httpd-vhosts.conf”, and make sure the following line is uncommented:

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

Now, lets add some folders for a test site.

mkdir ~/Sites/testsite/
mkdir ~/Sites/testsite/htdocs
mkdir ~/Sites/testsite/logs

We’ll add an HTML file as well, so we know when it’s working.

echo 'Hello, World!' > ~/Sites/testsite/htdocs/index.html

Now, we want to add our site information to our vhost config.

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

Comment out the “dummy” virtual hosts, and add this under them:

<VirtualHost *:80>
    ServerAdmin admin@localhost
    DocumentRoot "/Users/mike/Sites/testsite/htdocs"
    ServerName testsite
    ErrorLog "/Users/mike/Sites/testsite/logs/error_log"
    CustomLog "/Users/mike/Sites/testsite/logs/access_log" common
</VirtualHost>

And now we want to add a line to our hosts file to point “testsite” to our computer.

sudo vi /etc/hosts

In here, you want to add the following lines after “127.0.0.1 localhost”:

# Dev Sites
127.0.0.1 testsite

Lastly, we want to restart apache to make all the changes live.

sudo apachectl restart

Check your success @ http://testsite/

PHP

cd /etc/
sudo cp php.ini.default php.ini
sudo chown 666 php.ini
sudo vi php.ini

You absolutely need to uncomment the “;date.timezone” line, but here are a few settings and some settings I changed my values to in my php.ini. Keep in mind, the following lines do not follow each other, do a search for them to find their proper locations.

date.timezone = 'America/New_York'
post_max_size = 16M
upload_max_filesize = 16M

Now, lets actually enable PHP in Apache.

sudo vi /etc/apache2/httpd.conf

Uncomment the following line:

LoadModule php5_module        libexec/apache2/libphp5.so

and add “index.php” as an allowed index:

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

Finally, restart Apache.

sudo apachectl restart

If you want to test it, do this:

echo '<?php phpInfo(); ?>' > ~/Sites/testsite/htdocs/info.php

And visit http://testsite/info.php

MySQL

MySQL is a piece of cake for 10.6.

Download it here: http://dev.mysql.com/downloads/mysql/5.1.html#macosx-dmg

Install it in this order:

  1. MySQL
  2. MySQL Startup Item
  3. MySQL Preference Pane

I would suggest using Sequel Pro (the successor to CocoaMySQL) for a GUI Interface to your DB.

You can login to your database at 127.0.0.1, username root, no password.

Don’t forget to restart Apache when you’re done.

sudo apachectl restart

Good night, and good luck!