Solving Magento

Solutions for Magento E-Commerce Platform

by Oleg Ishenko

Using Macros for Apache Virtual Hosts Configuration for Magento Multistore Environment

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.

Readers who read this post also read these:

  • Magento Global Functions
    http://www.divisionlab.com/solvingmagento/n normally includes the names of a controller and its action (command). The path can be first checked for a URL-rewrite, for example, in web applications t...
  • Symbolic Links
    It is surprising how many people don't know about symbolic links and do not realize how useful they can be. For instance you are running a test server with multiple instances of out shop configured...
  • Magento Downloadable Product Type Tutorial
    In this tutorial I am going to demonstrate some of the functions I’ve talked about in my overview of the Downloadable product type. I will develop a simple module that will add a new feature to the...
  • Quickly Create Magento Module Structure with a Powershell Script
    Your work on a new module usually begins with a tedious task of creating the basic folder structure. You go to your app folder, cd to code, then to local, then to you working package directory. You...

Leave a Reply

Your email address will not be published. Required fields are marked *

Theme: Esquire by Matthew Buchanan.