Setup Dynamic Virtual Host (Apache2 on Ubuntu)

6.8k views Asked by At

I want to set up a single virtual host that can dynamically handle all requests based on the hostname used to access it. If %{HTTP_HOST} could be used in a DocumentRoot, this is probably exactly what I want:

<VirtualHost *:80>
    ServerAdmin [email protected]

    DocumentRoot /var/www/live/%{HTTP_HOST}/public
    <Directory />
            Options FollowSymLinks
            AllowOverride None
    </Directory>
    <Directory /var/www/live/%{HTTP_HOST}/public>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all
    </Directory>

    # Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
    LogLevel warn
    ErrorLog /var/www/live/%{HTTP_HOST}/logs/error.log
    CustomLog /var/www/live/%{HTTP_HOST}/logs/access.log combined
</VirtualHost>

...unfortunately, %{HTTP_HOST} is not allowed in the DocumentRoot (Warning: DocumentRoot [/var/www/live/%{HTTP_HOST}/public] does not exist). How else can I achieve my goal?

Update: I thought of pointing a catch-all vhost to a single directory and having a .htaccess use mod_rewrite to dynamically select the path but (honestly) I'm exhausted. I'll try at it again in the morning, but in the meantime, if anyone has good ideas, I'd love to hear them! Thank you!

2

There are 2 answers

0
Hemang Rami On BEST ANSWER

The official methods for achieving dynamic virtual hosts are explained in the Apache documentation:

http://httpd.apache.org/docs/2.0/vhosts/mass.html

2
Hassan Aly On

Maybe you can try the following solution from this article: Apache: Dynamic Virtual Hosts

A few months back I looked for a solution to overcome the problem of creating individual Virtual Hosts in Apache every time I wanted to configure a new site on a development machine (something that is a big issue in work where we have a lot of websites). Apache is able to support this functionality relatively easy using a module and a few lines in the configuration file. I set this up on Fedora 14, so results may be slightly different for other OS's (different paths, configuration file setup, etc)

Open up the main Apache conf (/etc/httpd/conf/httpd.conf), and ensure the module mod_vhost_alias is enabled. There should be a line in the configuration like

LoadModule vhost_alias_module modules/mod_vhost_alias.so 

Next, add the following lines to the bottom of this file. You'll need to edit the file with sudo privileges.

NameVirtualHost *:80

UseCanonicalName Off

<VirtualHost *:80>
    VirtualDocumentRoot /var/www/html/domains/%0 
</VirtualHost>

This sets up a catch all for any domain coming in over port 80 (the default port for http traffic, if your using https you will need to use 443 - alternatively you could remove the port restriction). The important line here is the VirtualDocumentRoot. The tells Apache where your files will reside on disk. The %0 part takes the whole domain name and inserts it into the path. To illustrate this if we went to a domain testing.com.dev the VirtualDocumentRoot would be:

/var/www/html/domains/testing.com.dev 

This type of configuration might be suitable for most situations, however I didn't want to have the .dev part of the domain in my folders on disk. I was able to achieve this by setting the VirtualDocumentRoot to:

VirtualDocumentRoot /var/www/html/domains/%-2+ 

The above example of testing.com.dev would now point to:

/var/www/html/domains/testing.com 

Remember to add the domain to your hosts file (/etc/hosts)

For a full list of options see the mod_vhost_alias documentation. Additional documentation can be found here.