Apache: enable maintenance mode across all virtual hosts

ec30 picture ec30 · Feb 11, 2014 · Viewed 12.8k times · Source

I have a server running Apache with several VirtualHosts e.g. domain1.example1.com domain2.example1.com etc. All these webapps are related. When the app is updated; I want to enable maintenance mode in apache for all VirtualHosts at the same time, redirecting to a centralized maintenance page. Right now I have in one of my Virtual Host configurations:

<VirtualHost *>
  ServerAdmin [email protected]
  DocumentRoot /var/www/website
  ServerName  website.com
  ServerAlias www.website.com      

  RewriteEngine On
  RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
  RewriteCond %{REQUEST_FILENAME} !/maintenance.html
  RewriteRule ^.*$    /maintenance.html [L]
</VirtualHost>

Ideally I want to enable the mode by renaming one file somewhere on the server e.g.

mv /app/system/maintenance.disabled /app/system/maintenance.html

Is something like this possible or can it only be configured for each VirtualHost seperately?

Answer

Welsh picture Welsh · Feb 12, 2014

Create a file called maintenance.conf with the following in it:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{REQUEST_FILENAME} !/maintenance.html
RewriteRule ^.*$    /maintenance.html [L]

Then setup your vhosts like:

<VirtualHost *>
  ServerAdmin [email protected]
  DocumentRoot /var/www/website
  ServerName  website.com
  ServerAlias www.website.com      

  Include conf/maintenance.conf
</VirtualHost>

Then when you want to enable maintenance mode, just uncomment those lines in maintenance.conf and restart apache.

Alternatively you could make maintenace.conf.disabled and maintenace.conf.enabled. Then create a symbolic link for maintenance.conf to point to the enabled or disabled version of the file and bounce apache.

You could even wrap doing that in a script. One called enabled-maintence-mode.sh and disable-maintenance-mode.sh that would remove and create the symbolic links respectively and restart apache.