Sling Academy
Home/DevOps/Apache: How to Show a Maintenance Page on Demand

Apache: How to Show a Maintenance Page on Demand

Last updated: January 22, 2024

Introduction

The ability to display a maintenance page is essential for website administration. Whether you are updating your site, fixing bugs, or making other changes that require your site to be temporarily unavailable, a maintenance page informs your visitors about the ongoing work and that you’ll be back online shortly. In this tutorial, we will explore how you can use Apache’s powerful capabilities to show a maintenance page on demand.

Prerequisites

  • Access to an Apache web server
  • Basic understanding of server management and Apache configuration files
  • FTP or SSH access to your server for editing configuration files and uploading the maintenance page.

Step-by-Step Guide

Step 1: Create a Maintenance Page

Begin by creating a simple HTML file called maintenance.html and design it to convey the necessary message to your visitors.

<html>
  <head>
    <title>Maintenance in Progress</title>
  </head>
  <body>
    <h1>We'll be back soon!</h1>
    <p>Sorry for the inconvenience but we’re performing some maintenance at the moment. We’ll be back up shortly!</p>
  </body>
</html>

Upload this file to a directory on your Apache server that is not publicly accessible to avoid accidental navigation to it.

Step 2: Configure Apache to Serve the Maintenance Page

The .htaccess file can be used to redirect users to the maintenance page. The following code sets up a temporary redirect for all visitors:

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123
RewriteCond %{REQUEST_URI} !/maintenance.html$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /maintenance.html [R=503,L]

Replace 123\.123\.123\.123 with your IP address to ensure you can still access the site.

This tells Apache to redirect all traffic to the maintenance.html page if the visiting IP address doesn’t match the one specified (which should be your own for you to continue seeing the website normally). The third line ensures that images on your maintenance page are not redirected, preventing broken image links.

Step 3: Set a Retry-After HTTP Header

Inform search engines and users when they can expect the site to be back by setting a Retry-After header:



  Header set Retry-After "Sun, 28 May 2023 16:00:00 GMT"

This should be added to your .htaccess file below the rewrite rules. The exact time and date should be the anticipated end of your maintenance period.

Step 4: Handle Ongoing Apache Web Server Requests

If you have long-running scripts or sessions that you’d prefer not to terminate, you can use mod_proxy or script-specific configurations to handle that. For Laravel applications, for instance, you may want to use the down Artisan command instead.

Step 5: Test

After making all the changes, it is crucial to test your configuration. Try accessing your site from multiple devices and networks, if possible, to ensure the maintenance page is being served properly.

Returning to Normal Operation

When you are ready to bring your site back online, simply reverse the changes made in the .htaccess file.

Conclusion

Serving a maintenance page is a courteous and professional way to handle downtime. Apache provides flexible options for website administrators to swiftly switch to and from a maintenance mode.

Remember that although the techniques discussed are reliable, always make a backup of your .htaccess and other configuration files before making any changes.

For more comprehensive changes, consider strategies such as auto-scaling, load balancing or a hosted solution like Amazon Web Services or Google Cloud, which offer more advanced options for minimising downtime during maintenance operations.

With careful planning and the right configuration, your maintenance periods can be smooth, and your users will appreciate the transparency.

Next Article: Apache mod_status module: Monitor web server and current connections

Previous Article: Apache: How to Display a Custom 404 Not Found Page

Series: Apache Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide