This tutorial is going to show you how to install Discourse on Ubuntu 18.04 server. Created by StackExchange founder Jeff Atwood, Discourse is an open source Internet forum (aka online message board) and mailing list management software, with the aim of revolutionizing forum discussion. It’s written with Ember.js and Ruby on Rails, using PostgreSQL as the back-end database management system.

Features of Discourse

  • Infinite scrolling. There’s no next page in a thread. Just scroll down to read more.
  • live updates, drag and drop attachments.
  • Forum threads can be ranked by popularity.
  • The “best of thread” view can show the best reply to a particular thread.
  • The ability to remember where you were reading in a thread.
  • Expanding URLs to provide a summary of the URL.
  • Users can reply via email.
  • The flagging system automatically hides inappropriate posts until they can be reviewed by a staff member.
  • Moderators can split, merge, local or archive any topic.
  • Based on the level of trust, a user can be promoted as moderator, or demoted as trolls, bad actors or spammers to keep the forum civilized. Builtin Akismet spam protection and heuristics including new user sandboxing, user flag blocking, and standard nofollow.
  • A badge system can show what a user has achieved.
  • Mobile-friendly, responsive web design. Users can read or post from laptop, tablet and phone.
  • 100% free open source. No paid commercial version with better or more complete features.
  • Single sign-on. Seamlessly integrate Discourse with your existing site’s login system.
  • Social login. Easily add common social logins like Google, Facebook, Twitter, etc.
  • iOS and Android app available.
  • Available in more than 30 languages.
  • Two-factor authentication to improve account security.
  • And many more.

Prerequisites of Installing Discourse on Ubuntu 18.04 Without Docker

The official method of installing Discourse is with Docker, which is great for those who want to get an application up and running quickly. But Docker is also resource hungry. Just think about it: If you have already got some components like PostgreSQL database server up and running, the Docker method will still run a separate PostgreSQL database inside the container, which is a waste of server resources. You need a 2GB RAM server to run Discourse with Docker. I’m going to show you how to run Discourse on a 1GB RAM server without docker.

To run Discourse, you need a server with at least 1GB RAM. You can click this referral link to create an account at Vultr to get $50 free credit (for new users only). Once you have an account at Vultr, install Ubuntu 18.04 on your server and follow the instructions below.

You also need a domain name. I registered my domain name at NameCheap because the price is low and they give whois privacy protection free for life.

Notice: I installed Discourse with a sudo user on Ubuntu 18.04. For best results, you should also follow this tutorial with a sudo user, not root.

To add a sudo user, simply run

sudo adduser username
sudo adduser username sudo

Then switch to the new user.

su - username

Step 1: Configure PostgreSQL Database Server

Log into your server via SSH, then run the following command to install PostgreSQL from the default Ubuntu software repository.

sudo apt install postgresql

PostgreSQL database server will automatically start and listens on 127.0.0.1:5432, as can be seen with the following command.

sudo netstat -lnpt | grep postgres

If you don’t see any output from the above command, it’s probably because PostgreSQL server isn’t running. You can start PostgreSQL server by issuing the following command.

sudo systemctl start postgresql

The postgres user will be created on the OS during the installation process. It’s the super user for PostgreSQL database server. By default, this user has no password and there’s no need to set one because you can use sudo to switch to the postgres user and log into PostgreSQL console.

sudo -u postgres psql

Create a database for Discourse.

CREATE DATABASE discourse;

Create a database user.

CREATE USER discourse_user;

Set a password for this user.

ALTER USER discourse_user WITH ENCRYPTED PASSWORD 'your_preferred_password';

Set this user as the owner of discourse database.

ALTER DATABASE discourse OWNER TO discourse_user;

Connect to the discourse database.

c discourse;

Create the hstore and pg_trgm extension.

CREATE EXTENSION hstore;

CREATE EXTENSION pg_trgm;

Log out from the PostgreSQL console.

q

Step 2: Install Latest Version of Ruby on Ubuntu 18.04

Discourse requires Ruby 2.5.2 or up. However, Ubuntu 18.04 comes with Ruby 2.5.1. We can install the latest version of Ruby via the Ruby version manager (RVM). First, install GNU privacy guard.

sudo apt install gnupg2

Import the public of Ruby version manager.

gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

Then install RVM with default Ruby and Rails.

curl -sSL https://get.rvm.io | bash -s stable --rails

Once the installation is complete, run the following command to source RVM scripts.

source ~/.rvm/scripts/rvm

List the available Ruby versions installed by RVM.

rvm list

Sample Output:

=* ruby-2.6.3 [ x86_64 ]

# => - current
# =* - current && default
# * - default

As you can see, ruby 2.6.3 is installed on my server.

Step 3: Download and Configure Discourse

Install the git tool.

sudo apt install git

