Apache 2.x userdir_mod
Well, Titan Network has been using a method to fix/update/create sites within the same LAMP environment as the server the live sites are on… So Dan Da Cunha implemented a brilliant idea using apache’s symbolic links (symlinks).
We’d log into the server without respective user-names and passwords, and once in we’d simply go to our user-named subdomain folder and do the following.
$ cd ~/svn $ ln -s project_name active |
However, the back-end of it was a bit of headache. First we had to create a CNAME in our DNS records for each developer, then create vhosts for each individual user-based sub-domain.
That wasn’t as big a deal as I’m making it out to be… but I’m a fan of SIMPLER, lol. Plus the ultimate con to this setup is that you only get to point ONE folder to your active project. So if you want to have someone check out your progress and move onto another project in the interim… you couldn’t.
So I had an idea to utilize the userdir_mod Apache2 uses; Create one CNAME in the DNS.
CNAME: *
Alias To: your domain
That’s called a wildcard CNAME. It lets us use pretty much anything for a sub-domain without having to create multiple records for each one. And with a few tweaks to the default vhost record and the userdir_mod, we were running each project folder we had from our /home/usename/public_html directory in a 2 part sub-domain.
http://<project_name>.<username>.domain.com
Yea, I know… you’re asking why don’t we just edit them from the folder root… well because all the sites we make are for domain root… and that would mess up a lot of CSS and image linking.
Steps to do this on your server. (Assuming you’re using the Apache packed with PHP and using Vhosts)
Step 1
Open /etc/apache2/sites-available/default in your favorite text editor.
Step 2
Sadly, now that we have a wildcard CNAME, supercalifragilisticexpialidocious.domain.com will bring up your site’s default vhost location’s directory listing (given you have autoindex_mod enabled)
TAKE NOTE, whether your VirtualHost file is headed like this <VirtualHost *:80> or something else, because step 3 is going to need the same structure.
Either put a faux index.php file in the “DocumentRoot” location
– OR –
Edit the first <Directory /> node to have Options Indexes changed to Options -Indexes so the directory won’t list and it will just reply with a HTTP/1 403 error.
– OR –
Put DirectoryIndex /path/to/file/name.php in your first line under the first <Directory /> node and point it to a php script that forwards them off to the main site or something, use your imagination!
Step 3
Open /etc/apache2/mods-available/userdir.conf in your favorite text editor.
Slap a # in front of each line in the orignal text to comment it out, and copy and paste the snippet below (make your edits post-paste).
<VirtualHost *:80> <IfModule mod_userdir.c> UseCanonicalName Off ServerName *.domain.com ServerAlias *.*.domain.com UserDir /home/*/public_html UserDir disabled root VirtualDocumentRoot /home/%2/public_html/%1 <Directory /> Options +Indexes FollowSymLinks DirectoryIndex index.php index.html index.htm AllowOverride All </Directory> ServerAdmin support@domain.com </IfModule> </VirtualHost> |
Obviously you can change some things around to your likings.
%1 is the first segment of your URL/URI
%2 is the second segment of your URL/URI
The reason we don’t put the user-name first in the URL is because if there is an already existing subdomain with the same name is your home directory’s public_html sub-directory, they will conflict and return a server error.
All underlying info can be found at http://httpd.apache.org/docs/2.0/mod/mod_vhost_alias.html.
From here you simple enable your userdir mod (if you haven’t already) and give it a test.
NOTE: DNS records take about an hour to propagate, so if it doesn’t work right away to be surprised.
read more






