When working with a multistore environment it is often necessary to setup several virtual hosts whose configuration is essentially the same apart from one or two settings. Instead of copying and pasting each configuration twice (do not forget about the SSL) one can use a third party extension for Apache web server called mod_macro. This module allows you setting up templates (macros) of your configuration. Your actual vhosts entries will contain references to these templates providing values for variables you defined.
Installing mod_macro on a Ubuntu server is simple by running the apt-get utility:
sudo apt-get install libapache2-mod-macro
Alternatively, download the module and perform a direct installation as described here.
Sample usage
Consider you have to configure two shops running from the same installation. You want them to have two different top level domains (myserver.com and myserver.de) and keep logs separate.
Create a new virtual host configuration file under /etc/apache2/sites-available/ and create your template as follows:
<Macro MyTld $tld>
ServerAdmin admin@myserver.com
ServerName www.myserver.$tld
ServerAlias myserver.$tld
ErrorLog ${APACHE_LOG_DIR}/error_www.myserver.$tld.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access_www.myserver.$tld.log vhost_combined
DocumentRoot /var/www/www.myserver.com
<Directory /var/www/www.myserver.com>
SetEnv MAGE_RUN_CODE "$tld"
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</Macro>
Also set up an SSL macro:
<Macro MySSL>
SSLEngine on
SSLCertificateFile /etc/ssl/certs/www.myserver.com-cert.pem
SSLCertificateKeyFile /etc/ssl/private/www.myserver.com.pem
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
BrowserMatch "MSIE [2-6]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
# MSIE 7 and newer should be able to use keepalive
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
</Macro>
Finally add your virtual hosts:
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
Use MyTld com
</VirtualHost>
<VirtualHost *:443>
Use MyTld com
Use MySSL
</VirtualHost>
<VirtualHost *:80>
Use MyTld de
</VirtualHost>
<VirtualHost *:443>
Use MyTld de
Use MySSL
</VirtualHost>
Advantages of using mod_macro are self-evident:
- smaller configuration files to maintain
- less bugs due to copy-pastes only partially updated
- better readability
More information on mod_macro can be found here.