Skip to content

Instantly share code, notes, and snippets.

@Justinzobel
Last active January 16, 2023 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Justinzobel/9d6f87312fc94a68584ffb17d526ba8d to your computer and use it in GitHub Desktop.
Save Justinzobel/9d6f87312fc94a68584ffb17d526ba8d to your computer and use it in GitHub Desktop.

Discourse Maintenance Page with Apache2

As root

cd /var/discourse
nano containers/app.yml

Comment out these lines:

  #- "templates/web.ssl.template.yml"
  #- "templates/web.letsencrypt.ssl.template.yml"

expose:
  #- "80:80"   # http
  #- "443:443" # https

Add at the END of templates section add (must be last):

  - "templates/web.socketed.template.yml"

Note: This will make Discourse listen only on internal IP and apache2 will take over the 80/443 ports and SSL termination

Note: Discourse must be rebuilt for this to take effect:

cd /var/discourse
./launcher rebuild app

Install apache2 and certbot

apt install -y apache2 certbot python3-certbot-apache

Make a directory for the html page:

mkdir /var/www/discourse_maintenance

HTML page: /var/www/discourse_maintenance/discourse_maintenance.html

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="refresh" content="5">
        <title>Discourse Maintenance</title>
        <style>
            .center {
                display: flex;
                justify-content: center;
            }
            .container {
                max-width: 500px;
                padding: 50px 50px 30px 50px;
            }
            .title {
                padding-top: 20px;
            }
            h1, p {
                font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
            }
        </style>
    </head>
    <body>
        <div class="center">
            <div class="container">
                <h1 class="title">Discourse Maintenance&hellip;</h1>
                <p>We are currently upgrading the site, or performing scheduled maintenance.</p>
                <p>You'll automatically be redirected to the site once it's available.</p>
            </div>
        </div>
    </body>
</html>

Enable Proxy Module:

a2enmod proxy
a2enmod proxy_http
a2enmod headers

Apache vhost file:

<IfModule mod_ssl.c>
<VirtualHost *:443>
  ServerName your.discourse.domain
  ServerAdmin your@email.com
  DocumentRoot /var/www/discourse_maintenance

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined

  # Maintenance Mode
  RewriteEngine On
  RewriteCond /var/www/under_maintenance -f
  # safety check to prevent redirect loops 
  RewriteCond %{REQUEST_URI} !/discourse_maintenance.html$
  # redirect internally all requests to maintenance.html 
  RewriteRule ^.*$ /var/www/discourse_maintenance/discourse_maintenance.html

  ProxyPass / unix:///var/discourse/shared/standalone/nginx.http.sock|http://127.0.0.1/
  ProxyPassReverse / unix:///var/discourse/shared/standalone/nginx.http.sock|http://127.0.0.1/

  SSLCertificateFile /etc/letsencrypt/live/your.discourse.domain/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/your.discourse.domain/privkey.pem
  Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

To enable maintenance run touch /var/www/under_maintenance

To disable maintenance run touch /var/www/under_maintenance

Credits: https://meta.discourse.org/t/add-an-offline-page-to-display-when-discourse-is-rebuilding-or-starting-up/45238 for the intial idea, html page (trimmed/edited to my liking) and nginx config from which I based the Apache config on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment