Sunday, April 20, 2008

Virtual Web Domain Host with Linux

http://www.linux-tip.net/cms/content/view/323/26/

Virtual hosts are used to run more than one web site on a single machine. Virtual hosts can be "IP-based", meaning that you have a different IP address for every web site, or "name-based", meaning that you have multiple names running on each IP address. You can also run your web pages on different ports like 8080 or 8090. The fact that they are running on the same physical server is not apparent to the end user. This workshop describes the different setups based on an OpenSuse 10.2 server.


I want to say first, that this is not the only way of setting up an Apache server. There are many ways of achieving this goal but this is the way I have taken. I do not issue any guarantee that this will work for your server or distribution.
We'll setup all together four different web pages in separate locations (directories) and will use different start pages (index.html) to proof the concept.

The workshop setup could look like this:

Step 1: Creating the index pages

Like already mentioned before, we have to create subdirectory and page index files first. Suse normaly stores the web pages in the following directory:

/srv/www/htdocs

We will use the same directory but will create a subdirectory for every virtual host.

mkdir /srv/www/htdocs/server_port80 mkdir /srv/www/htdocs/server_port8090 mkdir /srv/www/htdocs/server_www mkdir /srv/www/htdocs/server_www1

You can later store you content in this directories. Let's just create a single file called index.html that contains a message about the type of the server. An example file could look like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> <title></title> </head> <body> <div style="text-align: center;"><span style="font-weight: bold;">This is my webserver working on Port 80.<br> <a href="http://www.linux-tip.net">Linux-Tip.net</a></span></div> </body> </html>


Please change the content to your needs and save a modified index file in every subdirectory.

Step 2: Setup IP based virtual host
Suse stores the vhost configuration files in the following directory:

/etc/apache2/vhosts.d
During the start-up process, Apache will automatically use all files located in this directory for the final configuration.

You can easily create new vhost configuration files by using the template like this:

cd /etc/apache2/vhosts.d/ cp vhost.template vhost-port80.conf

This will copy the default template and will create a configuration file that we will later use for our IP-based virtual host running on port 80. I recommend using vi to edit the file. Please open it like this:

vi vhost-port80.conf

As you can see, the file has everything you need to setup a virtual host, but also includes a lot of explanations and comments. To get a slim file please delete it.

Here are the lines you should change for your Apache configuration:


VirtualHost – set IP address and port here
ServerAdmin - your webmaster's email adress
DocumentRoot – path to your web page contend (see step1)
ErrorLog - path to the error log file
CustomLog - path to the access log file
UseCanonicalName - leave it to off in this case
ScriptAlias – if you like to run cgi scripts on your web page, this is the location.

The file could look like this:

<VirtualHost 192.168.33.101:80> ServerAdmin webmaster@myserver.comThis email address is being protected from spam bots, you need Javascript enabled to view it ServerName server.myserver.com DocumentRoot /srv/www/htdocs/server_port80 ErrorLog /var/log/apache2/server_port80.log CustomLog /var/log/apache2/access_port80.log combined HostnameLookups Off UseCanonicalName Off ServerSignature On ScriptAlias /cgi-bin/ "/srv/www/htdocs/server_port80/cgi-bin/" <Directory "/srv/www/htdocs/server_port80/cgi-bin"> AllowOverride None Options +ExecCGI -Includes Order allow,deny Allow from all </Directory> <Directory "/srv/www/htdocs/server_port80"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost>

If you like to run you web page on a different port, please use the following configuration file:

<VirtualHost 192.168.33.101:8090> ServerAdmin webmaster@myserver.comThis email address is being protected from spam bots, you need Javascript enabled to view it ServerName server.myserver.com DocumentRoot /srv/www/htdocs/server_port8090 ErrorLog /var/log/apache2/server_port8090.log CustomLog /var/log/apache2/access_port8090.log combined HostnameLookups Off UseCanonicalName Off ServerSignature On ScriptAlias /cgi-bin/ "/srv/www/htdocs/server_port8090/cgi-bin/" <Directory "/srv/www/htdocs/server_port8090/cgi-bin"> AllowOverride None Options +ExecCGI -Includes Order allow,deny Allow from all </Directory> <Directory "/srv/www/htdocs/server_port8090"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost>

A web server normally listens on port 80. If you like to change this or to add port 8090, you have to hack the listen.conf file. Please see step 4 for more information.

Step 3: Setup a name-based virtual host

Create a new file using the default template similar to step2:

cd /etc/apache2/vhosts.d cp vhost.template vhost-www.conf

Here are the important hacks you have to make:
ServerName – use name you would like to see in the URL
UseCanonicalName On

Remark:
With UseCanonicalName on Apache will use the ServerName and Port directives to construct the canonical name for the server. This name is used in
all self-referential URLs, and for the values of SERVER_NAME and SERVER_PORT in CGIs.

If you have more IP addresses for your server or if you like to use different ports, please change the following line:

VirtualHost 192.168.33.101:80

The file could look like this:

<VirtualHost 192.168.33.101:80> ServerAdmin webmaster@myserver.comThis email address is being protected from spam bots, you need Javascript enabled to view it ServerName www.myserver.com DocumentRoot /srv/www/htdocs/server_www ErrorLog /var/log/apache2/server_www.log CustomLog /var/log/apache2/access_www.log combined HostnameLookups Off UseCanonicalName On ServerSignature On ScriptAlias /cgi-bin/ "/srv/www/htdocs/server_www/cgi-bin/" <Directory "/srv/www/htdocs/server_www/cgi-bin"> AllowOverride None Options +ExecCGI -Includes Order allow,deny Allow from all </Directory> <Directory "/srv/www/htdocs/server_www"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost>


Step 4: Hack the listen.conf file

The following file allows you to bind Apache to specific IP addresses and/or ports.

Please change the following lines here:

Listen - add the port or ports on which you would like to run you web pages
NameVirtualHost - this directive tells Apache on which IP address and, optionally, which port to listen for requests by clients containing the domain name in the HTTP header. The first argument can be a fully qualified domain name, but it is recommended to use the IP address. The second argument is the port and is optional. By default, port 80 is used and is configured via the Listen directive.

The file could look like this:

Listen 80 Listen 8090 <IfDefine SSL> <IfDefine !NOSSL> <IfModule mod_ssl.c> Listen 443 </IfModule> </IfDefine> </IfDefine> NameVirtualHost 192.168.33.101:80


To trouble shot you configuration, you should run the following command from the linux console:

tail –f /var/log/messages

Please do not forget the restart Apache after changing the configuration using the following commands (watch for errors):

service apache2 restart

or

/etc/init.d/apache2 restart

Download the example files here.

No comments: