Automating Maintenance Periods with Apache HTTP Server Version 2.2 and PHP 5.3

Assumptions: In this article, I will be assuming that you have shell access to your web server, the Apache HTTP Server installed (httpd), the PHP 5.3 CLI installed with the cURL extension, the ability to restart httpd and are using Unix-based system.


In an earlier article, I described a general system for handling maintenance periods with Apache HTTP Server Version 2.2. In this article, I will describe a system for automating the code changes required.

The configuration settings developed in the previous article were as follows:

ErrorDocument 503 /errors/503.html
RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^x\.x\.x\.x$
RewriteCond %{REQUEST_URI} !^/errors/503.html$
RewriteCond %{REQUEST_URI} !^/phpMyAdmin
RewriteRule .* - [R=503]
Header always set Retry-After "3600"

In this version, we will split the settings up into two parts, one static and one dynamic. The first line, ErrorDocument 503 /errors/503.html is placed into the virtual host configuration file for the site. The rest of the code is automatically generated by a PHP script (enable_maintenance.php) intended to be run at the command line and is included into the primary Apache configuration file like so:

Include /srv/www/*

Automated Apache configuration file generation

The dynamic portion of the Apache configuration file is generated through the co-operation of two PHP scripts, and one local and one server-side.


This script is run locally on your development machine via the (e.g. php enable_maintance.php > maintenance_on.conf). It uses the remote script, myip.php to determine the public IP address of your development machine, and generates an Apache configuration file to suit.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ip_string = curl_exec($ch);

$ip = explode('.', $ip_string);

$output  = "RewriteEngine On\n";
$output .= "RewriteCond %{REMOTE_ADDR} !^" . $ip[0] . "\." . $ip[1] . "\." . $ip[2] . "\." . $ip[3] . "$\n";
$output .= "RewriteCond %{REQUEST_URI} !^/errors/503.html$\n";
$output .= "RewriteRule .* - [R=503]\n";
$output .= "Header always set Retry-After \"3600\"\n";



myip.php is placed on the web server and simply returns the IP address of the requesting client.


Normal Usage Process

  1. Execute php enable_maintance.php > maintenance_on.conf on the development machine.
  2. Upload maintenance_on.conf to /server/www/ on the server
  3. Reload the Apache httpd configuration files (e.g. sudo apache2ctl graceful)
  4. Make the changes that necessitated the maintenance mode invocation
  5. Delete /server/www/
  6. Reload the Apache httpd configuration files (e.g. sudo apache2ctl graceful)
  7. You’re done!

This article was written for the Apache HTTP Server Version 2.2 and PHP version 5.3. The guidance here may or may not work with earlier versions of the software.

Initial version: July 3rd, 2010. Corrected an insignificant typo on September 19th, 2010.