Configuring Apache Virtual Hosts on Mac OS X

Main Thread5 min read

mountaindogmedia left the following comment on my post forinstalling Apache, PHP, and MySQL on Mac OS X:

Jason, have you tried a modifiedInclude statement for virtual hosts to map a directory? So instead of/etc/apache2/extra/httpd-vhosts.conf as indicated, one would use/etc/apache2/extra/vhosts/*.conf and then just create adefault.conf for the first virtual host, and then add/edit/delete vhost files as needed. I think it would be easier to manage host files and changes.

Indeed,mountaindogmedia, this is an easier way. In fact, this is the default configuration for many servers.

By default, the Apache Virtual Host configuration on Mac OS X is located in a single file:/etc/apache2/extra/httpd-vhosts.conf. You need to edit the Apache configuration to include this file and enable virtual hosts.

Over the years, I have created many virtual hosts. Each time editinghttpd-vhosts.conf. Tomountaindogmedia's point, this becomes difficult to manage. Furthermore, Apache configurations often get reset when upgrading Mac OS X. In the same amount of steps (two), you can adopt a more manageable configuration.

What are Virtual Hosts?

From theApache Virtual Host documentation:

The term Virtual Host refers to the practice of running more than one web site on a single machine.

By default, the Apache configuration on Mac OS X serves files from/Library/WebServer/Documents accessed by the namelocahost. This is essentially a single site configuration. You could mimic multiple sites by creating subdirectories and access a site atlocalhost/somesite.

This is not ideal for several reasons. Primarily, we would rather access the site using a name likesomesite.local. To do that, you need to configure virtual hosts.

A Cleaner Configuration

Before I being, I assume you alreadyinstalled and configured Apache on Mac OS X.

First, open theTerminal app and switch to theroot user to avoid permission issues while running these commands.

1sudosu-

Edit the Apache configuration file:

1vi/etc/apache2/httpd.conf

Find the following line:

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

Below it, add the following line:

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

This configures Apache to include all files ending in.conf in the/private/etc/apache2/vhosts/ directory. Now we need to create this directory.

1mkdir/etc/apache2/vhosts
2cd/etc/apache2/vhosts

Create the default virtual host configuration file.

1vi_default.conf

Add the following configuration:

1<VirtualHost *:80>
2 DocumentRoot "/Library/WebServer/Documents"
3</VirtualHost>

I create this file to serve as the default virtual host. When Apache can not find a matching virtual host, it will use the first configuration. By prefixing this file with an underscore, Apache will include it first. Techincally this file is not needed as it simply repeats the configuraton already inhttpd.conf. However, it provides a place to add custom configuration for the default virtual host (i.e.localhost).

Now you can create your first virtual host. The example below contains the virtual host configuration for my site. Of course, you will want to substitutejasonmccreary.me with your domain name.

Create the virtual host configuration file:

1vijasonmccreary.me.conf

Add the following configuration:

1<VirtualHost *:80>
2 DocumentRoot "/Users/Jason/Documents/workspace/jasonmccreary.me/htdocs"
3 ServerName jasonmccreary.local
4 ErrorLog "/private/var/log/apache2/jasonmccreary.local-error_log"
5 CustomLog "/private/var/log/apache2/jasonmccreary.local-access_log" common
6
7 <Directory "/Users/Jason/Documents/workspace/jasonmccreary.me/htdocs">
8 AllowOverride All
9 Require all granted
10 </Directory>
11</VirtualHost>

ThisVirtualHost configuration allows me to access my site fromhttp://jasonmccreary.local for local development.

Note: I use the extensionlocal. This avoids conflicts with anyreal extensions and serves as a reminder I am developing in mylocal environment.

Note: TheRequire all granted configuration became available in Apache 2.4 which comes with Mac OS X Yosemite. If you are running a version of OS X before Yosemite, use the equivalent 2.2 configuration in theupgrading Apache examples.

The final step is to restart Apache:

1apachectlrestart

If you run into any problems, run:

1apachectlconfigtest

This will test your Apache configuration and display any error messages.

Mapping the .local extension

In order to access sites locally you need to edit yourhosts file.

1vi/etc/hosts

Add a line to the bottom of this file for your virtual host. It should match the value you used for theServerName configuration. For example, my site:

1127.0.0.1 jasonmccreary.local

I like to run the following to clear the local DNS cache:

1dscacheutil-flushcache

Now you can access your site using the .local extension. For example,http://jasonmccreary.local.

A note about permissions

You may receive403 Forbidden when you visit your local site. This is likely a permissions issue. Simply put, the Apache user (_www) needs to have access to read, and sometimes write, to your web directory.

If you are not familiar with permissions,read more. For now though, the easiest thing to do is ensure your web directory has permissions of755. You can change permissions with the command:

1chmod755some/web/directory/

In my case, all my files were under my local~/Documents directory. Which by default is only readable by me. So I had to change permissions from my web directory all the way up to~/Documents to resolve the403 Forbidden issue.

Note: There are many ways to solve permission issues. I have provided this as theeasiest solution, not thebest.

In Closing

Any time you want to add a site to Apache on your Mac, simply create a virtual host configuration file for that site and map it in yourhosts file.

Find this interesting? Let's continue the conversation onTwitter.