By default, the Apache Virtual Host configuration on Mac OS X is located in a single file:
. 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 editing
. 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 the Apache 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
accessed by the name
. This is essentially a single site configuration. You could mimic multiple sites by creating subdirectories and access a site at
.
This is not ideal for several reasons. Primarily, we would rather access the site using a name like
. To do that, you need to configure virtual hosts.
A Cleaner Configuration
Before I being, I assume you already installed and configured Apache on Mac OS X.
First, open the Terminal app and switch to the
user to avoid permission issues while running these commands.
sudo su -
Edit the Apache configuration file:
vi /etc/apache2/httpd.conf
Find the following line:
#Include /private/etc/apache2/extra/httpd-vhosts.conf
Below it, add the following line:
Include /private/etc/apache2/vhosts/*.conf
This configures Apache to include all files ending in
in the
directory. Now we need to create this directory.
mkdir /etc/apache2/vhosts
cd /etc/apache2/vhosts
Create the default virtual host configuration file.
vi _default.conf
Add the following configuration:
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents"
</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 in
. However, it provides a place to add custom configuration for the default virtual host (i.e.
).
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 substitute jason.pureconcepts.net with your domain name.
Create the virtual host configuration file:
vi jason.pureconcepts.net.conf
Add the following configuration:
<VirtualHost *:80>
DocumentRoot "/Users/Jason/Documents/workspace/jason.pureconcepts.net/htdocs"
ServerName jason.pureconcepts.local
ErrorLog "/private/var/log/apache2/jason.pureconcepts.local-error_log"
CustomLog "/private/var/log/apache2/jason.pureconcepts.local-access_log" common
<Directory "/Users/Jason/Documents/workspace/jason.pureconcepts.net/htdocs">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
This
configuration allows me to access my site from http://jason.pureconcepts.local for local development.
Note: I use the extension local. This avoids conflicts with any real extensions and serves as a reminder I am developing in my local environment.
Note: The
configuration became available in Apache 2.4 which comes with OS X Yosemite. If you are running a version of OS X before Yosemite, use the equivalent 2.2 configuration in the upgrading Apache examples.
The final step is to restart Apache:
apachectl restart
If you run into any problems, run:
apachectl configtest
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 your hosts file.
vi /etc/hosts
Add a line to the bottom of this file for your virtual host. It should match the value you used for the
configuration. For example, my site:
127.0.0.1 jason.pureconcepts.local
I like to run the following to clear the local DNS cache:
dscacheutil -flushcache
Now you can access your site using the .local extension. For example, http://jason.pureconcepts.local.
A note about permissions
You may receive 403 Forbidden when you visit your local site. This is likely a permissions issue. Simply put, the Apache user (
) 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 of
. You can change permissions with the command:
chmod 755 some/web/directory/
In my case, all my files were under my local
directory. Which by default is only readable by me. So I had to change permissions from my web directory all the way up to
to resolve the 403 Forbidden issue.
Note: There are many ways to solve permission issues. I have provided this as the easiest solution, not the best.
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 your hosts file.