Sep 15, 2018

Setting up Discourse on Laravel Forge

My server is manager with Laravel Forge, mostly because I found that setting up new project with SSL takes a lot of time. Plus, I never know if I "did it right". Forge is great if you use PHP, typically Laravel, WordPress or Symfony, but with something else, it can be tricky.

I recently wanted to install Ghost 2.0, but I gave up rapidly, just reading the install guide.

The same was about to happen with Discourse, but this time I decided to dive into it. This guide will help you setting up Discourse, next to your other project with Laravel Forge.

Downtime required
Installing Discourse will require you to turn off Nginx, and all other websites hosted on your server will be down during this time.

Getting started

1. Setup your DNS

Before we start, make sure the domain you want to use for Discourse is pointing to your server. I recommend doing this first, because it might take a lot of time.

2. Install Docker

Discourse ships as a Docker container, which really helps installing it next to other websites. Imagine if it required another version of Nginx or Redis than what you use!

I followed this step-by-step guide: How To Install and Use Docker on Ubuntu 16.04

3. Stop Nginx

In order to install Discourse, you need to be able to access the docker container from the outside. When you launch the build, it will fail because your 80 port is already in-use. There is probably a way to use another port instead but I couldn't be bothered.

There is probably a way to use another port instead but I couldn't be bothered: I stopped Nginx and shut down all my other websites for a few hours. It wasn't a big deal for me.

1sudo service nginx stop

4. Install Discourse

To install Discourse, I created a Mailgun account because the install process requires SMTP information. Then, I followed this guide: How To Install Discourse on Ubuntu 16.04

Setting up Nginx

5. Add a new website on Forge

Now, it's time to head to your Laravel Forge account and add a new website. Select a static HTML website config, we will modify that later.

Laravel Forge configuration for Discourse

6. Setup SSL certificate

One of the main reason I'm happily paying for Forge is that I hate SSL. The following steps assume you're using Let's Encrypt from Forge. If you don't, you may have to change some config lines in the Nginx config.

7. Update Nginx config

Instead of serving the default folder, we want to redirect the traffic to Discourse's Docker container. I have modified the configuration to remove all PHP references and use Discourse. Make sure Nginx is reloaded and running after your modifications.

1# FORGE CONFIG (DO NOT REMOVE!)
2include forge-conf/forum.allraces.org/before/*;
3 
4server {
5 listen 443 ssl http2;
6 listen [::]:443 ssl http2;
7 server_name forum.allraces.org;
8 root /home/forge/forum.allraces.org/; # /!\ UPDATE WITH YOUR DOMAIN
9 
10 # FORGE SSL (DO NOT REMOVE!)
11 ssl_certificate /etc/nginx/ssl/forum.allraces.org/412071/server.crt;
12 ssl_certificate_key /etc/nginx/ssl/forum.allraces.org/412071/server.key;
13 
14 ssl_protocols TLSv1.2;
15 ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
16 ssl_prefer_server_ciphers on;
17 ssl_dhparam /etc/nginx/dhparams.pem;
18 
19 add_header X-Frame-Options "SAMEORIGIN";
20 add_header X-XSS-Protection "1; mode=block";
21 add_header X-Content-Type-Options "nosniff";
22 
23- index index.html index.htm index.php;
24 
25 charset utf-8;
26 
27 # FORGE CONFIG (DO NOT REMOVE!)
28 include forge-conf/forum.allraces.org/server/*;
29 
30- location / {
31- try_files $uri $uri/ /index.php?$query_string;
32- }
33 
34 location = /favicon.ico { access_log off; log_not_found off; }
35 location = /robots.txt { access_log off; log_not_found off; }
36 
37 access_log off;
38 error_log /var/log/nginx/forum.allraces.org-error.log error;
39 
40- error_page 404 /index.php;
41 
42- location ~ \.php$ {
43- fastcgi_split_path_info ^(.+\.php)(/.+)$;
44- fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
45- fastcgi_index index.php;
46- include fastcgi_params;
47- }
48+ location / {
49+ proxy_pass http://unix:/var/discourse/shared/standalone/nginx.http.sock:;
50+ proxy_set_header Host $http_host;
51+ proxy_http_version 1.1;
52+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
53+ proxy_set_header X-Forwarded-Proto https;
54+ proxy_set_header X-Real-IP $remote_addr;
55+ }
56 
57 location ~ /\.(?!well-known).* {
58 deny all;
59 }
60}
61 
62# FORGE CONFIG (DO NOT REMOVE!)
63include forge-conf/forum.allraces.org/after/*;

Tweaking Discourse

8. Update the Discourse container

This is the toughest part, based on this guide.

Note that I provide my entire config file, yours might be a little different but if you focus on the diff part, it should be the same.

1## this is the all-in-one, standalone Discourse Docker container template
2##
3## After making changes to this file, you MUST rebuild
4## /var/discourse/launcher rebuild app
5##
6## BE *VERY* CAREFUL WHEN EDITING!
7## YAML FILES ARE SUPER SUPER SENSITIVE TO MISTAKES IN WHITESPACE OR ALIGNMENT!
8## visit http://www.yamllint.com/ to validate this file as needed
9 
10templates:
11 - "templates/postgres.template.yml"
12 - "templates/redis.template.yml"
13 - "templates/web.template.yml"
14 - "templates/web.ratelimited.template.yml"
15## Uncomment these two lines if you wish to add Lets Encrypt (https)
16 #- "templates/web.ssl.template.yml"
17 #- "templates/web.letsencrypt.ssl.template.yml"
18+ - "templates/web.socketed.template.yml"
19 
20## which TCP/IP ports should this container expose?
21## If you want Discourse to share a port with another webserver like Apache or nginx,
22## see https://meta.discourse.org/t/17247 for details
23- expose:
24- - "80:80" # http
25- - "443:443" # https
26 
27params:
28 db_default_text_search_config: "pg_catalog.english"
29 
30 ## Set db_shared_buffers to a max of 25% of the total memory.
31 ## will be set automatically by bootstrap based on detected RAM, or you can override
32 db_shared_buffers: "256MB"
33 
34 ## can improve sorting performance, but adds memory usage per-connection
35 #db_work_mem: "40MB"
36 
37 ## Which Git revision should this container use? (default: tests-passed)
38 #version: tests-passed
39 
40env:
41 LANG: en_US.UTF-8
42 # DISCOURSE_DEFAULT_LOCALE: en
43 
44 ## How many concurrent web requests are supported? Depends on memory and CPU cores.
45 ## will be set automatically by bootstrap based on detected CPUs, or you can override
46 UNICORN_WORKERS: 4
47 
48 ## TODO: The domain name this Discourse instance will respond to
49 ## Required. Discourse will not work with a bare IP number.
50 DISCOURSE_HOSTNAME: forum.allraces.org
51 
52 ## Uncomment if you want the container to be started with the same
53 ## hostname (-h option) as specified above (default "$hostname-$config")
54 #DOCKER_USE_HOSTNAME: true
55 
56 ## TODO: List of comma delimited emails that will be made admin and developer
57 ## on initial signup example '[email protected],[email protected]'
58 DISCOURSE_DEVELOPER_EMAILS: '*****@gmail.com'
59 
60 ## TODO: The SMTP mail server used to validate new accounts and send notifications
61 # SMTP ADDRESS, username, and password are required
62 # WARNING the char '#' in SMTP password can cause problems!
63 DISCOURSE_SMTP_ADDRESS: smtp.mailgun.org
64 DISCOURSE_SMTP_PORT: 587
65 DISCOURSE_SMTP_USER_NAME: [email protected]
66 DISCOURSE_SMTP_PASSWORD: ************
67 #DISCOURSE_SMTP_ENABLE_START_TLS: true # (optional, default true)
68 
69 ## If you added the Lets Encrypt template, uncomment below to get a free SSL certificate
70 #LETSENCRYPT_ACCOUNT_EMAIL: [email protected]
71 
72 ## The CDN address for this Discourse instance (configured to pull)
73 ## see https://meta.discourse.org/t/14857 for details
74 #DISCOURSE_CDN_URL: //discourse-cdn.example.com
75 
76## The Docker container is stateless; all data is stored in /shared
77volumes:
78 - volume:
79 host: /var/discourse/shared/standalone
80 guest: /shared
81 - volume:
82 host: /var/discourse/shared/standalone/log/var-log
83 guest: /var/log
84 
85## Plugins go here
86## see https://meta.discourse.org/t/19157 for details
87hooks:
88 after_code:
89 - exec:
90 cd: $home/plugins
91 cmd:
92 - git clone https://github.com/discourse/docker_manager.git
93 
94## Any custom commands to run after building
95run:
96 - exec: echo "Beginning of custom commands"
97 ## If you want to set the 'From' email address for your first registration, uncomment and change:
98 ## After getting the first signup email, re-comment the line. It only needs to run once.
99 #- exec: rails r "SiteSetting.notification_email='[email protected]'"
100 - exec: echo "End of custom commands"

9. Rebuild Discourse

Grab a coffee, this step is really long! As long as the first time you installed Discourse I guess, but it felt much longer.

1sudo -s
2/var/discourse/launcher rebuild app

You're done!

Laravel Forge configuration for Discourse