Video tutorial

Prerequisites

Before you start following this tutorial make sure you have MacPorts installed. I have written about that in my previous blog post and video tutorial.

Installing Apache

Installing Apache web server from MacPorts is as easy as installing any other package. You open the Terminal and issue command:

$ sudo port install apache2

Port will as usual download and install all Apache dependencies. It will take some time to install. How much time? It depends on your computer and Internet connection speed. When the installation is finished we have all the software to run Apache but we still need to do some configuration to make it work. For example if you go and try to start Apache right away you will probably get this:

This happens for example when you have fresh install of OS X 10.9 – Mavericks. If you don’t get any error messages – great! For those of you that got them here is how you solve them. The error messages are caused because hostname is not in the /etc/hosts file. To fix it first check your hostname:

$ hostname
DevMac

In my case the hostname is DevMac (yours will be different), remember it and open /hosts/etc file with nano:

$ sudo nano /etc/hosts

Add your hostname to the end of all the lines where you see localhost. After your edits /etc/hosts file should look like this:

At this point Apache should be running. You can check it with command:

$ netstat -an | less
tcp46 0 0 *.80 *.* LISTEN

If you don’t see *.80 anywhere Apache didn’t start. In that case go and read logs in /opt/local/apache2/logs/error_log. The error messages are pretty informative. If they don’t make sense to you Google them. In our case Apache started OK and is listening on port 80 (I deleted not relevant lines form the netstat command). At this point you can open your web browser and go to http://localhost or http://devmac. Both should work and you should see something like this:

The HTML code for the example site you see in your browser is located in /opt/local/apache/htdocs directory and the file displayed is named index.html. You can go there and edit it with nano. Just make sure you use sudo to edit it. After saving the file refresh the browser and you should see your changes. Cool :)

Creating virtual hosts

Now lets create some example virtual host. Go to your home directory and create folders as shown below:

$ cd ~
$ mkdir -p ws/site1

For the sake of this tutorial we will put there simple index.html file to later check if the Apache virtual host configuration works properly. In site1 directory create index.html file with following content:

<html><body><h1>Hello World! Site1</h1></body></html>

OK we have our simple site to test virtual host configuration. Next step is to configure Apache virtual hosts.

Configuring Apache

Server config

Apache has many configuration files but the main one is /opt/local/apache/conf/httpd.conf. This is where we will do our modifications first. Open it with sudo nano /opt/local/apache/conf/httpd.conf. A lot of configuration options in it are self explanatory, like User and Group the Apache server runs at, the port the server listens at (Listen) and so on. Right now we are interested in one of the lines. Find line:

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

it should be near the end of the file, and change it to:

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

We’ve removed # form the beginning of it. All lines starting with # are ignored when the configuration file is read. Removing it we asked Apache to include conf/extra/httpd-vhosts.conf file with additional configurations during start. Save the file and exit. The path after Include configuration statement is relative to where Apache was installed (in our case /opt/local/apache2).

Virtual hosts configuration

Virtual host configuration file /opt/local/apache2/conf/extra/httpd-vhosts.conf is where we will configure our site1 virtual host. Open it with nano:

$ sudo nano /opt/local/apache2/conf/extra/httpd-vhosts.conf

at the end of it you will find two example configurations for virtual hosts:

Save the file and exit. We named our virtual host site1.dev so we have to make sure it’s in our /etc/hosts file. Open it like you did before with nono and add site1.dev right after your hostname. When you are done it should look like this:

Hi,
Great tutorial. All works fine until… I include the virtual host line, I get error 403 Forbidden, when trying to load localhost, as well as when loading the virtual host.
What could be the issue?
Many thanks!

sudo bin/apachectl start
(48)Address already in use: make_sock: could not bind to address [::]:80
(48)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs