Virtual Brain Online Logo

Bookmark: Root \ Apache \ How to enable and configure Virtual hosts, vhosts, in Apache2.

How to enable and configure Virtual hosts, vhosts, in Apache2.


Last Updated: 2006-09-27

How to enable and configure Virtual hosts, vhosts, in Apache2.

Virtual hosts or vhosts enable you to host multiple domain names on one IP address. The configuration is Apache2 looks a little more complicated at first because it is spread out over so many files.

The configuration options explained below are written for a SuSE 10 installation with Apache2. Other distros might have a different configuration layout. Use


Command:
skylinux@2of9:/etc/apache2> grep -H "OptionName" *
to find an option within any file inside the current directory. Replace OptionName with the string you are looking for. Please inform me of any special requirements your Linux distro might have. Click To Update.

This Guide assumes that you have Apache2 installed and configured for Single domain use. If you enter the server's IP/domain into your browser you should see the Apache2 generated test page or your website. If not, check Virtual Brain Online for suggestions.

I strongly recommend that you make a backup before you begin
A simple tar backup of your config folder will do

Command:
2of9:/ # tar -zcvf apache2_config_backup_pre_virutalhost.tar.gz /etc/apache2/

Your backup file is now called apache2_config_backup_pre_virutalhost.tar.gz and contains all your config files under /etc/apache2.

Since I'm using SuSE 10 on my server, I double checked the settings in /etc/sysconfig/apache2

Now we need to make sure that Apache2 will actually listen for a connection to one of it's VirtualHosts.
Open listen.conf and change
#NameVirtualHost *:80
to
NameVirtualHost *:80

Next, open the default-server.conf, which is located in /etc/apache2/default-server.conf in SuSE Linux
The default-server.conf contains all the default server settings. This is also the place where I give permission to browse my server root. Adjust the configuration options below to match your own setup.
DocumentRoot /mnt/raid5/srv/www/htdocs/network-technologies.org/
<Directory "/mnt/raid5/srv/www/htdocs/">
Order allow,deny
DirectoryIndex index.php index.html
IndexOptions FancyIndexing HTMLTable VersionSort
UseCanonicalName off
Allow from all
</Directory>

All that's left now is to create the virtual hosts configuration file, apache2 wants all VirtualHost configuration stored in the directory apache2/vhosts.d/
I would recommend that you create one file per parent domain and one catch all file, this will make maintenance a lot simpler. At the time of writing, I have three files in my vhosts.d directory.
2of9:/etc/apache2/vhosts.d # ls -l
-rw-r--r-- 1 root root 292 Nov 26 10:26 catch_all.conf
-rw-r--r-- 1 root root 645 Nov 26 10:22 mirkokaiser.com.conf
-rw-r--r-- 1 root root 1468 Nov 26 10:20 network-technologies.org.conf

And each file contains:

File:
Filename: mirkokaiser.com.conf
<VirtualHost *:80>
DocumentRoot /mnt/raid5/srv/www/htdocs/mirkokaiser.com/
ServerName www.mirkokaiser.com
ServerAdmin spam@some_email_addess
ServerSignature On
CustomLog /var/log/apache2/mirkokaiser.com-access.log combined
ErrorLog /var/log/apache2/mirkokaiser.com-error.log
LogLevel warn
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /mnt/raid5/srv/www/htdocs/mirkokaiser.com/
ServerName mirkokaiser.com
ServerAdmin spam@some_email_addess
ServerSignature On
CustomLog /var/log/apache2/mirkokaiser.com-access.log combined
ErrorLog /var/log/apache2/mirkokaiser.com-error.log
LogLevel warn
</VirtualHost>



File:
Filename: network-technologies.org.conf
<VirtualHost *:80>
DocumentRoot /mnt/raid5/srv/www/htdocs/network-technologies.org/
ServerName www.network-technologies.org
ServerAdmin spam@some_email_addess
ServerSignature On
CustomLog /var/log/apache2/network-technologies.org-access.log combined
ErrorLog /var/log/apache2/network-technologies.org-error.log
LogLevel warn
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /mnt/raid5/srv/www/htdocs/network-technologies.org/
ServerName network-technologies.org
ServerAdmin spam@some_email_addess
ServerSignature On
CustomLog /var/log/apache2/network-technologies.org-access.log combined
ErrorLog /var/log/apache2/network-technologies.org-error.log
LogLevel warn
</VirtualHost>


<VirtualHost *:80>
DocumentRoot /mnt/raid5/srv/www/htdocs/network-technologies.org/
ServerName www.network-technologie.org
ServerAdmin spam@some_email_addess
ServerSignature On
CustomLog /var/log/apache2/network-technologie.org-access.log combined
ErrorLog /var/log/apache2/network-technologie.org-error.log
LogLevel warn
</VirtualHost>

<VirtualHost *:80>
DocumentRoot /mnt/raid5/srv/www/htdocs/network-technologies.org/
ServerName network-technologie.org
ServerAdmin spam@some_email_addess
ServerSignature On
CustomLog /var/log/apache2/network-technologie.org-access.log combined
ErrorLog /var/log/apache2/network-technologie.org-error.log
LogLevel warn
</VirtualHost>



File:
Filename: catch_all.conf
<VirtualHost _default_:80>
DocumentRoot /mnt/raid5/srv/www/htdocs/network-technologies.org/
ServerAdmin spam@some_email_addess
ServerSignature On
CustomLog /var/log/apache2/catch_all-access.log combined
ErrorLog /var/log/apache2/catch_all.org-error.log
LogLevel warn
</VirtualHost>


Restart your server, give it a try and let me know how it went.

Originally Posted by Skylinux @ 2005-11-26 10:46:40
Bug Fix in BB Tags
Last update by Skylinux @ 2005-11-26 17:08:45

Make sure that the configuration file ends with .conf or apache will not read it from your virtual hosts folder.
Last update by Skylinux @ 2005-12-28 21:50:46

 

No Comments yet .....

 

Add Your Comment:

Note: All posts require administrator approval. Please allow 24 hours for message approval.

Name:
E-Mail:
Title
Plain text only, less then 65 000 characters.

Please write the following string into the box below: QWERTY

Please answer the question above and type the answer into the text box below.