Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-56041

Cleanup custom 404 page and more easily support custom 50x error pages

XMLWordPrintable

    • MOODLE_310_STABLE
    • MDL-56041-custom-error-pages
    • Hide

      Setup

      1) Install heartbeat for simulating error pages:

      https://github.com/catalyst/moodle-tool_heartbeat
      From your site wwwroot, run:

      cd admin/tool
      git clone https://github.com/catalyst/moodle-tool_heartbeat heartbeat

      Then, visit site admin and install the tool.

      2) Configure your webserver to use the Moodle error page for both 403 and 404 errors:

      https://docs.moodle.org/dev/Error_pages#404_File_not_found_-_Web_server_errors

      For Apache:
      Edit /etc/apache2/conf-available/localized-error-pages.conf
      Add these lines, replacing SITEPATH, and save the file:

      ErrorDocument 403 /SITEPATH/error/index.php
      ErrorDocument 404 /SITEPATH/error/index.php
      

      Where SITEPATH would be stable_master for a site located at localhost/stable_master, so that line might look like:
      "ErrorDocument 404 /stable_master/error/index.php" (You need the leading slash here)

      3) Configure the web server to disable directory listing:
      For apache:
      Edit the file /etc/apache2/apache2.conf
      Locate the section corresponding to your web root:

      <Directory /var/www/>
              Options Indexes FollowSymLinks
              ...
      </Directory>

      and change the Options line, removing 'Indexes', i.e.:

      <Directory /var/www/>
              Options FollowSymLinks
              ...
      </Directory>

      Save the file.

      4) purge caches and restart your webserver

      php admin/cli/purge_caches.php
      sudo service apache2 restart

       

      Moodle themed error pages

      5) Visit a url which doesn't exist and confirm you get a Moodle themed error page

      /doesnotexist.html

      6) Visit a url which is forbidden and confirm you get a Moodle themed error page

      /lib/classes/

       

      Default static plain error pages

      7) Make sure the file: {$CFG->dataroot}/climaintenance.template.html does NOT exist

      8) Visit: /admin/tool/heartbeat/errors/bootstrap-late.php and confirm you get a nice but plain error page

      9) Turn on CLI maintenance mode and confirm you get a similar nice but plain error page when you visit the site.

      php admin/cli/maintenance.php --enable

       Now, disable maintenance mode:

      php admin/cli/maintenance.php --disable 

       

      Custom static themed error pages 

      10) Edit /error/plainpage.php and some random customization like making the background style blue. You may need to restart your web server

      11) Repeat steps 7 & 8 and confirm you now get custom 50x pages.

       

      Test messaging to support user

      12) Make sure mailcatcher/mailhog is set up and running to catch emails

      13) Make sure $CFG->supportemail is set (set this to something like test@example.com). You can find this in the site admin settings.

      14) Revisit SITEPATH/doesnotexist.html as a logged in user

      15) Confirm you don't see a message form by default

      16) Set the support user to a real user in config.php

      $CFG->supportuserid = 2;

      17) Reload and confirm you see the message form

      18) Add a message and send it

      19) Confirm the support user (the admin) received the email by checking mailhog/catcher

      Show
      Setup 1) Install heartbeat for simulating error pages: https://github.com/catalyst/moodle-tool_heartbeat From your site wwwroot, run: cd admin/tool git clone https: //github.com/catalyst/moodle-tool_heartbeat heartbeat Then, visit site admin and install the tool. 2) Configure your webserver to use the Moodle error page for both 403 and 404 errors: https://docs.moodle.org/dev/Error_pages#404_File_not_found_-_Web_server_errors For Apache: Edit /etc/apache2/conf-available/localized-error-pages.conf Add these lines, replacing SITEPATH, and save the file: ErrorDocument 403 /SITEPATH/error/index.php ErrorDocument 404 /SITEPATH/error/index.php Where SITEPATH would be stable_master for a site located at localhost/stable_master, so that line might look like: "ErrorDocument 404 /stable_master/error/index.php" (You need the leading slash here) 3) Configure the web server to disable directory listing: For apache: Edit the file /etc/apache2/apache2.conf Locate the section corresponding to your web root: <Directory /var/www/> Options Indexes FollowSymLinks ... </Directory> and change the Options line, removing 'Indexes', i.e.: <Directory /var/www/> Options FollowSymLinks ... </Directory> Save the file. 4) purge caches and restart your webserver php admin/cli/purge_caches.php sudo service apache2 restart   Moodle themed error pages 5) Visit a url which doesn't exist and confirm you get a Moodle themed error page /doesnotexist.html 6) Visit a url which is forbidden and confirm you get a Moodle themed error page /lib/classes/   Default static plain error pages 7) Make sure the file: {$CFG->dataroot}/climaintenance.template.html does NOT exist 8) Visit: /admin/tool/heartbeat/errors/bootstrap-late.php and confirm you get a nice but plain error page 9) Turn on CLI maintenance mode and confirm you get a similar nice but plain error page when you visit the site. php admin/cli/maintenance.php --enable  Now, disable maintenance mode: php admin/cli/maintenance.php --disable   Custom static themed error pages   10) Edit /error/plainpage.php and some random customization like making the background style blue. You may need to restart your web server 11) Repeat steps 7 & 8 and confirm you now get custom 50x pages.   Test messaging to support user 12) Make sure mailcatcher/mailhog is set up and running to catch emails 13) Make sure $CFG->supportemail is set (set this to something like test@example.com). You can find this in the site admin settings. 14) Revisit SITEPATH/doesnotexist.html as a logged in user 15) Confirm you don't see a message form by default 16) Set the support user to a real user in config.php $CFG->supportuserid = 2; 17) Reload and confirm you see the message form 18) Add a message and send it 19) Confirm the support user (the admin) received the email by checking mailhog/catcher

      This is a follow on issue from MDL-55916, but it also touches on a bunch of other issues. The status code that moodle returns for lots of pages aren't great, and the pages which are served under these conditions aren't great either. These could all be logged individually but I think are worth sorting holistically together.

      So current problems:

      1. If a url is valid, but the page throws an exception, then this is served as a 404 when it should be a 500 (or maybe 503)
      2. But if I go to a page for a record which doesn't exist, using MUST_EXIST, this throws an exception, but should be a 404 (and currently is a 404)
      3. If a url is valid, and you are logged in, but don't have a capability, eg /cohort/index.php?contextid=3 as a student then I get a 404 but I should get a 403 Forbidden instead
      4. If I go to /admin/index.php as a student then I get a 200 page, I should get a 403
      5. If I go to a page which isn't a moodle script I get a 404 from apache / nginx which is probably different to the 404 that moodle would give me
      6. If I am using the clean urls plugin / rewriting api, and I have a url which isn't routable, I should also serve a 404 (working fine) but I should also get the same 404 error page as the other two 404 use cases above
      7. There is /error/index.php but it's not very good, ie even if you set it up, all of the moodle 404's above don't use this page, only the apache 404's
      8. If I put the site into cli maintenance mode I get a vanilla unthemed 503 page, you can create a template file but it's static html, not php
      9. If I put the site into 'gui' maintenance mode, it doesn't use the template above!
      10. It's hard for a moodle to customize any of this

      So what I'm proposing is a common page layout / api / files / renderers, similar to what I think the /error/ page was trying to achieve but just done properly. Importantly they would be designed to be called from either apache or from within moodle.

      I haven't thought it through completely, but off the top off my head I think we want a renderer / template for each type of error 403, 404 etc, a theme layout common to all errors, and fixing all of the above to send the correct headers and then defer to the right page. And also a set of instructions for setup with apache / nginx etc

      Related wiki pages:

      https://docs.moodle.org/38/en/Linking_Moodle_and_Docs#Removing_Moodle_Docs_links_for_teachers

       

       New wiki page:

       https://docs.moodle.org/dev/Error_pages

        1. MDL-56041.jpg
          MDL-56041.jpg
          32 kB
        2. MDL-56041 (2).jpg
          MDL-56041 (2).jpg
          61 kB
        3. MDL-56041 (3).jpg
          MDL-56041 (3).jpg
          40 kB

            brendanheywood Brendan Heywood
            brendanheywood Brendan Heywood
            Peter Burnett Peter Burnett
            Jake Dallimore Jake Dallimore
            Anna Carissa Sadia Anna Carissa Sadia
            Votes:
            4 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 5 hours, 6 minutes
                5h 6m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.