Assuming you are at your home directory, run the following command to clone the Discourse code repository from Github.

git clone https://github.com/discourse/discourse.git

Move the directory to /var/www/.

sudo mv discourse/ /var/www/

Change directory and use the latest stable release of Discourse. You can go to the Github releases page to see the latest stable version. I’m now using v2.3.4.

cd /var/www/discourse/

git checkout v2.3.4

Install bundler: the Ruby dependency manager.

~/.rvm/rubies/ruby-2.6.3/bin/gem install bundler -v '1.17.3'

Install the following packages to compile source code.

sudo apt-get install gcc ruby-dev libxslt-dev libxml2-dev zlib1g-dev libpq-dev imagemagick

Then install dependencies of Discourse. This process could use a lot of RAM.

RAILS_ENV=production bundle install --path vendor/bundle/

Copy the default configuration file to a new file.

cp config/discourse_defaults.conf config/discourse.conf

Edit the new file.

nano config/discourse.conf

Configure the database connection.

# host address for db server
# This is set to blank so it tries to use sockets first
db_host = localhost

# port running db server, no need to set it
db_port = 5432

# database name running discourse
db_name = discourse

# username accessing database
db_username = discourse_user

# password used to access the db
db_password = your_password

Change the domain name used with your Discourse forum.

# hostname running the forum
hostname = "community.example.com"

Save and close the file.

Step 4: Start Discourse

Install required packages.

sudo apt install redis-server optipng pngquant jhead jpegoptim gifsicle

Edit the production environment config file.

nano /var/www/discourse/config/environments/production.rb

Add the following code as the fifth line.

require 'uglifier'

Then find the following line.

config.assets.js_compressor = :uglifier

Replace it with:

config.assets.js_compressor = Uglifier.new(harmony: true)

Save and close the file. Then run the following command to initialize the database. If you see any errors during this step, simply run the command again.

RAILS_ENV=production bundle exec rake db:migrate

Next, we are going to compile static assets such as JavaScript, but before doing that, we need to edit a file.

nano /var/www/discourse/lib/tasks/assets.rake

We need to find the lines that contains brotli and comment them out to disable Brotili compression, because the JavaScript files will be compressed with Gzip. If Gzip and Brotili compression are both enabled, there will be some annoying errors when we compile the assets. Find the following line (line 269) and comment it out.

brotli(path, max_compress)

Save and close the file. Next, run the following command to compile assets.

RAILS_ENV=production bundle exec rake assets:precompile

Next, edit the puma.rb file

nano /var/www/discourse/config/puma.rb

Find the following lines.

APP_ROOT = '/home/discourse/discourse'

Change the application path to

APP_ROOT = '/var/www/discourse'

Save and close the file. Then create the sockets and process ID directory.

mkdir /var/www/discourse/tmp/sockets/
mkdir /var/www/discourse/tmp/pids/

Start Discourse.

RAILS_ENV=production bundle exec puma -C /var/www/discourse/config/puma.rb

Sample output:

[28389] Puma starting in cluster mode...
[28389] * Version 3.12.1 (ruby 2.6.3-p62), codename: Llamas in Pajamas
[28389] * Min threads: 8, max threads: 32
[28389] * Environment: development
[28389] * Process workers: 4
[28389] * Preloading application
[28389] * Listening on unix:///var/www/discourse/tmp/sockets/puma.sock
[28389] ! WARNING: Detected 3 Thread(s) started in app boot:
[28389] ! #[email protected]/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/message_bus-2.2.0/lib/message_bus.rb:667 sleep> - /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/hiredis-0.6.3/lib/hiredis/ext/connection.rb:19:in `read'
[28389] ! #[email protected]/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/message_bus-2.2.0/lib/message_bus/timer_thread.rb:38 sleep> - /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/message_bus-2.2.0/lib/message_bus/timer_thread.rb:123:in `sleep'
[28389] ! #[email protected]/var/www/discourse/lib/discourse.rb:616 sleep> - /var/www/discourse/lib/discourse.rb:619:in `sleep'
[28389] * Daemonizing...

Discourse is listening on Unix socket: /var/www/discourse/tmp/sockets/puma.sock.

Step 5: Configure Nginx Reverse Proxy

Install Nginx web server from the default Ubuntu 18.04 software repository.

sudo apt install nginx

Copy the sample Nginx virtual host configuration file.

sudo cp /var/www/discourse/config/nginx.sample.conf /etc/nginx/conf.d/discourse.conf

Edit the new file.

sudo nano /etc/nginx/conf.d/discourse.conf

Find the following lines and comment them out because we are going to use puma.

