##Apache setup
Apache is included with OSX. The configuration files are found at /etc/apache2/
. We'll be editing two files: /etc/apache2/httpd.conf
and /etc/apache2/extras/httpd-vhosts.conf
.
####httpd.conf
First, we need to enable some modules. Uncomment (remove the #
) the lines that contain #LoadModule php5_module libexec/apache2/libphp5.so
and LoadModule rewrite_module libexec/apache2/mod_rewrite.so
.
Just a bit below that line, you'll see this:
User _www
Group _www
Change the user to your username. For example, on my machine it reads User jweber
The default document root for Apache is /Library/WebServer/Documents/
. If you'd like to serve pages from a different directory on your machine, look for the following block:
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/Library/WebServer/Documents"
Change the DocumentRoot
to whatever directory you're using. You'll also need to find the following block a little farther down the file...
#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory "/Library/WebServer/Documents">
...and change the path there as well. In that code block that <Directory "/Libary/WebServer/Documents">
sets off, you'll find this block:
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
Change the last line to AllowOverride All
.
Finally, toward the bottom of the file you'll find this:
# Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf
Uncomment that last line to include vhosts.
####httpd-vhosts.conf
Virtual hosts allow us to host multiple domain names on one server. There are two ways to go about using vhosts for local development in Apache. The first, and simplest, is to use IP-based vhosts and serve each site through the same IP address (in this case we'll use localhost, which is the same as 127.0.0.1) with a different port number for each site. For example, on my machine I have set up the url http://localhost:8881 to serve up the local copy of Opportunity's 2013 Annual Report. Going to http://localhost:8882 serves up the local Brand Guidelines microsite. The second way to go about this is to use name-based vhosts to serve each site through the same port (port 80 is the default). For example, you could set up http://annualreport.dev to serve up the Annual Report, and http://brandguidelines.dev to serve up the Brand Guidelines. Using name-based vhosts adds an additional step to configuring a new site, but makes your dev urls easier to remember (and cooler).
#####IP-based vhosts
Edit httpd-vhosts.conf
. You should see some example configurations already in place. Use the following as an example to add your first site:
<VirtualHost *:8881>
ServerAdmin jweber@opportunity.org
DocumentRoot "/Library/WebServer/Documents/Annual-Report-2013"
ServerName localhost
</VirtualHost>
You'll also need to tell Apache to listen on that port. Add the following to the bottom of the file (it can technically go anywhere in the file, or in httpd.conf, but the bottom of the vhosts configuration file is convenient):
Listen 8881
Repeat this for each additional dev site you want to add. You'll need to add Listen [port number]
for each vhost on a separate line.
Anytime you make a change to the Apache configuration, you'll need to restart Apache for the changes to take effect. Do this with sudo apachectl restart
.
####Name-based vhosts
Similar to IP-based vhosts, but with a few changes and an additional step.
Use port 80 and set the ServerName
to the domain name you want to use. You'll want to set a ServerAlias
for any other urls, e.g. www.[domain name]
. You can add as many ServerAlias's as you'd like. Use the following example:
<VirtualHost *:80>
ServerAdmin jweber@opportunity.org
DocumentRoot "/Library/WebServer/Documents/Annual-Report-2013"
ServerName annualreport.dev
ServerAlias www.annualreport.dev
</VirtualHost>
Next, you'll need to edit the following file: /etc/hosts
. By default, it should contain something like the following:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
This file basically acts as the DNS on your machine. Each entry matches a host name with an IP address. Add a new record for each named vhost. For example, to add the Annual Report vhost from above, the hosts
file should look like this:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
127.0.0.1 annualreport.dev
Restart Apache and you should be good to go.