Install Discourse Forum Software on Ubuntu 18.04 Without Docker

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. (If your Ubuntu server doesn’t have the netstat command, you can run sudo apt install net-tools command to install it.)

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 Ruby on Ubuntu 18.04

Discourse requires Ruby 2.6 or up. However, Ubuntu 18.04 repository comes with Ruby 2.5.1. The latest version of Ruby is 2.7.1. However, I don’t recommend using the latest version, because it might not be compatible with Discourse. For best compatibility, I recommend installing Ruby 2.6 from a PPA.

sudo apt install software-properties-common

sudo apt-add-repository ppa:brightbox/ruby-ng

sudo apt install ruby2.6

To check your Ruby version number, run

ruby -v

Output:

ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]

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

Create the /var/www/ directory, if it’s not already created.

sudo mkdir /var/www/

Move the discourse 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.5.0.

cd /var/www/discourse/

git checkout v2.5.0

Install bundler: the Ruby dependency manager.

sudo /usr/bin/gem install bundler

Install the following packages to compile source code.

sudo apt-get install gcc build-essential ruby2.6-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 /usr/local/bin/bundle config set path '/var/www/discourse/vendor/bundle/'
RAILS_ENV=production /usr/local/bin/bundle install

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: Obtain a Free MaxMind Licence Key

Discourse comes with a built-in web analytics tool. If you want to know the geographic information of your visitors, you need a MaxMind licence key.

Create an account at MaxMind. Maxmind will send you an email. Click the link in the email to set a password, then log in to your MaxMind account.  Next, select My License Key on the left bar.

maxmind license key

Click the Generate New License Key button.

maxmind generate new license key

Give your license key a name. Then choose No, because we don’t need to use the geoipupdate program. Then click the Confirm button.

geoipupdate

Once the license key is created, copy the license key. Open the Discourse configuration file.

nano config/discourse.conf

Find the following line and add your license key here.

maxmind_license_key=

Save and close the file.

Step 5: Start Discourse

Install required packages.

sudo apt install redis-server optipng pngquant jhead jpegoptim gifsicle nodejs npm

sudo npm install -g svgo

Edit the production environment config file.

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

Add the following code as the fifth line.

require 'uglifier'

discourse production configuration

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 /usr/local/bin/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 contain 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 281) and comment it out.

brotli(path, max_compress)

Save and close the file. Next, run the following command to compile assets. This process can use lots of RAM like 1GB.

RAILS_ENV=production /usr/local/bin/bundle exec rake assets:precompile

Next, edit the puma.rb file

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

Find the following line.

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/ /var/www/discourse/tmp/pids/

Start Discourse.

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

Sample output:

[24161] Puma starting in cluster mode...
[24161] * Version 4.3.1 (ruby 2.6.6-p146), codename: Mysterious Traveller
[24161] * Min threads: 8, max threads: 32
[24161] * Environment: development
[24161] * Process workers: 4
[24161] * Preloading application
[24161] * Listening on unix:///var/www/discourse/tmp/sockets/puma.sock
[24161] ! WARNING: Detected 4 Thread(s) started in app boot:
[24161] ! #<Thread:[email protected]/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/atomic/ruby_thread_local_var.rb:38 sleep_forever> - /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/concurrent-ruby-1.1.6/lib/concurrent-ruby/concurrent/atomic/ruby_thread_local_var.rb:40:in `pop'
[24161] ! #<Thread:[email protected]/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/message_bus-2.2.3/lib/message_bus.rb:667 sleep> - /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/redis-4.1.3/lib/redis/connection/ruby.rb:68:in `select'
[24161] ! #<Thread:[email protected]/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/message_bus-2.2.3/lib/message_bus/timer_thread.rb:38 sleep> - /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/message_bus-2.2.3/lib/message_bus/timer_thread.rb:123:in `sleep'
[24161] ! #<Thread:[email protected]/discourse.rb:708 sleep> - lib/discourse.rb:711:in `sleep'
[24161] * Daemonizing...

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

Step 6: 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/nginx.http.sock;
  server unix:/var/www/discourse/tmp/sockets/nginx.https.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 7: 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.

install discourse ubuntu 18.04 without docker

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

