Apache Virtual Hosts and mod_rewrite on Ubuntu Hardy

Posted by on Jun 16, 2008 in Technology, Ubuntu, Web Development23 comments

I’ve been wanting to set up a virtual host, with mod_rewrite on my system for a while, so I could have a local copy of my blog with Permalinks, for playing around, and despite a few hiccups, have finally got round to setting it up and implementing it successfully.

Apache Software Foundation Logo

Creating a Virtual Host

Note: The Apache installation in Ubuntu is actually controlled by the file /etc/apache2/apache2.conf with /etc/apache2/httpd.conf being a secondary config file loaded after apache2.conf. It’s advisable to leave apache2.conf alone, and make any server-wide changes to httpd.conf. The Ubuntu Apache installation also goes a step further by allowing site-specific configuration files, which are located in the directory /etc/apache2/sites-available. One already exists, and is called “default”, which controls the web root, /var/www. To activate any new sites created in this directory, we just create new file in this directory, then create a symbolic link to it in the /etc/apache2/sites-enabled directory. When Apache starts, it scans the apache2.conf, httpd.conf files, and also the /etc/apache2/sites-enabled directory for any links to config files in /etc/apache2/sites-available. If the link isn’t there, the configuration isn’t loaded.

For this tutorial, we’ll be creating a new config file in /etc/apache2/sites-available and activating it by creating to it in /etc/apache2/sites-enabled – apache2.conf and httpd.conf will not be touched.

Ok, I wanted to create a Virtual Host called 64bitjungledev.com, and also be able to view the site on my local installation when I point my browser to 64bitjungledev.com. The first thing I did was create a directory for the new Virtual Host to reside in. I was feeling particularly lazy, so just created it in the /var/www directory, and changed the owner of the new directory to my user name and group (so I could add/edit files etc.) I know, I know, I should create it in my home directory – but everything with “/var/www/ can be substituted for “/home/USERNAME/html/”. Anyway, in a Terminal, I executed:

sudo mkdir /var/www/64bitjungledev
sudo chown myuser:mygroup /var/www/64bitjungledev

Then, I created a new config file in the /etc/apache2/sites-available to contain the configuration information for my new Virtual Host. In a Terminal, I executed the following:

cd /etc/apache2/sites-available
gksu gedit 64bitjungledev.com

This creates a new file called 64bitjungledev.com in the directory and opens it in a text editor. In this new file, I dumped:

<VirtualHost *:80>
ServerName 64bitjungledev.com
ServerAlias www.64bitjungledev.com
DocumentRoot /var/www/64bitjungledev
DirectoryIndex index.php
<Directory /var/www/64bitjungledev>
AllowOverride all
Options Indexes FollowSymLinks MultiViews
Order allow,deny
Allow from 127.0.0.1
</Directory>
CustomLog /var/log/apache2/64bitjungledev.com-access.log combined
</VirtualHost>

This is basically telling Apache that we want it to host a new site, called 64bitjungledev.com, and all it’s files will be located in /var/www/64bitjungledev. “Allow from 127.0.0.1” tells Apache that only my computer can access it, and “AllowOverride all” tells Apache that the site can use .htaccess files that contain Apache declarations that can override these settings. The CustomLog declaration is telling Apache to create an access log file for this site in /var/log/apache2/64bitjungledev.com-access.log and the “combined” tells it to include two extra fields of information in the log file – The “Referer” (sic) HTTP request header, and The User-Agent HTTP request header. Probably superfluous considering all trafic will be refered from my local machine, and the only agent accessing the site is Firefox! You can change “combined” to “common”, which doesn;t include these two fields.

Anyway, I digress… There are two methods to enabling the new site – create a symbolic link in /etc/apache2/sites-enabled to this new file, or, have Ubuntu do it for us:

cd /etc/apache2/sites-available
sudo a2ensite 64bitjungledev.com

Will do exactly the same as:

sudo ln -s /etc/apache2/sites-available/64bitjungledev.com /etc/apache2/sites-enabled/64bitjungledev.com

