Ubuntu Tips: Enabling mod_rewrite and Virtual Hosts in Apache
Set up a Virtual Host on your machine to test sites and coding more vigorously ahead of deploying to production.
23rd March 2011
As a developer you may like to run software such as Wordpress or Drupal on your development server and use the htaccess file for mod_rewrite. By setting up Virtual Host on your machine you can have your development machine masquerade as the live machine and test sites and coding more vigorously ahead of deploying to production.
It is a straight forward concept but not the most easy activity to setup this up on your own development machine as it requires a certain amount of terminal work to configure the system. It is however simplier than you may initally think. So if you want to create and test web sites locally on Ubuntu you will first need to install Apache.
You should probably also install PHP, MySQL and phpMyAdmin so you have the full LAMP software stack. If this is new to you I suggest reading my article Ubuntu Tips: Linux, Apache, MySQL and PHP. Or you can simply run the following command to install all the core elements.
sudo apt-get install lamp-server
Step 1 - Enable mod_rewrite
Lets begin. You are going to start my changing or checking the current configuration of your Apache web server to enable the mod rewrite. To enable mod rewrite run the follow command:
sudo a2enmod rewrite
Once the module is enabled we are ready to begin setting up your first virtual host for Apache. Virtual hosting is a solution to hosting multiple domain names on a single computer using one IP address. This is the principle behind shared hosting.
Step 2 - Setting up a new Virtual Host
Start by changing the current directory in the terminal to the following:
cd /etc/apache2/sites-available
Now duplicate the default configuration file. In my example I am going to setup the host matthew.dev. You should replace matthew.dev with the address you wish to use.
sudo cp default matthew.dev
Now open the new configuration file in gedit and replace the content of the file with the following. Remember to update the paths to the directory containing your website.
sudo gedit matthew.dev
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName matthew.dev
DocumentRoot /home/matthew/public_html/sites/matthew.dev
<Directory /home/matthew/public_html/sites/matthew.dev>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
</VirtualHost>
When you have completed making the changes save the file and exit gedit. Then execute the following command to create a symlink and add the new virtual host to Apache.
sudo a2ensite matthew.dev
You will be prompted to re-start Apache in order to activate the new virtual host.
sudo /etc/init.d/apache2 reload
The final tweaking requires you to add the new hostname to the hosts file and loop the name back to your internal IP address 127.0.0.1.
sudo gedit /etc/hosts
You should add the new host on it's own line in the format of IP address hostname.
127.0.0.1 matthew.dev
Once you have added the new server name to hosts file save and close the file. Then re-start Apache once more to commit the changes.
sudo /etc/init.d/apache2 reload
You have now finished! All that is left to do is begin using your new virtual host.
Links
This work is licenced under a Creative Commons Licence.