Virtual Hosting in Apache (Part 1)

If we want to publish various domains, we would not need the same number of servers as domains.

With Apache Virtual Hosting we can have all domains we need in one server.

Today is the firs part to deploy in practice this subject, in some days we will have the second part: Apache Virtual Hosting (Part 2).

There are two virtual hosting types:

  • Name based: One IP to various domains.
  • IP based: Every domain have its IP, every IPs as network interfaces have our server.

Usually (in hme servers) we take the name-based hosting, therefore we host more than one domain, or subdomains, or host other domains and portals (I hope with payment). It happens to me, then I am going to tell you my experience in some cases and concrete applications.

In Apache2 we can adminitrate virtual hosting in a directory. Then we can do so many files as hosted sites you have in the file /etc/apache2/sites-available/default.

Basic site: www

The site is a subdomain in, therefore this example runs for,,….

BE CAREFUL: Never forget type your domain in your DNS server (you can study in depth here); or throug your Domain Register if you do not have DNS server.

The tag basic structure is:

<VirtualHost *>
        DocumentRoot /var/www/tudominio

        ErrorLog /var/log/apache2/error.log
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined
        ServerSignature On

Commentin the tags one by one:

  • ServerAdmin: Web server administrator mail.
  • ServerName: What domain name petitions server must reply.
  • ServerAlias: Solve errors, as forgetful clients who forget typing www.
  • DocumentRoot: directory where I copy my published data.
  • ErrorLog: Log where the daemon type the errors. So we do not abuse of syslog.
  • LogLevel: Detail level for the errors receptioned.
  • CustomLog: Log where type your clients accessed.
  • ServerSignature: Autogenerated pages final signature; where usually appends the operating system, web server, verions, etc.

Redirection to a executable cgi out of web tree

For example we can publish the charge lever of an UPS; the manufacturer provides a cgi:

<VirtualHost *>
        DocumentRoot /var/www/tudominio/sistema

        ScriptAlias /sai/ "/usr/lib/cgi-bin/nut/"
        <Directory "/usr/lib/cgi-bin/nut">
                AllowOverride None
                Options +ExecCGI
                Order allow,deny
                Allow from all


We have the the two first lines the name for the domain of the response, and the rrot directory. Really al this the estrategy is in ScriptAliastag and the Directory directive, wich be able to  be hoste in every domain:

  • ScriptAlias: Is the nick (/sai/) which will be the directory where are hosted our cgi’s.
  • Directory: Only specifies how i managed a directory out of the tree.

In this way we call from our web browser to the cgi file as follows:

You can continue the second part of this subject in the link: Apache Virtual Hosting (Part 2).

Leave a Reply

Your email address will not be published. Required fields are marked *