Create dynamic virtual hosts in Apache HTTP with vhost_alias
Apache HTTP’s mod_user makes /home/user/public_html/
directories web-accessible as example.com/~user/
. While useful, it is often not enough. What if you need to access /home/user/public_html/project
? Sure, it is available at example.com/~user/project/
, but what if you prefer to access it on it’s own sub-domain (such as project.user.example.com
?) You can create a separate Apache VirtualHost for each project or use mod_rewrite, but you’ll still need a RewriteBase (for Drupal) and to confront the regex. Two options, but they both create room for errors and are yet-another development hassle. Instead, forget about mod_user
and the .htaccess
file. Use mod_vhost_alias. Apache HTTP Server docs describe mod_vhost_alias
:
This module creates dynamically configured virtual hosts, by allowing the IP address and/or the Host: header of the HTTP request to be used as part of the pathname to determine what files to serve.
Exactly what you need. (Note: this is written for Debian/Ubuntu, but can be adapted for CentOS/RHEL)
Enable mod_vhost_alias: sudo a2enmod vhost_alias
Create a new virtualhost file in your Apache HTTP settings directory (/etc/apache/sites-available
for Debian/Ubuntu) named project.user.dev
<VirtualHost SERVER_IP_ADDRESS:80>
ServerAdmin admin@ example.com
ServerName dev.example.com
ServerAlias *.dev.example.com
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /home/*/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
#Apache Environment variables are used in recent installs, set
#log locations specifically if not available in your distribution.
ErrorLog ${APACHE_LOG_DIR}/dev-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/dev-access.log combined
#URL structure project.user.dev.example.com
UseCanonicalName Off
VirtualDocumentRoot /home/%-4/public_html/%-5/
</VirtualHost>
Change SERVER_IP_ADDRESS to your server’s IP address. Save the file, then enable the new Virtual Host: sudo a2ensite project.user.dev
, then reload the Apache config /etc/init.d/apache2 reload
. Try out all of your new virtual hosts now. Check the dev-error.log
to diagnose any 404s.
The most important part of this is the VirtualDocumentRoot
setting. The percent-number values specify which part of the requested host name to use as the DocumentRoot
. If values are positive the count begins on the left, if values are negative the count begins on the right. When a value isn’t supplied it is replaced with an underscore.
Note: %-4
and %-5
are used instead of %2
and %1
, so requests for dev.example.com
are directed to /home/_/public_html/
, and user.dev.example
.com are directed to /home/user/public_html/
. If %2
and %1
are used, requests for dev.example.com
will be directed to /home/example/public_html/_
, and user.dev.example.com
will be directed to /home/dev/public_html/example
; not as useful, IMO, so I use the former. Additionally, %-4
and %-5
allow www.project.user.dev.example.com
to point to the correct directory, so www.*
and Domain Access will work.
Comments
(Statically copied from previous site)
Jan replied on May 24, 2013 - 4:30am PERMALINK
I get failure to reload apache:
$ sudo a2ensite project.user.dev Enabling site project.user.dev. To activate the new configuration, you need to run: service apache2 reload $ sudo service apache2 reload * Reloading web server config [fail] $
When I disable the site and reload, there are no errors. I am on Ubuntu 13.4 and using your file verbatim.
brad replied on June 1, 2013 - 10:24am PERMALINK
You cannot use it verbatim, you must change the SERVER_IP_ADDRESS value to the correct IP of your server.
If that isn’t the problem, it may be an issue with using 13.04. This article is written for 12.04 because it is the most recent LTS and that is all I’ll run on servers. Can you check the Apache logs to determine what is causing the error? You should be able to get a more detailed error message.
sathishkumar replied on July 28, 2016 - 1:45am PERMALINK
Hi,
I have tried your above method but still i got “server not found error”. I have done my configuration part correctly and the configuration part has copied below for your reference.
Kindly check with this and update the resolution for this issue