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.

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

discourse postgresql

If you don’t see any output from the above command, it’s probably 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

Output:

=* ruby-2.6.0 [ x86_64 ]

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

Step 3: Download and Configure Discourse

Clone the Discourse code repository from Github.

sudo apt install git

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.

cd /var/www/discourse/

git checkout v2.2.4

Install Ruby dependency manager.

~/.rvm/rubies/ruby-2.6.0/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 third 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 commands to prepare Discourse for production.

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

If you see the following error,

Uglifier::Error: Unexpected token: keyword (const). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true).

Simply run the command again and the error will disappear.

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

Discourse will be 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;

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.

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

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.

discourse create admin account from console

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.

restart discourse

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.0/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

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.0/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.

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 puma.rb configuration file to your home directory.

cp config/puma.rb ~

Then check out the latest stable version. For instance,

git checkout v2.3.2

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

If you see the following error, simply run the command again.

/var/www/discourse/vendor/bundle/ruby/2.6.0/bin/rake: No such file or directory - brotli

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

Finally, start Discourse.

sudo systemctl start discourse

Now you can check Discourse version from the admin dashboard.

upgrade discourse without docker

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: 3 Average: 5]

3 Responses to “Install Discourse Forum Software on Ubuntu 18.04 Without Docker

  • Brilliant article thanks for sharing

  • Dear,

    thanks for this great guide – but always an error at this step

    always this error:

    /var/www/discourse# RAILS_ENV=production bundle exec rake db:migrate

    rake aborted!
    NoMethodError: undefined method `keys’ for nil:NilClass
    /var/www/discourse/app/models/global_setting.rb:57:in `load_defaults’
    /var/www/discourse/config/application.rb:32:in `’
    /var/www/discourse/Rakefile:5:in `require’
    /var/www/discourse/Rakefile:5:in `’
    /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-12.3.2/exe/rake:27:in `’
    /var/www/discourse/vendor/bundle/ruby/2.6.0/bin/ruby_executable_hooks:24:in `eval’
    /var/www/discourse/vendor/bundle/ruby/2.6.0/bin/ruby_executable_hooks:24:in `’

    I hope you have an idea to fix it

    Thanks & regards,
    Mat

  • change 2.6.0 to 2.6.3

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • If my answer helped you, please consider supporting this site. Thanks :)