The Joy of Hex

Drunken Monkey Coding Style with a hint of Carlin humor

Mar 8, 2011 - 4 minute read - Apache Linux Ubuntu

Setting up virtual hosts for Apache on Ubuntu for local development

Lately I have been doing a lot of php development, and I am using Ubuntu Linux for it.

One of my biggest pains was/is setting up an alias or virtual host for the new project (yes, I am coming from the lazy windows world).

After a few tries and a lot of help from Robert Basic I put together this post to remind me next time I get a bad case of stupid.

So… First things first, I will assume that you have an Ubuntu setup with Apache web server installed, and that you are using gedit.

Fire up the terminal and type:

1
sudo a2enmod vhost_alias

If you did not get any error messages and your return looks like below, you are on the right track.

1
2
Enabling module vhost_alias.
Run '/etc/init.d/apache2 restart' to activate new configuration!

Next thing to do is to go to sites-available directory by typing

1
cd /etc/apache2/sites-available/

OK, now we are in apaches directory where all the definition files for virtual hosts are. We want to copy the default template one, cryptically named default

1
sudo cp default our-test-site

This will create a copy of the default template named our-test-site (you of course should substitute this with anything you wish). Let’s edit it, type

1
sudo gedit our-test-site

This will open up the file in the editor, below are the contents of default vhost file (as usual YMMV if you did some customization)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
	ServerAdmin webmaster@localhost

	DocumentRoot /var/www

		Options FollowSymLinks
		AllowOverride None


		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Order allow,deny
		allow from all


	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all


	ErrorLog /var/log/apache2/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"

        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128

We need to add one line and edit two lines.

Add ServerName our-test-site.local just above the DocumentRoot directive (in front of line 4).

Edit DocumentRoot /var/www path on line 4 and set it to /path-to-the-test-site-WITHOUT-trailing-slash. It should look something like this

1
DocumentRoot /path-to-the-test-site-WITHOUT-trailing-slash

In case you did not notice my subtle hints, there should NOT be a trailing slash at the end of the path.

Edit path on line 9 and set it to /path-to-the-test-site-WITH-trailing-slash/. It should look something like this

1
DocumentRoot /path-to-the-test-site-WITHOUT-trailing-slash

In case you did not notice my subtle hints, there SHOULD be a trailing slash at the end of the path.

And there you have it, almost done, the virtual host file is setup. Enable it by typing

1
sudo a2ensite our-test-site

The response should look like this

1
2
Enabling site our-test-site.
Run '/etc/init.d/apache2 reload' to activate new configuration!

At this point the virtual host setup is done, all that is left is to tell the server that our-test-site.local should be resloved to 127.0.0.1. We do that by typing

1
sudo gedit /etc/hosts

and adding 127.0.0.1 our-test-site.local after the localhost (line 1).

The entire hosts file should look like

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
127.0.0.1	localhost
127.0.0.1	our-test-site.local
127.0.1.1	ubuntu-vm

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

Save it, close the editor and finally type

1
sudo /etc/init.d/apache2 restart

or

1
sudo apache2ctl restart

So there you go, your virtual host is setup, open the browser and type http://our-test-site.local and enjoy.

Update: In case you encounter problems accessing the content of the localhost, you should add the ServerName localhost into your default virtual host (as described above for the new virtual host). Then disable and enable site, and restart the apache

1
2
3
sudo a2dissite default
sudo a2ensite default
sudo /etc/init.d/apache2 restart

Update 2: In your new virtual host file you should change your

1
AllowOverride None

to

1
AllowOverride All

for your first two directory nodes (the / one and the one with the path to your site). That will allow all the .htaccess files to work properly and allow redirection.

And of course do not forget to

1
2
3
sudo a2dissite our-test-site
sudo a2ensite our-test-site
sudo /etc/init.d/apache2 restart