install discourse forum on ubuntu 18.04

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;

discourse https upgrade insecure requests

Save and close the file. And reload Nginx.

sudo nginx -t
sudo systemctl reload nginx

Step 8: Create Admin Account

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

RAILS_ENV=production /usr/local/bin/bundle exec rake admin:create

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

discourse create admin account from console

After that, restart Discourse.

RAILS_ENV=production /usr/local/bin/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.

restart discourse

If you see the 502 bad gateway error, then the restart command wasn’t successful, you need to start Discourse with:

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

Once logged in, you can start the setup wizard. (https://community.example.com/wizard) and follow the instructions to finish the 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 9: 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 user 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=/usr/local/bin/bundle exec sidekiq -C /var/www/discourse/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 running.

sudo systemctl status discourse-sidekiq

Step 10: Create Systemd Service for Discourse

First, stop the current Discourse process with

cd /var/www/discourse/
RAILS_ENV=production /usr/local/bin/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=/usr/local/bin/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 running.

sudo systemctl status discourse

Step 11: 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. Note that I highly recommend running iRedMail mail server on a fresh clean OS. Installing iRedMail on an OS that has other web applications can fail, and likely break existing applications.

  • How to easily set up a full-featured mail server on Ubuntu 18.04 with iRedMail

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.

discourse send test email

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.

Unable to Send Email

If your Discourse instance doesn’t send emails and you see the following message on the Discourse web page,

All outgoing email has been globally disabled by an administrator. No email notifications of any kind will be sent.

you need to go to Settings -> Email (not Emails), set disable emails to no. Save the setting and restart Discourse.

sudo systemctl restart discourse discourse-sidekiq

If your Discourse still can’t send emails, check if it can ping the mail server.

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 ~
cp config/discourse.conf ~

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 /usr/local/bin/bundle install --path vendor/bundle/

Edit the assets.rake file.

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

Find the following line (line 273) and comment it out.

brotli(path, max_compress)

Save and close the file. Next, run the following commands to prepare for production.

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

Then you can check 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.

upgrade discourse without docker ubuntu

Discourse Theme Customization

If you don’t like the default white theme, you can customize it via Admin dashboard -> Customize -> Themes. There are 3 themes installed by default.

  • Shades of Blue
  • Dark
  • Light

I selected the Shades of Blue theme. You can also add customize CSS by clicking the Edit CSS/HTML button. Below are the CSS I added for my Discourse.

html {
    background-color:#ececec;
}

#main-outlet {
    background-color: #fff;
    padding-left: 20px;
}

.d-header {
    background-color:rgb(45, 45, 45);
}


a:visited {
    color: blueviolet;
}

a {
    color: blueviolet;
}

How to Move Discourse to A New Server

First, make a backup of your original Discourse forum, and download the tar.gz file. (Do not change the filename.)

discourse backup

Then you need to install Discourse on the new server by following step 1 to step 11. Next, upload the backup file to /var/www/discourse/public/backups/default/ directory on the new server.

Go to the /var/www/discourse/ directory on the new server.

cd /var/www/discourse/

Install dependencies.

sudo gem install thor

Go to Discourse Admin dashboard -> Settings -> Backups, tick on allow restore.

discourse restore from backup command line

Next, run the following command to restore the site from backup.

RAILS_ENV=production script/discourse restore file-name-of-the-backup-file.tar.gz

Once the restore is finished, restart Discourse.

sudo systemctl restart discourse discourse-sidekiq

Refresh the Discourse web page, and you will see your original Discourse forum back online.

If you see the following error,

You must use Bundler 2 or greater with this lockfile.

Then you need to update bundler.

gem install bundler

Update gemfile.lock

RAILS_ENV=production bundle update --bundler

You can also remove the Gemfile.lock file so you won’t see this error.

How to Uninstall Discourse

Remove PostgreSQL database server.

sudo apt remove postgresql

Remove the webroot 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

Monitoring the Health Your Discourse Instance

You should keep an eye on the discourse error log, which you can find via Admin dashboard -> Logs -> Error Logs. If your server gets a lot of traffic, you might need more RAM to run Discourse. The following log shows my server was out of memory.

discourse error log

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 🙂

Sidebar