Question

Custom error pages on Ubuntu 20.4 Apache

Posted November 13, 2021 136 views
ApacheUbuntu 20.04HTML

Hi. I have a HTML website deployed using apache2 on a ubuntu 20.4 droplet. I have a top-level .htaccess file, but I am still getting the default apache error pages. Any ideas?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @jdogcoder,

  • where is your DocumentRoot?
  • full Apache configuration file or vHost?
  • .htaccess file? (you can change sensible information if present…)
  • apache error log file content?
  • do you want to show a custom error pages?

in my experience, if you are using “/home/<user>/” as DocumentRoot, this is a headache…

I hope to hear from you soon …

A big hug,

HR ;)

  • Thank you for the response @hejeroaz! I will try and answer your clarifying questions to the best of my ability.

    • Document Root: I am assuming you are talking about where my idex.html file is? If so, /var/www/mydomain/index.html
    • My full config file. I am assuming you want the one for my domain, not apache in general. (Let me know if I am wrong, I am happy to provide the other one also.)
    <VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName  *mydomain*.com
        ServerAlias www.*mydomain*.com
        DocumentRoot /var/www/*mydomain*.com
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    </VirtualHost>
    
    • Here is my .htaccess file.
    Options +FollowSymLinks
    
    <FilesMatch "\.(jpg|png|css|js)$">
    Header set Cache-Control "max-age=86400"
    </FilesMatch>
    
    ErrorDocument 400 /errors/400.html
    
    ErrorDocument 401 /errors/401.html
    
    ErrorDocument 403 /errors/403.html
    
    ErrorDocument 404 /errors/404.html
    
    ErrorDocument 500 /errors/500.html
    
    <Files .htaccess>
    order allow,deny
    deny from all
    </Files>
    
    
    • Hi @jdogcoder,

      all seems good…

      do you have the headers module enabled?

      /var/www/*mydomain*com/.htaccess: Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration
      

      what about ls -la /etc/apache2/mods-enabled and ls -la /etc/apache2/mods-available

      you can check

      for details, but in short do a2enmod headers to enable mod headers, restart Apache and give it a new try…

      please, keep me in touch ;)

      • headers are enabled, and It is still not working

        • Hi @jdogcoder,

          I did some tests....

          • create new droplet

          Update server:

          apt update && apt dist-upgrade -y && sleep 10 && reboot
          apt install apache2 -y 
          

          Apache2 Ubuntu Default Page - OK

          Enable headers

          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:~# a2enmod headers
          
          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:~# systemctl restart apache2
          
          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:~# ls -la /etc/apache2/mods-enabled/
          total 8
          drwxr-xr-x 2 root root 4096 Nov 15 16:03 .
          drwxr-xr-x 8 root root 4096 Nov 15 16:01 ..
          lrwxrwxrwx 1 root root   36 Nov 15 16:01 access_compat.load -> ../mods-available/access_compat.load
          lrwxrwxrwx 1 root root   28 Nov 15 16:01 alias.conf -> ../mods-available/alias.conf
          lrwxrwxrwx 1 root root   28 Nov 15 16:01 alias.load -> ../mods-available/alias.load
          lrwxrwxrwx 1 root root   33 Nov 15 16:01 auth_basic.load -> ../mods-available/auth_basic.load
          lrwxrwxrwx 1 root root   33 Nov 15 16:01 authn_core.load -> ../mods-available/authn_core.load
          lrwxrwxrwx 1 root root   33 Nov 15 16:01 authn_file.load -> ../mods-available/authn_file.load
          lrwxrwxrwx 1 root root   33 Nov 15 16:01 authz_core.load -> ../mods-available/authz_core.load
          lrwxrwxrwx 1 root root   33 Nov 15 16:01 authz_host.load -> ../mods-available/authz_host.load
          lrwxrwxrwx 1 root root   33 Nov 15 16:01 authz_user.load -> ../mods-available/authz_user.load
          lrwxrwxrwx 1 root root   32 Nov 15 16:01 autoindex.conf -> ../mods-available/autoindex.conf
          lrwxrwxrwx 1 root root   32 Nov 15 16:01 autoindex.load -> ../mods-available/autoindex.load
          lrwxrwxrwx 1 root root   30 Nov 15 16:01 deflate.conf -> ../mods-available/deflate.conf
          lrwxrwxrwx 1 root root   30 Nov 15 16:01 deflate.load -> ../mods-available/deflate.load
          lrwxrwxrwx 1 root root   26 Nov 15 16:01 dir.conf -> ../mods-available/dir.conf
          lrwxrwxrwx 1 root root   26 Nov 15 16:01 dir.load -> ../mods-available/dir.load
          lrwxrwxrwx 1 root root   26 Nov 15 16:01 env.load -> ../mods-available/env.load
          lrwxrwxrwx 1 root root   29 Nov 15 16:01 filter.load -> ../mods-available/filter.load
          lrwxrwxrwx 1 root root   30 Nov 15 16:03 headers.load -> ../mods-available/headers.load
          lrwxrwxrwx 1 root root   27 Nov 15 16:01 mime.conf -> ../mods-available/mime.conf
          lrwxrwxrwx 1 root root   27 Nov 15 16:01 mime.load -> ../mods-available/mime.load
          lrwxrwxrwx 1 root root   32 Nov 15 16:01 mpm_event.conf -> ../mods-available/mpm_event.conf
          lrwxrwxrwx 1 root root   32 Nov 15 16:01 mpm_event.load -> ../mods-available/mpm_event.load
          lrwxrwxrwx 1 root root   34 Nov 15 16:01 negotiation.conf -> ../mods-available/negotiation.conf
          lrwxrwxrwx 1 root root   34 Nov 15 16:01 negotiation.load -> ../mods-available/negotiation.load
          lrwxrwxrwx 1 root root   33 Nov 15 16:01 reqtimeout.conf -> ../mods-available/reqtimeout.conf
          lrwxrwxrwx 1 root root   33 Nov 15 16:01 reqtimeout.load -> ../mods-available/reqtimeout.load
          lrwxrwxrwx 1 root root   31 Nov 15 16:01 setenvif.conf -> ../mods-available/setenvif.conf
          lrwxrwxrwx 1 root root   31 Nov 15 16:01 setenvif.load -> ../mods-available/setenvif.load
          lrwxrwxrwx 1 root root   29 Nov 15 16:01 status.conf -> ../mods-available/status.conf
          lrwxrwxrwx 1 root root   29 Nov 15 16:01 status.load -> ../mods-available/status.load
          

          Configure temporal domainname

          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:/etc/apache2/sites-available# nano 000-default.conf
          <VirtualHost *:80>
            ServerName test-dok.hermesrodriguez.io
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
          </VirtualHost>
          
          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:/etc/apache2/sites-available# systemctl restart apache2
          

          Create fake custom error pages

          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:~# cd /var/www/html/
          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:/var/www/html# mkdir errors
          
          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:/var/www/html# echo "Custom error page" > errors/400.html
          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:/var/www/html# echo "Custom error page" > errors/401.html
          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:/var/www/html# echo "Custom error page" > errors/403.html
          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:/var/www/html# echo "Custom error page" > errors/404.html
          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:/var/www/html# echo "Custom error page" > errors/500.html
          

          Add .htaccess file

          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:/var/www/html# nano .htaccess
          Options +FollowSymLinks
          
          <FilesMatch "\.(jpg|png|css|js)$">
            Header set Cache-Control "max-age=86400"
          </FilesMatch>
          
          ErrorDocument 400 /errors/400.html
          ErrorDocument 401 /errors/401.html
          ErrorDocument 403 /errors/403.html
          ErrorDocument 404 /errors/404.html
          ErrorDocument 500 /errors/500.html
          
          <Files .htaccess>
            order allow,deny
            deny from all
          </Files>
          
          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:/var/www/html# systemctl restart apache2
          

          Allow Apache override my .htaccess definitions file

          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:/var/www/html# nano /etc/apache2/apache2.conf
          [...]
          <Directory /var/www/>
                  Options Indexes FollowSymLinks
                  # AllowOverride None
                  AllowOverride All
                  Require all granted
          </Directory>
          [...]
          
          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:/var/www/html# systemctl restart apache2
          

          Check for errors…

          root@ubuntu-s-1vcpu-1gb-intel-nyc1-01:/var/www/html# tail -f /var/log/apache2/error.log
          [Mon Nov 15 16:03:45.575328 2021] [core:notice] [pid 1983:tid 139800305867840] AH00094: Command line: '/usr/sbin/apache2'
          [Mon Nov 15 16:08:19.541713 2021] [mpm_event:notice] [pid 1983:tid 139800305867840] AH00491: caught SIGTERM, shutting down
          [Mon Nov 15 16:08:19.642236 2021] [mpm_event:notice] [pid 2100:tid 139946680355904] AH00489: Apache/2.4.41 (Ubuntu) configured -- resuming normal operations
          [Mon Nov 15 16:08:19.642373 2021] [core:notice] [pid 2100:tid 139946680355904] AH00094: Command line: '/usr/sbin/apache2'
          [Mon Nov 15 16:12:17.522519 2021] [mpm_event:notice] [pid 2100:tid 139946680355904] AH00491: caught SIGTERM, shutting down
          [Mon Nov 15 16:12:17.602729 2021] [mpm_event:notice] [pid 2204:tid 140627472669760] AH00489: Apache/2.4.41 (Ubuntu) configured -- resuming normal operations
          [Mon Nov 15 16:12:17.602876 2021] [core:notice] [pid 2204:tid 140627472669760] AH00094: Command line: '/usr/sbin/apache2'
          [Mon Nov 15 16:16:32.184766 2021] [mpm_event:notice] [pid 2204:tid 140627472669760] AH00491: caught SIGTERM, shutting down
          [Mon Nov 15 16:16:32.271276 2021] [mpm_event:notice] [pid 2316:tid 140404413201472] AH00489: Apache/2.4.41 (Ubuntu) configured -- resuming normal operations
          [Mon Nov 15 16:16:32.271386 2021] [core:notice] [pid 2316:tid 140404413201472] AH00094: Command line: '/usr/sbin/apache2'
          ^C
          

          You can test for a few days in

          The headers are being added

          % curl -vis http://test-dok.hermesrodriguez.io/test.jpg
          *   Trying 159.223.174.132:80...
          * Connected to test-dok.hermesrodriguez.io (159.223.174.132) port 80 (#0)
          > GET /test.jpg HTTP/1.1
          > Host: test-dok.hermesrodriguez.io
          > User-Agent: curl/7.77.0
          > Accept: */*
          >
          * Mark bundle as not supporting multiuse
          < HTTP/1.1 200 OK
          HTTP/1.1 200 OK
          < Date: Mon, 15 Nov 2021 16:33:52 GMT
          Date: Mon, 15 Nov 2021 16:33:52 GMT
          < Server: Apache/2.4.41 (Ubuntu)
          Server: Apache/2.4.41 (Ubuntu)
          < Last-Modified: Wed, 18 Nov 2020 05:17:03 GMT
          Last-Modified: Wed, 18 Nov 2020 05:17:03 GMT
          < ETag: "6f58-5b45ab8bcf1c0"
          ETag: "6f58-5b45ab8bcf1c0"
          < Accept-Ranges: bytes
          Accept-Ranges: bytes
          < Content-Length: 28504
          Content-Length: 28504
          < Cache-Control: max-age=86400
          Cache-Control: max-age=86400     <==== our custom setting
          < Content-Type: image/jpeg
          Content-Type: image/jpeg
          
          <
          * Failure writing output to destination
          * Closing connection 0
          

          Do not hesitate to write if you think I can help you

          ;)

          • I get the errors

            jasper@ubuntu-s-1vcpu-1gb-nyc1-01:~$ sudo tail -f /var/log/apache2/error.log
            [Mon Nov 15 16:56:23.504161 2021] [mpm_event:notice] [pid 1773:tid 139621933808704] AH00491: caught SIGTERM, shutting down
            [Mon Nov 15 16:56:41.897886 2021] [mpm_event:notice] [pid 808:tid 140629782494272] AH00489: Apache/2.4.41 (Ubuntu) OpenSSL/1.1.1f configured -- resuming normal operations
            [Mon Nov 15 16:56:41.920157 2021] [core:notice] [pid 808:tid 140629782494272] AH00094: Command line: '/usr/sbin/apache2'
            [Mon Nov 15 16:57:07.435414 2021] [mpm_event:notice] [pid 808:tid 140629782494272] AH00491: caught SIGTERM, shutting down
            [Mon Nov 15 16:57:07.574212 2021] [mpm_event:notice] [pid 1175:tid 140055764651072] AH00489: Apache/2.4.41 (Ubuntu) OpenSSL/1.1.1f configured -- resuming normal operations
            [Mon Nov 15 16:57:07.574350 2021] [core:notice] [pid 1175:tid 140055764651072] AH00094: Command line: '/usr/sbin/apache2'
            [Mon Nov 15 19:58:44.404731 2021] [core:error] [pid 1176:tid 140055477655296] [client 45.146.164.110:52758] AH00126: Invalid URI in request POST /cgi-bin/.%2e/.%2e/.%2e/.%2e/bin/sh HTTP/1.1
            [Mon Nov 15 22:52:56.535657 2021] [mpm_event:notice] [pid 1175:tid 140055764651072] AH00491: caught SIGTERM, shutting down
            [Mon Nov 15 22:52:56.670686 2021] [mpm_event:notice] [pid 2814:tid 139800081841216] AH00489: Apache/2.4.41 (Ubuntu) OpenSSL/1.1.1f configured -- resuming normal operations
            [Mon Nov 15 22:52:56.670812 2021] [core:notice] [pid 2814:tid 139800081841216] AH00094: Command line: '/usr/sbin/apache2'
            
          • Now I see something different, just not what it is supposed to be.

          • Hi @jdogcoder,

            [Mon Nov 15 19:58:44.404731 2021] [core:error] [pid 1176:tid 140055477655296] [client 45.146.164.110:52758] AH00126: Invalid URI in request POST /cgi-bin/.%2e/.%2e/.%2e/.%2e/bin/sh HTTP/1.1
            

            some bad body is trying to break your apache....

            this is normal that happens …

            did you solve the original problem ??

          • Hey @hejeroaz, I still am not getting the intended error pages. It looks a bit different though. (see https://jaspermayone.com/thiswillerror)

          • You need to do a clean installation and test again… in any case, I can give you a hand by remote screen session and help you to solve this problem… but my English is not so good (I’m Spanish)…

            it’s seems that your .htaccess isn’t be loaded…

            did you do this ?

            [...]
            <Directory /var/www/>
                    Options Indexes FollowSymLinks
                    # allow your .htaccess override all apache options
                    AllowOverride All
                    Require all granted
            </Directory>
            [...]
            

            last steps:

            • check ALL Apache config and logs files
            • do a fresh apache re-installation

            any other idea?

          • I will try a clean installation first.