Apache and Lighttpd server send error 503 when web site is under maintenance

When you are updating website you may need to send error 503 to client. Error 503 indicates that web server is temporary out of service or down. This is useful if you are running popular database driven website such as a forum or e-commerce site. So when the site is under maintenance you can send user a good message indicating that site is down for some work.

This tip will help you to disable a site for maintenance using mod_rewrite without redirecting url.

Both Lighttpd and Apache webserver allows you to send this message to client using server side rewriting using mod_rewrite and php.

Error 503 means

=> Server is too busy to serve your request
=> Server is slashdotted or dugg to death
=> Server is forced to send this message etc

But why to send error 503?

Error 503 informs search engine that site is temporary out of service. This is quite important for site which is heavily depends upon search engine for selling products and services.

PHP code – down.php

The down.php file simply sends a 503 HTTP error header indicating that service is unavailable. echo command sends a message for your site.

So first create a php file as follows:
<?
header("HTTP/1.1 503 Service Unavailable");
echo "Server is down for maintenance. We will be back in 60 minutes.";
?>

Task: Lighttpd send error 503 when web is under maintenance

Copy down.php to webserver documentation root:
cp down.php /home/lighttpd
OR
cp down.php /var/www

Open lighttpd configuration file
# vi /etc/lighttpd/lighttpd.conf
Now add following code:

$HTTP["url"] != "/down.php$" {
   url.rewrite = ( "" => "/down.php" )
}

Save and close the file.

It is also possible to block site for rest of the world except for website developer or site admin IP address:

$HTTP["remoteip"] != "201.203.149.149" {
                   url.rewrite = ( "" => "/down.php" )
}

Restart lighttpd:
# /etc/init.d/lighttpd restart

Task: Apache send error 503 when web is under maintenance

You can use an Apache mod_rewrite module. Open .htaccess file
vi .htaccess
Append following code:
RewriteEngine on
RewriteRule !down.php$ /down.php [L]

Again it is possible to block site for rest of the world except for website developer or site admin IP address:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^201\.203\.149\.149$
RewriteRule !down.php$ /down.php [L]

Save and close file. Restart Apache 2 web server:
# /etc/init.d/httpd restart
OR
# /etc/init.d/apache2 restart

Let us say user hits url http://domain.com/dir/somefile.html
First rules will check for client IP address – 201.203.149.149. If match found user is allowed to browse site. Otherwise user will get 503 error with message:

Server is down for maintenance. We will be back in 60 minutes. 
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
14 comments… add one
  • Steve Mar 29, 2012 @ 19:50

    Weird thing. I only see the echo message once. The next time I visit the site there’s not echo message just a header message? Any thoughts as to why this would happen. It’s returning a 503 message every time, it’s just not returning the echo’d message.

    Thanks!

  • Preet Feb 7, 2012 @ 7:35

    The "something" can be ascecsed very easily with PHP. If you have a web page address that looks like this:index.php? month=september&name=smith&age=24Then you can access those values in PHP like this:<?phpecho $_GET('month');echo $_GET('name');echo $_GET('age');?>This will print septembersmith24.As for accessing a text file, that's easy too:<?php$text = file_get_contents( 'textfile.txt' );echo $text;?>This will read the entire contents of the file textfile.txt into the variable $text, and then print it out for you.You can also access many databases with PHP, I highly recommend you use MySQL. It's a bit too complicated to describe here, though. There are some good references made to good books by folks above. The "Teach Yourself" series is great.

  • Max Jan 18, 2012 @ 17:04

    Good idea! In this case, we simply took the site down for a few hours so a 503 probably wasn’t necessary. Great suggestion for longer cases though.

    Thanks!

  • Anonymous Oct 25, 2008 @ 18:11

    Wow, thanks alot! I’ve been wondering how to do this for ages!

  • Frederick Jul 25, 2008 @ 20:31

    A better .htaccess rule might be:


    RewriteEngine On
    RewriteCond %{REQUEST_URI} !down.php$
    RewriteRule ^(.*)$ /down.php [L]

  • 🐧 nixCraft Jun 15, 2007 @ 10:35

    Do you have other rewrite rules? if so add this rule before those rules

  • Carl Jun 14, 2007 @ 13:58

    I have tried to use your method, but only the main page and a page one level deeper redirect successfully. If I go any deeper down a valid URL it does not display the site down page.

  • Sagri May 11, 2007 @ 10:39

    Hi,
    Thats very efficient way of costimising error pages.

    Do you know the way to do the same on tomcat alone with out web server. ie; with out apache.

    I use only tomcat 5.5.12 and I want to customise my 503 pages when I am updating.

    Thanks in advance.

  • sangamnath Mar 30, 2007 @ 11:05

    Hi, i am getting the error 503 when i am importing the data to the client side.i am really not understanding wats the reason behind this.So please can any one help me out to solve this problem.Thanks in advance….

  • 🐧 nixCraft Jan 6, 2007 @ 11:13

    Jerry,

    No .htaccess rule is correct. If file is not down.php it will display content of down.php for all site urls and sub directories.

    Hope this helps.

  • Jerry Jan 6, 2007 @ 10:50

    I’m just learning to hack .htaccess and use mod_rewrite. It looks to me like this line:

    RewriteRule !down.php$ /down.php [L]

    only catches URLs to the root directory? How can I redirect all URLs, even URLs to subdirectories deeper in the site, so all URLs get a 503 maintenance page?

    Thanks.

  • Daniel Jan 1, 2007 @ 14:49

    Do you need to restart Apache after modifying .htaccess? I think not. I you were changing httpd.conf I would agree.

  • 🐧 nixCraft Dec 30, 2006 @ 1:22

    Good idea; For lighttpd
    lighttpd -t -f /etc/lighttpd.conf && /etc/init.d/lighttpd restart

    Appreciate your post.

  • Lars Noodén Dec 29, 2006 @ 12:26

    Good tips, but if the site is really important then the step on Restarting Apache 2 web server after saving and closing the file needs to verify that the syntax of the configuration file is correct. There are many ways mistakes or typos can be introduced, any of which will bring the server down.

    # /etc/init.d/httpd restart
    or
    # /etc/init.d/apache2 restart

    One should do a configuration test first. e.g.

    # /etc/init.d/apache2 configtest &&
    # /etc/init.d/apache2 reload

    or
    /usr/sbin/apache2ctl -t -f /etc/apache2.conf &&
    /usr/sbin/apache2ctl -f /etc/apach2.conf graceful

Leave a Reply

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

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.