Home > Support > HOWTO List > Web > Virtual Hosts

Web howtos

Host Multiple Domains: Setting Up Virtual Hosts With Webmin

If you want to run multiple websites from your VPS, then you can use Apache's support for Virtual Hosts.

First up, where are you going to put your HTML files? You can put the files anywhere you want, but one useful convention is to have a Linux user per virtual host.  And to put the HTML files under that user's home directory.  This is especially convenient if the user will be uploading files via FTP, since if you chroot the user's FTP access then they can only access files in their home directory.

To setup a user, run something like this:


username=TheDomainNameWithoutAnyDots
adduser $username
passwd $username
mkdir -p /home/$username/htdocs
chown -R $username /home/$username

Then, to create a virtual host using Webmin:

After you have created the Virtual Host, there are a few other things you may wish to edit.

For example, click on the Virtual Host, and go to Networking and Addresses.  Enter an "Alternate virtual server names" of *.YourOtherDomain.com.  With this setting, your virtual host will serve pages for http://yourotherdomain.com/ as well as http://www.yourotherdomain.com/.

At this point you may also wish to set other options like "Log Files", so the log files for the Virtual Host end up in separate log file from the main server's log files.

To activate your changes, click "Apply Changes" on the main Apache Webmin page.

Of course, be sure to configure your DNS server so the virtual host domain name points to your server's IP address.

Webmin creates a VirtualHost directive in the Apache config file (/etc/httpd/conf/httpd.conf).  An example VirtualHost directive looks like this:


<VirtualHost *:80>
DocumentRoot /home/vhostdomain.com/htdocs
ServerName vhostdomain.com
ServerAlias *.vhostdomain.com
</VirtualHost>

See http://httpd.apache.org/docs/2.2/vhosts/examples.html for some more VirtualHost examples.

Resolving: [warn] _default_ VirtualHost overlapon port 80, the first has precedence

If you get this error when restarting Apache, un-comment the NameVirtualHost *:80 line in /etc/httpd/conf/httpd.conf.

Resolving: 403 Forbidden

Unix needs to be able to 'execute' directories in order to open them (not 'read' them as you would expect). If you get a forbidden error, make sure that the directory containing your HTML files and each of its parent directories has chmod o+x set on it. The following script should do that for you:


dir=/home/somevhostdomain.com/htdocs/; 
while true; do 
# the exit case when we get to the top level directory /
if [ -z "$dir" -o "$dir" = "/" ]; then 
    break; 
fi; 
echo chmodding o+x $dir; 
# make the directory exectuable (openable) by others
chmod o+x $dir; 
# go 'up' a directory
dir=`dirname $dir`; 
done

Redirecting an Incoming Request

Somtimes you have content which has been moved or deleted, but you'd like to return something to the client instead of the dreaded 404 response.  In this case, you can use the Redirect directive to specify what you want to do:


# Temporary Redirect (code 302)
Redirect      /your/path/file.html http:\//yourdomain.com/some/path/file.html
Redirect temp /your/path/file.html http:\//yourdomain.com/some/path/file.html

# Permanent Redirect (code 301)
Redirect permanent /your/path/file.html http:\//yourdomain.com/some/path/file.html

# Moved (code 303)
Redirect seeother /your/path/file.html http:\//yourdomain.com/some/path/file.html

# Gone (code 410)
Redirect gone	/your/path/file.html

Note, these do not have to be static html files; the incoming and redirected requests could be to any resource type.