Enable Apache virtual hosts on OS X 10.8 Mountain Lion

I like to place each of my projects in a subfolder of Sites. The Sites folder is no longer present in OS X 10.8, but you can create it manually from the Terminal:

mkdir ~/Sites

For instance, for the project that I will be hosting locally at http://test.site, I created a folder called test.site:

mkdir ~/Sites/test.site

First, let’s add the test.site domain to hosts file:

sudo nano /etc/hosts

At the end of the file, add:

127.0.0.1       test.site

Next step, edit /etc/apache2/httpd.conf, and uncomment:

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

Now, edit Apache’s virtual hosts configuration file httpd-vhosts.conf, and place the following:

NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents"
    <Directory "/Library/WebServer/Documents">
        Allow From All
        AllowOverride All
    </Directory>
    CustomLog "|/usr/sbin/rotatelogs /private/var/log/apache2/default-access_log 86400" combined
    ErrorLog "|/usr/sbin/rotatelogs /private/var/log/apache2/default-error_log 86400"
</VirtualHost>
<VirtualHost *:80>
    ServerName "test.site"
    DocumentRoot "/Users/damiano/Sites/test.site"
    <Directory "/Users/damiano/Sites/test.site/">
        Allow From All
        AllowOverride All
    </Directory>
    CustomLog "|/usr/sbin/rotatelogs /private/var/log/apache2/test.site-access_log 86400" combined
    ErrorLog "|/usr/sbin/rotatelogs /private/var/log/apache2/test.site-error_log 86400"
</VirtualHost>

The first entry points to the default web site, which will also work as a catch-all host for any virtual host that wasn’t defined anywhere else. The second entry points instead to the~/Sites/test.site folder created for http://test.site

Finally, restart Apache to load the new configuration:

sudo apachectl restart
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: