Snow Leopard Apache vhosts

Ok, while setting up my working environment, I had to define vhosts for the sites/applications I’m working on. So here is how I configured Apache.

sudo vi +/'# Virtual hosts' /etc/apache2/httpd.conf

This text will open the httpd.conf file and set the cursor at the first line where he encounters the string # Virtual hosts. Now, you could remove the comment block so that Apache would use only one configuration file for all your vhosts, but I find that a bit dirty. I prefer 1 file per vhost. So I added the following line:

Include /private/etc/apache2/extra/vhosts/*.conf

Now, lets set the logging correct. I like the error logs to look like error.log, see the notices and have a custom logformat. So set the following entries like this:

ErrorLog "/private/var/log/apache2/error.log"
LogLevel notice
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined

So now Apache will load all vhost files located under /private/etc/apache2/extra/vhosts ending with a .conf. But watch out, the folder “vhosts” doesn’t exists yet, so you will have to create it!

sudo mkdir /private/etc/apache2/extra/vhosts

Ok, next step is to create the a vhost. In this example, I’ll make a vhost for phpMyAdmin. Create a config file in the newly create vhost folder (phpmyadmin.conf) en insert the following content:

<VirtualHost *:80>
  DocumentRoot /Users/michael/Sites/phpMyAdmin/
  ServerName phpmyadmin.local
  ErrorLog "/private/var/log/apache2/phpmyadmin.error.log"
  CustomLog "/private/var/log/apache2/phpmyadmin.log" combined
</VirtualHost>

Since the phpmyadmin.local url doesn’t exist, we have to tell our system that it is a local url and that no DNS lookup is required. Edit /etc/hosts file to add the url.

 127.0.0.1 phpmyadmin.local

Now, just restart Apache and surf to the url 🙂