upstream discourse {
    server unix:/var/www/discourse/tmp/sockets/thin.0.sock;
    server unix:/var/www/discourse/tmp/sockets/thin.1.sock;
    server unix:/var/www/discourse/tmp/sockets/thin.2.sock;
    server unix:/var/www/discourse/tmp/sockets/thin.3.sock;
}

Find the following lines and uncomment them.

# upstream discourse {
#       server unix:/var/www/discourse/tmp/sockets/puma.sock;
# }

Find the following line.

server_name enter.your.web.hostname.here;

Change the server name. Don’t forget to add DNS A record for the domain name.

server_name community.example.com;

Nginx by default doesn’t support Brotli compression, so comment out the following line.

brotli_static on;

Save and close the file. Create the cache directory.

sudo mkdir -p /var/nginx/cache/

Then test nginx configuration.

sudo nginx -t

If the test is successful, reload Nginx for the changes to take effect.

sudo systemctl reload nginx

Now you should be able to see the Discourse forum at http://community.example.com.

Step 6: Enable HTTPS

To encrypt HTTP traffic, we can enable HTTPS by installing a free TLS certificate issued from Let’s Encrypt. Run the following command to install Let’s Encrypt client (certbot) on Ubuntu 18.04 server.

sudo apt install certbot python3-certbot-nginx

Next, run the following command to obtain and install TLS certificate.

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d community.example.com

Where

  • --nginx: Use the nginx plugin.
  • --agree-tos: Agree to terms of service.
  • --redirect: Force HTTPS by 301 redirect.
  • --hsts: Add the Strict-Transport-Security header to every HTTP response. Forcing browser to always use TLS for the domain. Defends against SSL/TLS Stripping.
  • --staple-ocsp: Enables OCSP Stapling. A valid OCSP response is stapled to the certificate that the server offers during TLS.

The certificate should now be obtained and automatically installed.

