What I want to do is have dynamically created virtualhosts based on a username and subfolder, for example if I were to create the following folder:

/home/USER/www/PROJECT_FOLDER

The following domain will be mapped to the previous folder as its webroot

http://PROJECT_FOLDER.USER.domain.com

Aside from creating a script that checks for new folders, creating the matching VirtualHost in the Apache configuration, and restarting HTTPD, is there a config-friendly method of accomplishing this?

-- EDIT -- Thank you @kashani for suggesting mod_vhost_alias. It worked perfectly.

link|improve this question
feedback

2 Answers

up vote 3 down vote accepted

You might want to look into mod_vhost_alias. http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

The config in your case should look like this, though I haven't tested it.

VirtualDocumentRoot /home/%2/www/%1 

and for logging

LogFormat "%V %h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-Agent}i\"" dynamic_vhosts
CustomLog logs/access_log dynamic_vhosts

That'll put the vhost the request came into at the front of each log entry. You can adjust the rest of the setting to match what you need. If %V doesn't work, try %v. I don't believe there is a way to split out into separate log files at least with the standard Apache mod_log.

link|improve this answer
Thanks @kashani! After working with mod_vhost_alias for a while, that seemed to do the trick! My only concern now is if the project_folder directory has capital letters in it the apache usually translates EXAMPLE.username.domain.com as "example.username.domain.com" when in fact the directory may be "/home/username/www/EXAMPLE" – Workman Feb 3 '11 at 21:25
I can't think of a way to solve that one, but glad to hear mod_vhost_alias worked out for you. – kashani Feb 3 '11 at 21:38
One more semi-related question. Is there any way to also have virtual mapping of error and access logs using mod_vhost_alias? – Workman Feb 9 '11 at 20:24
put the logging in the answer for better formating. – kashani Feb 10 '11 at 18:47
@Workman, mod_speling might help with the case sensitivity of the folders. But I've never used it in conjunction with mod_vhost_alias: httpd.apache.org/docs/2.2/mod/mod_speling.html – Mat Feb 10 '11 at 19:01
show 1 more comment
feedback

With "ServerAlias *.example.com" and the Virtual User Hosts RewriteRule example, I believe you can easily setup apache to do what you wish

http://httpd.apache.org/docs/2.2/rewrite/rewrite_guide.html#uservhosts

link|improve this answer
Can you do this with local hostnames as well? Such as PROJECT_FOLDER.USER.dev (where 'dev' is the name of the machine)? – user69389 Feb 3 '11 at 18:37
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.