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

last updated in Categories Apache, CentOS, Debian Linux, Howto, lighttpd, RedHat/Fedora Linux, Suse Linux, Sys admin, Tip of the day, Tips, Ubuntu Linux

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
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"] != "" {
                   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
# /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 – 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. 


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14 comment

  1. 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
    # /etc/init.d/apache2 restart

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

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

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

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

    Appreciate your post.

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

  4. 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?


  5. 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.

  6. 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….

  7. 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.

  8. 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.

  9. A better .htaccess rule might be:

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

  10. 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.


  11. 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.

  12. 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.


    Have a question? Post it on our forum!