Randomness

Ubuntu: name based virtual hosts

Posted on: August 21, 2007

Name-Based Virtual Hosts

With name-based virtual hosts, one instance of Apache hosts several domains. You do not need to set up multiple IPs for a machine. This is the easiest, preferred alternative. Reasons against the use of name-based virtual hosts are covered in the Apache documentation.

Configure it directly by way of the configuration file /etc/apache2/ sites-available/filename(by default we have default name file take this as reference and create new file). To activate name-based virtual hosts, specify a suitable directive. NameVirtualHost *. * is sufficient to prompt Apache to accept all incoming requests. Subsequently, configure the individual hosts:

ServerName http://www.example.com
DocumentRoot /home/www/htdocs/example.com
ServerAdmin webmaster@example.com
ErrorLog /var/log/apache2/www.example.com-error_log
CustomLog /var/log/apache2/www.example.com-access_log common

ServerName http://www.myothercompany.com
DocumentRoot /home/www/htdocs/myothercompany.com
ServerAdmin webmaster@myothercompany.com
ErrorLog /var/log/apache2/www.myothercompany.com-error_log
CustomLog /var/log/apache2/www.myothercompany.com-access_log common

A VirtualHost entry must also be configured for the domain originally hosted on the server (www.example.com). In this example, the original domain and one additional domain (www.myothercompany.com) are hosted on the same server.

Just as in NameVirtualHost, a * is used in the VirtualHost directives. Apache uses the host field in the HTTP header to connect the request to the virtual host. The request is forwarded to the virtual host whose ServerName matches the host name specified in this field.

For the directives ErrorLog and CustomLog, the log files do not need to contain the domain name. Here, use a name of your choice.

ServerAdmin designates the e-mail address of the responsible person that can be contacted if problems arise. In the event of errors, Apache gives this address in the error messages it sends to clients.

******************************************************

In Apache2 to configure virtual hosts there are two important files i.e

/etc/apache2/sites-available and /etc/apache2/sites-enabled/

We need to create symlink between these two folders for each virtual host or you can use a2ensite and a2dissite

a2ensite

This Will create the correct symlinks in sites-enabled to allow the site configured in sitefilename to be served

a2dissite

This Will remove the symlinks from sites-enabled so that the site configured in sitefilename will not be served

Advertisements

1 Response to "Ubuntu: name based virtual hosts"

Hey There. I discovered your weblog using msn.
That is an extremely smartly written article. I will make sure to bookmark it and come back to learn more of
your useful information. Thanks for the post. I will certainly comeback.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: