Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Enabling Maintenance Site In Apache

There are many ways to put a site into "maintenance mode", but I wanted a solution that wouldn't require the original site to be "working". E.g. sites like wordpress and phpBB have a maintenance mode, but this will only work if those sites still exist on the server and aren't fundamentally broken. Thus my solution outlined below is to have a separate site that is ready to go in the background, that we tell apache to switch to serving up when we want to go into "maintenance mode".

Steps

Create a folder on the web server to represent the maintenance site. E.g.

mkdir /var/www/maintenance

Download the maintenance.html and .htaccess files from my Github maintenance repo and rename them index.html and .htaccess in that folder (in the future, there may be different single-page maintenance pages to choose from).

Add Apache Configurations

Create the apache configurations for the site:

sudo vim /etc/apache/sites-available/maintenance.conf
<VirtualHost *:80>

    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/maintenance
    <Directory /var/www/maintenance/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        allow from all
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Now create the SSL configuration for the site (you will need to tweak the certificate paths).

sudo vim /etc/apache2/sites-available/maintenance-ssl.conf
<VirtualHost *:443>

    SSLEngine on
    SSLCertificateFile /path/to/cert.pem
    SSLCertificateKeyFile /path/to/private.pem
    SSLCertificateChainFile /path/to/chain.pem

    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/maintenance
    <Directory /var/www/maintenance/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        allow from all
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Switching Out

Now you have your site ready and waiting to be switched. You can switch-over to serving up the maintenance site by running:

sudo a2ensite maintenance
sudo a2ensite maintenance-ssl
sudo a2dissite 000-default
sudo a2dissite default-ssl
sudo service apache2 reload

... and then you can switch back out of maintenance mode by running:

sudo a2ensite 000-default
sudo a2ensite default-ssl
sudo a2dissite maintenance
sudo a2dissite maintenance-ssl
sudo service apache2 reload

I would put each of these sets of commands into two different BASH scripts for enabling and disabling maintenance mode. a2ensite creates the symlink in sites-enabled that points to the configuration in sites-available, which "enables it" when you reload apache. a2dissite removes that symlink.

Last updated: 27th June 2020
First published: 27th June 2020