Wednesday, November 3, 2010

How to create virtual host on linux

sudo gedit /etc/apache2/apache2.conf

A search for the word virtual bring us to the following line:
# Include the virtual host configurations:
Include sites-enabled/

Change it like this:
# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/[^.#]*

This mean that when starting apache, it will look for files in /etc/apache2/sites-enabled/.
Lets go there and see what is in.

$cd /etc/apache2/sites-enabled/
$ls -l
total 1
lrwxrwxrwx 1 root root 36 2005-12-27 01:42 000-default -> /etc/apache2/sites-available/default

Well, this only links to the file in directory /etc/apache2/sites-available/ . You might wonder what is the point in doing such. Well, this simply allows you, mainly when you are using your box as a web server, to:

1. Have a simple main configuration file

2. Do be able to edit or create a new host by creating/editing a file from /etc/apache2/sites-available/

3. In case your web server doesn't restart because of misconfiguration, you can simply remove the link from the file in /etc/apache2/sites-enabled/ pointing to the malformed file in /etc/apache2/sites-available/

Now let say you want to be able to map the domain name dev.example.com to you local machine, using the code file in /home/myuser/public_html/example.com/.
While in /etc/apache2/sites-available, create a new file (let say example.com.conf)

$sudo gedit example.com.conf

NameVirtualHost 127.0.0.1:80

ServerName dev.example.com
ServerAlias www.dev.example.com
ServerAdmin emaildev.example.com
DocumentRoot /var/www/dev.example.com/html


Order Deny,Allow
Allow from all
# Don't show indexes for directories
Options -Indexes




Note:People who don't want to bother knowing how the site enabling system works might just jump to the end of the article to find debian built-in command syntax. If you want to know how it works, or do not use a debian based distro, carry on.

Now, we specified a new host to apache but it is not yet linked to the repertory where apache actually look for virtual hosts. Let go to:

$cd /etc/apache2/sites-enabled/

and create a link to the file we just created:

$sudo ln -s /etc/apache2/sites-available/example.com.conf example.com.conf

Now apache is almost ready to restart, but before doing so, we must inform our linux system that dev.example.com and www.dev.example.com are not to be looked for on the net, but on the local machine instead.
To do so, simply edit /etc/hosts and add the new host names at the end of the line beginning by 127.0.0.1, which is localhost.
In the end, your file should look like:

127.0.0.1 localhost.localdomain localhost dev.example.com www.dev.example.com

And now we are done, simply reload apache:

sudo /etc/init.d/apache2 reload
or
sudo /etc/init.d/apache2 restart

Open your web browser and enter the following address dev.example.com. Magic, it runs the same as when you were using http://localhost/~myuser/example.com but it is far more usefull when devellopping a web service and want to be able to develop applications on your machine just like it is where the real web site.

Edit: As you can see from the comments, many people pointed out that you can use a debian specific command (so if you are not using a debian based system, don't expect to find that command :) ).
to enable a new virtual host simply type:

sudo a2ensite mysiteavailable-site

to disable a virtual host:

sudo a2dissite mysiteavailable-site

where mysiteavailable-site is the name of the virtual hos you want to enable/disable, so in out example: example.com.conf

More info : read here

http://www.debuntu.org/2006/02/22/7-virtual-hosting-using-apache-2

2 comments:

  1. ServerName nsb.dev
    ServerAlias www.nsb.dev
    ServerAdmin admin@nsb.dev
    DocumentRoot /data/projects/nsb

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

    ReplyDelete
  2. Really good article and also it's a short and sweet one.

    ReplyDelete