Custom error pages in Apache keep “something went wrong” from looking like “the server fell down the stairs.” A branded 404 with a search box or a home link reduces dead-ends, and a calm 500 page prevents a transient outage from turning into a support-ticket stampede.

When a request fails, Apache can swap the default error response with a custom document via the ErrorDocument directive. Each mapping targets a local URL-path (internal redirect), an external URL, or a short inline string, and a local URL-path preserves the original HTTP status code while serving the custom body.

Local error documents must remain reachable inside the same site context, or error handling can recurse into more errors; the safest pattern is a simple static HTML page under the site’s DocumentRoot with minimal dependencies. The commands and paths below assume a Debian or Ubuntu layout (/etc/apache2/, apache2ctl, apache2), while RHEL-style installs commonly use /etc/httpd/ and httpd.

Steps to configure custom error pages in Apache:

  1. Open a terminal with sudo privileges.
  2. List enabled VirtualHost definitions to locate the active site configuration.
    $ sudo apache2ctl -S
    VirtualHost configuration:
    *:80                   is a NameVirtualHost
             default server mysite.example (/etc/apache2/sites-enabled/000-mysite.conf:1)
             port 80 namevhost mysite.example (/etc/apache2/sites-enabled/000-mysite.conf:1)
    ##### snipped #####

    On Debian and Ubuntu, entries under /etc/apache2/sites-enabled/ typically point to files in /etc/apache2/sites-available/.

  3. Create the error document directory at /var/www/html/errors/.
    $ sudo install -d -m 0755 /var/www/html/errors

    Match the filesystem path to the site DocumentRoot in the relevant VirtualHost.

  4. Create a custom 404 page at /var/www/html/errors/404.html.
    $ sudo tee /var/www/html/errors/404.html >/dev/null <<'EOF'
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>404 - Not Found</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
      <h1>404 - Not Found</h1>
      <p>The requested URL was not found on this server.</p>
      <p><a href="/">Back to home</a></p>
    </body>
    </html>
    EOF
  5. Create a custom 500 page at /var/www/html/errors/500.html.
    $ sudo tee /var/www/html/errors/500.html >/dev/null <<'EOF'
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>500 - Server Error</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
      <h1>500 - Server Error</h1>
      <p>A server error occurred while processing the request.</p>
      <p><a href="/">Back to home</a></p>
    </body>
    </html>
    EOF
  6. Open the target site configuration file.
    $ sudo vi /etc/apache2/sites-available/000-mysite.conf
  7. Add ErrorDocument directives inside the relevant VirtualHost block.
    <VirtualHost *:80>
      ServerName mysite.example
      DocumentRoot /var/www/html
    
      ErrorDocument 404 /errors/404.html
      ErrorDocument 500 /errors/500.html
    </VirtualHost>

    The second argument to ErrorDocument is a URL-path (like /errors/404.html), not a filesystem path (like /var/www/html/errors/404.html), and an error document that triggers the same error can cause recursion.

  8. Save the configuration changes.
  9. Exit the editor.
  10. Check the configuration syntax.
    $ sudo apache2ctl configtest
    Syntax OK

    Syntax OK confirms that Apache parsed the configuration without errors.

  11. Reload the apache2 service.
    $ sudo systemctl reload apache2

    Use sudo systemctl restart apache2 when a reload fails or a full restart is required.

  12. Request a missing URL to confirm the custom 404 page is served with status 404.
    $ curl -i -H 'Host: mysite.example' http://127.0.0.1/no-such-page
    HTTP/1.1 404 Not Found
    Content-Type: text/html; charset=utf-8
    ##### snipped #####
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>404 - Not Found</title>
    ##### snipped #####

    Replace the Host value with the site ServerName when multiple VirtualHost entries share the same IP and port.

Discuss the article:

Comment anonymously. Login not required.