Saturday, December 19, 2009

Apache virtual hosts

Just saw an article which helped me easily create a virtual host for Apache on a Debian install (Sites and Modules, Linux Format #126, p63, but I can't find the article online to link to it). In any case, it made setting up a virtual host on my Ubuntu laptop very simple. The article explained the debian setup for Apache.  The virtual host setups are in the sites-available directory (there is also a mods-available directory which works similarly to sites).

To create a virtual site that answers to http://mysite.local on your existing apache setup in Ubuntu, do the following:

First we need a directory to hold the html. It can be anywhere, but we will put it in /var next to the www folder that already exists for the default site. And we will add a test file to be served up:

sudo mkdir /var/www-mysite
sudo nano /var/www-mysite/index.html

Nano will open with a blank file. Type something like Testing 1,2,3 and then hit ctrl-x to exit and save. Next we need to make the virtual host configuration file to point to this directory:

sudo nano /etc/apache2/sites-available/mysite.local

This will open up a new mysite.local file for editing.  Put the following in the new file:

<VirtualHost *:80>
ServerAdmin admin@localhost
ServerName mysite.local
DocumentRoot /var/www-mysite
LogLevel warn
CustomLog /var/log/apache2/mysite.access.log combined

Then save the file with ctrl-x. Next you need to tell Apache to enable the site. All this seems to do is create a symlink in the sites-enabled directory that points to the file in the sites-available directory. Type the following:

sudo a2ensite mysite.local

There is also a corresponding a2dissite which disables the site by destroying the symlink. Then we need to tell Apache to reload the configuration:

sudo /etc/init.d/apache2 reload

I get a message about not being able to recognize the fully qualified name--that doesn't matter for our purposes here.

This would all work now, except there is nothing telling your computer where to find mysite.local. (This assumes that nothing is already set up in dns somewhere pointing to your site.) Edit the hosts file with

sudo nano /etc/hosts

and add an entry with mysite.local

then press ctrl-x to save and exit.

Now put http://mysite.local into your browser and watch the fun begin! You can set up additional parts of the apache configuration by copying the configuration from default to your new file in sites-available, or consulting whatever application you are going to run to see what configuration it requires.

No comments:

Post a Comment