nav-left cat-right RSS
cat-right

How to Set Up Virtual Web Hosting with Apache

Apache
Up until a couple of years ago, I used shared web hosting for serving up my various sites. I went through several of them because for whatever reason, they turned out to not be what I wanted. I finally came to the realization that I needed to get my own dedicated host. I found a good provider (The Planet), and started migrating over my websites (along with my friend Chris’ sites). Something I knew I would have to learn is how to set up Apache to be able to do virtual hosting. In this guide, I’ll show you how I did it, and the script I created to make things easier.

The first thing to do is to set up a logical directory structure — I hate putting web docs in some silly folder like /usr/local/apache2/htdocs — that’s always seemed like such a weird place to put things (I guess it goes against my feelings that anything in /usr shouldn’t be writable by users) Since this is a webserver, I wanted something closer to the root, like /www/<domain>. I also wanted to have a different directory structure and logging for each virtual host, I chose /www/logs/<domain>.

The next thing is to set up the apache config file to be able to include a separate file just for vhosts. Add this section somewhere in your httpd.conf file:

# Virtual Hosts
NameVirtualHost *
Include etc/httpd-vhosts.conf

Now, create httpd-vhosts.conf in the apache config dir, and create a manual entry to make sure it works:


    ServerName      www.mydomain.com
    ServerAdmin     webmaster@mydomain.com
    ServerAlias     mydomain.com
    DocumentRoot    /www/mydomain.com/docs
    DirectoryIndex  index.html index.php
    AccessFileName  .htaccess
    HostnameLookups Off
    ErrorLog        /www/logs/mydomain.com/error.log
    LogLevel        warn
    CustomLog       /www/logs/mydomain.com/access.log combined

(Be sure to change “mydomain.com” to your domain)

Create /www/<domain.com> and log files, as well as change ownerships properly:

# mkdir /www/logs/<domain.com>
# mkdir /www/<domain.com>
# chmod g+w /www/<domain.com>
# chown nobody:nobody /www/<domain.com>
# chown nobody:nobody /www/logs/<domain.com>

Next, create some sort of test file (like index.html) in the new doc directory.

When you’re ready, restart the webserver (usually just by running apachectl graceful) With any luck, you should be seeing the webpage you created when you go do www.<domain.com>. If not, look at the apache config for errors, and verify that the DNS entries are correct.

Now, as you were expecting, I have a script to be able to automate this. It’s pretty simple, but handy. I call it “add_vhost” and I put it into /usr/local/bin:

#!/usr/bin/perl -w
# AddVhost
# Add Virtual Host Configs for Apache
# by Ed Salisbury (ed@edsalisbury.net)
# http://www.edsalisbury.net
# (c)2009 Ed Salisbury, Some Rights Reserved
#
# License:
# Except where otherwise noted, this work is licensed under Creative Commons
#   Attribution ShareAlike 3.0.
#
# You are free:
#   * to Share -- to copy, distribute and transmit the work
#   * to Remix -- to adapt the work
#
# Under the following conditions:
#   * Attribution. You must attribute the work in the manner specified by the
#     author or licensor (but not in any way that suggests that they endorse
#     you or your use of the work).
#   * Share Alike. If you alter, transform, or build upon this work, you may
#     distribute the resulting work only under the same, similar or a
#     compatible license.
#   * For any reuse or distribution, you must make clear to others the license
#     terms of this work. The best way to do this is with a link to the
#     license's web page (http://creativecommons.org/licenses/by-sa/3.0/)
#   * Any of the above conditions can be waived if you get permission from the
#     copyright holder.
#   * Nothing in this license impairs or restricts the author's moral rights.

use strict;

my $DOCROOT = '/www';
my $LOGDIR = '/www/logs';
my $USER = 'nobody';
my $GROUP = 'nobody';
my $VHOST_CFG = "/usr/local/apache2/etc/httpd-vhosts.conf";

my $domain = $ARGV[0];

if (!$domain)
{
    print "Error: No domain specified!\n";
    exit(1);
}
open (OUT, ">> $VHOST_CFG");
print OUT "\n";
print OUT "<VirtualHost *>\n";
print OUT "\tServerName      www.$domain\n";
print OUT "\tServerAdmin     webmaster\@$domain\n";
print OUT "\tServerAlias     $domain\n";
print OUT "\tDocumentRoot    $DOCROOT/$domain\n";
print OUT "\tDirectoryIndex  index.html index.php\n";
print OUT "\tAccessFileName  .htaccess\n";
print OUT "\tHostnameLookups Off\n";
print OUT "\tErrorLog        $LOGDIR/$domain/error.log\n";
print OUT "\tLogLevel        warn\n";
print OUT "\tCustomLog       $LOGDIR/$domain/access.log combined\n";
print OUT "</VirtualHost>\n";
close (OUT);

system("mkdir $LOGDIR/$domain");
system("mkdir $DOCROOT/$domain");
system("chmod g+w $DOCROOT/$domain");
system("chown $USER:$GROUP $DOCROOT/$domain");
system("chown $USER:$GROUP $LOGDIR/$domain");

Be sure to change the config variables to suit your environment. When you want to add a virtual host, simply run:

% sudo add_vhost domain.com

It will create the config and set everything up for you. All you need to do then is to restart the webserver, and you’re good to go!

No related posts.

One Response to “How to Set Up Virtual Web Hosting with Apache”

  1. Hosting Blog says:

    This is a very useful resource for someone who is hosting on a dedicated server, if you have enough resources to go for a managed dedicated hosting then nothing like it and you will not need to hit your head over all the apache coding. Never the less thanks for the great tip.
    Hosting Blog´s last blog ..How to Redirect a folder to a Subdomain using .htaccess My ComLuv Profile

Leave a Reply

CommentLuv Enabled