And you can access Discourse forum via HTTPS (https://community.example.com).

If Firefox shows a yellow triangle in the browser address bar, that’s because some images are still served on HTTP protocol. To solve this problem, you can edit the Discourse Nginx configuration file.

sudo nano /etc/nginx/conf.d/discourse.conf

Add the following line in the SSL server block to upgrade insecure requests.

add_header Content-Security-Policy upgrade-insecure-requests;

Save and close the file. And reload Nginx.

sudo nginx -t
sudo systemctl reload nginx

Step 7: Create Admin Account

Go to the discourse directory (/var/www/discourse/) and run the following command to create an admin account.

RAILS_ENV=production bundle exec rake admin:create

You will be asked to enter an email address and password for the admin account.

After that, restart Discourse.

RAILS_ENV=production bundle exec pumactl -P /var/www/discourse/tmp/pids/puma.pid restart

Now refresh the Discourse web page and you will be able to login.

Once logged in, you can start the setup wizard. (https://community.example.com/wizard) and follow the instructions to finish installation. If you use Cloudflare CDN, then you need to go to settings -> security -> content security policy src and add this URL: https://community.example.com/cdn-cgi/

Step 8: Configure background Processing Service: Sidekiq

Sidekiq is an open source job scheduler. Many tasks, like sending emails, are executed asynchronously by sidekiq. Edit the sidekiq.yml file.

nano /var/www/discourse/config/sidekiq.yml

Add the following lines to the end of the file. This configuration is suitable for a Discourse forum with low user activity and RAM. If there are lots of users activities, consider doubling the concurrency and number of queues.

production:
  :concurrency: 2
  :queues:
    - [critical, 4]
    - [default, 2]
    - [low]
    - [ultra_low]

Save and close the file. Then create a Systemd service for sidekiq.

sudo nano /etc/systemd/system/discourse-sidekiq.service

Add the following lines in the file. Replace username with your real username.

[Unit]
Description=Discourse sidekiq background processing service
After=multi-user.target

[Service]
Type=simple
User=username
PIDFile=/var/www/discourse/tmp/pids/sidekiq.pid
WorkingDirectory=/var/www/discourse
Environment=RAILS_ENV=production
ExecStart=/home/username/.rvm/gems/ruby-2.6.3/wrappers/bundle exec sidekiq -C config/sidekiq.yml
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

Save and close the file. Then start and enable this service.

sudo systemctl start discourse-sidekiq
sudo systemctl enable discourse-sidekiq

Check the status. Make sure it’s runing.

sudo systemctl status discourse-sidekiq

Step 9: Create Systemd Service for Discourse

First, stop the current Discourse process with

cd /var/www/discourse/
RAILS_ENV=production bundle exec pumactl -P /var/www/discourse/tmp/pids/puma.pid stop

Then edit the puma.rb file.

nano /var/www/discourse/config/puma.rb

Comment out the following two lines (Add the # symbol at the beginning of each line) because Systemd will handle process ID and daemonization.

pidfile "#{APP_ROOT}/tmp/pids/puma.pid"

daemonize true

Next, create a Systemd service for Discourse.

sudo nano /etc/systemd/system/discourse.service

Add the following lines in the file.  Replace username with your real username.

[Unit]
Description=Discourse service

[Service]
Type=simple
User=username
PIDFile=/var/www/discourse/tmp/pids/puma.pid
WorkingDirectory=/var/www/discourse
Environment=RAILS_ENV=production
ExecStart=/home/username/.rvm/gems/ruby-2.6.3/wrappers/bundle exec puma -C config/puma.rb
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

Save and close the file. Then start and enable this service.

sudo systemctl start discourse
sudo systemctl enable discourse

Check the status. Make sure it’s runing.

sudo systemctl status discourse

Step 10: Configure SMTP

Discourse needs to send emails so that visitors can register account on your forum and receive notifications. To edit SMTP settings, open the discourse.conf file.

nano /var/www/discourse/config/discourse.conf

You can find the following lines to configure SMTP server. Normally you would want to use 587 as the SMTP port.

# address of smtp server used to send emails
smtp_address =
# port of smtp server used to send emails
smtp_port = 25

# domain passed to smtp server
smtp_domain =

# username for smtp server
smtp_user_name =

# password for smtp server
smtp_password =

# smtp authentication mechanism
smtp_authentication = plain

# enable TLS encryption for smtp connections
smtp_enable_start_tls = true

You may also want to add the From: address in this file like below.

# From: address
notification_email = [email protected]

For how to set up your own mail server, please check out the following tutorial:

If you prefer to use a third party SMTP relay service, then I recommend Mailjet, which allows you to send 6000 emails per month for free.

After saving the SMTP settings, restart Discourse service.

sudo systemctl restart discourse

Then you can test email sending in your Discourse admin dashboard. You can go to mail-tester.com, which will give you a unique email address. Send a test email from your Discourse to this email address to know your sender score.

Optimize RAM Usage

The default puma configuration makes Discourse use a lot of RAM. By default, my Discourse uses 4 workers, 8 minimal threads, 32 maximal threads. If your RAM isn’t enough, the redis server will be stopped. To reduce RAM usage, you can decrease the number of workers and threads in puma.rb file.

nano /var/www/discourse/config/puma.rb

Find the following two lines.

workers "#{num_workers}"
threads 8, 32

You can change the values like below, which tells puma to use 2 workers, 4 minimal threads and 16 maximal threads. This setting is suitable for servers with only 1GB RAM.

workers 2
threads 4, 16

Save and close the file. Then restart the Discourse service.

sudo systemctl restart discourse

Upgrade Discourse

You can subscribe to the RSS feed of Discourse releases to stay informed with the latest version. Before upgrading Discourse, I strongly recommend doing a manual database backup in the Discourse admin dashboard and download it to your hard disk.

Note: The one-click browser upgrade (https://community.yourdomain.com/admin/upgrade) doesn’t work if you installed Discourse without Docker. You need to follow the instructions below.

To upgrade Discourse, first stop the service.

sudo systemctl stop discourse

Go to the Discourse installation directory.

cd /var/www/discourse/

Get new tags from the Github repository.

git fetch --tags

Before checking out the latest stable version, I recommend backing up the configuration file to your home directory.

cp config/puma.rb ~
cp config/environments/production.rb ~
cp config/sidekiq.yml ~

Then delete the Gemfile.lock file.

rm Gemfile.lock

And check out the latest stable version. For instance,

git checkout v2.3.4

Install dependencies for the new Discourse version. This process could use a lot of RAM.

RAILS_ENV=production bundle install --path vendor/bundle/

Prepare for production.

RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake assets:precompile

Next, you can see if the new puma.rb configuration file has added some new lines, compared to the original file. If there’s nothing new, then you can simply replace the file with the original one.

mv ~/puma.rb /var/www/discourse/config/puma.rb
mv ~/production.rb /var/www/discourse/config/environments/production.rb
mv ~/sidekiq.yml /var/www/discourse/config/sidekiq.yml

Finally, start Discourse.

sudo systemctl start discourse

Now you can check Discourse version from the admin dashboard.

How to Uninstall Discourse

Remove PostgreSQL database server.

sudo apt remove postgresql

Remove the web root directory.

sudo rm /var/www/discourse/ -rf

Remove the Nginx config file.

sudo rm /etc/nginx/conf.d/discourse.conf

Remove Let’s Encrypt SSL certificate.

sudo certbot revoke --cert-name community.example.com

Remove SystemD service.

sudo rm /etc/systemd/system/discourse.service
sudo rm /etc/systemd/system/discourse-sidekiq.service

Conclusion

I hope this article helped you install Discourse forum software on Ubuntu 18.04 without using Docker. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂

Rate this tutorial

[Total: 6 Average: 5]