Reputation: 2372
Lets say I have this folder structure on disk:
/var/www/site1.domain.com
/var/www/site2.domain.com
/var/www/site3.domain.com
/var/www/site4.domain.com
/var/www/site5.anotherdomain.com
/var/www/site6.anotherdomain.com
/var/www/site7.anotherdomain.com
/var/www/site8.anotherdomain.com
These are all similar plain html/css/js websites configured in exactly the same way.
Is it possible to set up apache in such a way that incoming requests for each domain are directed to the appropriate directory without configuring hundreds of <VirtualHost..>
nodes? I guess I could auto generate them, but I'm hoping for a fancy go look in the request's domain's folder kind of configuration..
Is this possible? Even if it is possible, are there any concerns with this security or otherwise?
If it is not possible, is there another web technology like nginx for example that can do this?
Upvotes: 0
Views: 65
Reputation: 471
Yes there are security and other concerns. Is better to have one virtualhost per site with separate logging and separate SSL certificates etc.
If they are different sites is better to have they separated because:
/var/www/site1.domain.com-backup
or /var/www/secret-logs
they can be accessed.Is better to auto-generate them. You can use many templating tools from a simple bash script which accepts some parameters on envvars to more complex tools like SaltStack / Ansible.
Here a simple bash script to do it, it should be adapted to your needs:
#!/bin/bash
cat << EOF > /etc/nginx/sites-available/$SERVER_NAME.conf
server {
listen 80;
server_name $SERVER_NAME $ADDITIONAL_SERVER_NAMES;
access_log /var/log/nginx/$SERVER_NAME-access.log;
error_log /var/log/nginx/$SEVER_NAME-error.log;
location / {
root /var/www/$SERVER_NAME;
}
}
EOF
# Enable config on nginx
ln -s /etc/nginx/sites-available/$SERVER_NAME.conf /etc/nginx/sites-enabled
# Test configration and apply if no errors
nginx -t && service nginx reload
To execute the script name it nginx-site-generator
, give execution permissions and:
SERVER_NAME=site1.com ADDITIONAL_SERVER_NAMES=www.site1.com ./nginx-site-generator
Upvotes: 1
Reputation: 2372
So it turns out there is an apache module for this:
sudo a2enmod vhost_alias
Read more about the module here:
http://httpd.apache.org/docs/2.4/mod/mod_vhost_alias.html
Here is the config that worked for me:
#Define SNAME 999
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName domainname.com
ServerAlias *.domainname.com
VirtualDocumentRoot /var/www/domainname.com/%0
#RewriteEngine on
#This sets the variable to env:
#RewriteRule .* - [E=SNAME:%{SERVER_NAME}]
ErrorLog ${APACHE_LOG_DIR}/domainname.com.error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog ${APACHE_LOG_DIR}/domainname.com.access.log combined
</VirtualHost>
Upvotes: 0
Reputation: 17634
It's super easy with nginx:
location / {
root /var/www/$host;
}
Plus nginx is blazingly fast. If you wish to stick to Apache though, you ought to read this: https://httpd.apache.org/docs/2.4/vhosts/mass.html
Upvotes: 1