I thought I would share a technique we use here at Tierra for instantly creating subdomains on our development servers without having to touch the Apache config files or restarting Apache. This is useful for quick projects or per developer sandboxes on a project.
One Time Steps
1. Create a new virtual host in Apache with its own document root. Inside the virtual host configuration specify the following (with your domain name instead of example.com) along with the normal settings:
ServerAlias *.example.com
and then restart Apache.
2. In the new virtual server’s document root create a sub-directory called subdomains. Set the permissions on that directory so that those folks who will be creating subdomains can create directories under that directory.
3. Add the following to the .htaccess file in the new virtual server’s document root (change ‘\.example\.com’ to your domain).
RewriteEngine On
# redirect domains
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{REQUEST_URI} !^/subdomains
RewriteCond %{HTTP_HOST} ^([^.]*)\.example\.com$ [NC]
RewriteRule ^(.*)$ /subdomains/%1/$1
# fix trailing slash
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [L]
4. In your DNS add a wildcard subdomain A record, usually a subdomain named ‘*”, pointing back at the IP of the server that contains your new virtual host. If you have problems doing this through your DNS provider we recommend switching over to Nettica – they have a great UI for managing DNS at great prices.
Creating a New Subdomain
Once those four steps are completed, whenever you need a new subdomain just create a directory under subdomains and it will magically appear when you browse to that subdomain. For example, if you create a directory at subdomains/foo you can browse to it at http://foo.example.com without having to restart Apache or touch the Apache httpd.conf.
Final Notes
Some PHP scripts, like the WordPress installer, use the full internal request path, so http://foo.example.com/bar.html becomes http://example.com/subdomains/foo/bar.html. For WordPress once it is installed you can go into the options and reset the URL to the subdomain URL and it will no longer use the full URL it picked up at the install.
You’ll also note that you can’t create a subdomain named ‘www’ – this allows you to host your main server out of a different virtual server on the same machine.
I hope you find this as useful as we do. We have a LOT of these in use on our development server and we have found it’s a great way to compartmentalize projects.
Leave a Reply
Copyright © 2010 Tierra Innovation, Inc.
|
RSS | Client Login
May 14th, 2008 at 3:42 pm
If you don’t need all the features of Apache, lighttpd can do this in two lines:
simple-vhost.server-root = “/home/web”
simple-vhost.document-root = “/htdocs/”
Then, create a directory:
/home/web/host.name.com/htdocs/
And you instantly have a virtual host. Quite slick. We use this for our wiki hosting since it eases setup and lighttpd+fastcgi is such a great combination.