April 5, 2009

Apache virtual hosts quick and easy

"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:










This is my webserver working on
Port 80.
Linux-Tip.net





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:




ServerAdmin webmaster@myserver.comThis e-mail 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/"

AllowOverride None
Options +ExecCGI -Includes
Order allow,deny
Allow from all


Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all




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


ServerAdmin webmaster@myserver.comThis e-mail 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/"

AllowOverride None
Options +ExecCGI -Includes
Order allow,deny
Allow from all


Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all



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:




ServerAdmin webmaster@myserver.comThis e-mail 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/"

AllowOverride None
Options +ExecCGI -Includes
Order allow,deny
Allow from all


Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all





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



Listen 443



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" linux-tip.net

No comments:

Post a Comment