To disable the site, we can run:

sudo rm /etc/apache2/sites-enabled/64bitjungledev.com

Or:

sudo a2dissite 64bitjungledev.com

The result is the same. In order for the URL 64bitjungledev.com to work in my browser, I just had to change the hosts file, so my computer looks for 64bitjungledev.com on my machine as opposed to looking it up via DNS:

gksu gedit /etc/hosts

I changed the line

127.0.0.1 localhost

to:

127.0.0.1 localhost 64bitjungledev.com www.64bitjungledev.com

Saved, and closed the file. Now, I had to restart Apache for the new changes to take affect:

sudo /etc/init.d/apache2 reload

I created a test file in the document root:

gedit /var/www/64bitjungledev/index.php

Containing the old faithful:

<?php
phpinfo();
?>

and pointed my browser to www.64bitjungledev.com. Success! Now for mod_rewrite…

Enabling mod_rewrite

First, to enable the Apache module, mod_rewrite (which is disabled on a default Hardy install), I had to create a symbolic link to the module file, so in a Terminal, I ran:

sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load

The Apache installation in Ubuntu has many different configuration files, and uses this modular approach to activae/deactivate them. The rewrite.load file simply contains the load module declaration which would normally be placed in the httpd.conf file:

$more /etc/apache2/mods-available/rewrite.load
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

When Apache starts in Ubuntu, it checks the /etc/apache2/mods-enabled directory for links to available modules, and loads the modules it finds. Creating the link above can also be achieved by running:

sudo a2enmod rewrite

which is basically a short-cut approach to creating the link in /etc/apache2/mods-enabled, similar to the a2ensite used above – and, similarly:

sudo a2dismod rewrite

will have the same effect as:

sudo rm /etc/apache2/mods-enabled/rewrite.load

That’s pretty much it. The only additional thing I had to do, was make a slight change to the /etc/apache2/sites-available/default file, which, as mentioned above, is the default Apache configuration file for /var/www. So, I opened it up:

gksu gedit /etc/apache2/sites-available/default

And changed the “AllowOverride” declaration to “all” for both the / and /var/www directories. I just needed to restart Apache once again:

sudo /etc/init.d/apache2 reload

Testing mod_rewrite

Before going ahead and installing a local copy of 64bitjungle, I wanted to test if the system was working. I needed two files for this – a .htaccess file, and a PHP file. The .htaccess file contains the test RewriteRule for Apache to process the URL redirection of “clean” URLs to the PHP script, so that www.64bitjungledev.com/xyz/somedata will actually point to www.64bitjungle.com/thescript.php?thevariable=somedata. So, in a Terminal, I created the .htaccess file first:

gedit /var/www/64bitjungledev/.htaccess

And wrote a simple Apache RewriteRule, in addition to telling Apache to switch on the rewrite engine:

<IfModule mod_rewrite.c>
RewriteEngine On
#test rule
RewriteRule ^xyz/([^/.]+)/?$ thescript.php?thevariable=$1 [L]
</IfModule>

This is basically telling Apache to switch on the RewriteEngine if mod_rewrite is loaded. The rather scrappy-but-suffices-for-testing RewriteRule basically tells Apache to “grab every character in the clean URL after xyz/ and store it in a temporaty variable, then forward it to thescript.php with thevariable set to the stuff just stored”.

A very simple PHP script can be written and used to test if the data is being passed properly:

gedit /var/www/64bitjungledev/thescript.php

which can contain simply:

<?php
echo $_GET['thevariable'];
?>

After restarting Apache, pointing my browser to www.64bitjungledev.com/xyz/DoesItWork now outputs “DoesItWork”, www.64bitjungledev.com/xyz/anything1234 ouputs “anything1234” etc. The fun just doesn’t stop…!

With mod_rewrite successfully installed, and working for this new domain, I can now install a local version of 64bitjungle.com to play with. I haven’t actually done that yet though…

References:

Tags: , , , ,