Apache Multi-site Configuration for Mac OS X

Author: MikeW    Date: 2016-08-28 11:49   

This is a tip on how to configure Apache 2 to support multiple sites on one server for Mac OS X El Capitan, Mavericks, and Yosemite. This is a much improved process thanks to this write up on Apache and vhosts. A vhosts approach is used to configure the server. The makes the setup simpler and quicker.

Note: This tip assumes you have some knowledge of Mac OSX system administration. You need to use sudo to edit and create files and directories. Make sure you can use either nano or vi before attempting to edit any configuration files.

Setup /etc/hosts for Multiple Sites

To be able to use multiple sites on one server, you must first update the /etc/hosts file. Add an entry for each of your sites to the localhost address. For example:

  1. sudo nano /etc/hosts
  2. Add the sites you want to add to the server to the localhost entry:
    • 127.0.0.1 localhost site1 site2

Setup the vhost files

Before the Apache configuration file is edited, a little setup. is required.

  1. Open a Terminal window.
  2. Become super user: sudo bash
  3. cd /etc/apache2
  4. mkdir vhosts
  5. Copy the vhost files into the the directory.
    • Files are included in alphabetical order.

Sample Files

Here are some sample files that demonstrate how you could setup each test site on the Apache server.

Imporant Note: The Apache server needs to be able to read the directory your files are in. If you are like me and your files are stored in a subdirectory under your home directory, the default permissions (700) may cause access issues. Make sure any web directories under your home directory have their permissions to 755. That should fix any access issues. If you need to write to a web directory, you may need to create a new web directory with sudo/root and set the directory group to staff.

00Default.conf

This is a default vhost. Any unamed url, (e.g., localhost) will serve up the page using this setting.

<VirtualHost *:80>
    DocumentRoot "/Users/myhome/www/site1"
    <Directory "/Users/myhome/www/site1">
	Options Indexes FollowSymLinks
	AllowOverride All
	Require all granted
    </Directory>
</VirtualHost>

site1.conf

Here is the configuration for site1 in the www directory.

<VirtualHost *:80>
    ServerAdmin admin@example.com
    DocumentRoot "/Users/myhome/www/site1"
    ServerName site1
    ServerAlias site1.local
    ErrorLog "/private/var/log/apache2/site1-error_log"
    CustomLog "/private/var/log/apache2/site1-access_log" common
    <Directory "/Users/myhome/www/site1">
	Options Indexes FollowSymLinks
	AllowOverride All
	Require all granted
    </Directory>
</VirtualHost>

site2.conf

Here is the configuration for site2 in the www directory.

<VirtualHost *:80>
    ServerAdmin admin@example.com
    DocumentRoot "/Users/myhome/www/site2"
    ServerName site2
    ServerAlias site2.local
    ErrorLog "/private/var/log/apache2/site2-error_log"
    CustomLog "/private/var/log/apache2/site2-access_log" common
    <Directory "/Users/myhome/www/site2">
	Options Indexes FollowSymLinks
	AllowOverride All
	Require all granted
    </Directory>
</VirtualHost>

Configure the httpd.conf

Edit the httpd.conf file. Uncomment these two lines to enable PHP5 ad mod_rewrite.

LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php5_module libexec/apache2/libphp5.so

Then scroll down to this comment:

# Virtual hosts

And add this line.

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

So now, any .conf file placed into the vhosts directory will automatically load when Apache starts.

Restart Apache

To restart apache:

apachectl restart

Finished

That is it. Now to add a new web site to Apache, just put a new site file in the vhosts directory, update your /etc/hosts file, and restart Apache. Voila!