Install Discourse Forum Software on Debian 11 Without Docker

This tutorial is going to show you how to install Discourse on Debian 11 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.

Why Install Discourse on Debian 11 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 server 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.

Prerequisites

To run Discourse, you need a server with at least 1GB RAM. If you are looking for a virtual private server (VPS), I recommend Kamatera VPS, which features:

  • 30 days free trial.
  • Starts at $4/month (1GB RAM)
  • High-performance KVM-based VPS
  • 9 data centers around the world, including United States, Canada, UK, Germany, The Netherlands, Hong Kong, and Isreal.

Follow the tutorial linked below to create your Linux VPS server at Kamatera.

Once you have a VPS running Debian 11, 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 Debian 11. 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 commands to install the latest version of PostgreSQL from the upstream repository. The latest version provides the best performance.

echo "deb [signed-by=/etc/apt/keyrings/postgresql.asc] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list

sudo mkdir -p /etc/apt/keyrings

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo tee /etc/apt/keyrings/postgresql.asc

sudo apt update

sudo apt install -y postgresql postgresql-contrib

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

sudo ss -lnpt | grep postgres

discouse postgresql debian

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

Check the status.

sudo systemctl status [email protected]

Sample output:

 [email protected] - PostgreSQL Cluster 14-main
     Loaded: loaded (/lib/systemd/system/[email protected]; enabled-runtime; vendor preset: enabled)
     Active: active (running) since Wed 2022-06-01 08:03:16 EDT; 3 days ago
   Main PID: 548 (postgres)
      Tasks: 8 (limit: 2340)
     Memory: 28.7M
        CPU: 4s

The postgres user will be created on the OS during the installation process. It’s the superuser for PostgreSQL database server. Run the following command to 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 Debian 11

Discourse requires Ruby 2.7 or higher, which is included in Debian 11 repository, so run the following command to install it.

sudo apt install ruby ruby-dev

To check your Ruby version number, run

ruby -v

Output:

ruby 2.7.4p191 (2021-07-07 revision a21a3b7d23) [x86_64-linux-gnu]

Step 3: Download and Configure Discourse

Install the git tool.

sudo apt install git

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

sudo mkdir -p /var/www/

Make www-data user as the owner of this directory.

sudo chown www-data:www-data /var/www/ -R

Clone the Discourse code repository from Github.

sudo -u www-data git clone https://github.com/discourse/discourse.git

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.8.3.

cd /var/www/discourse/

sudo -u www-data git checkout v2.8.3

Install bundler: the Ruby dependency manager.

sudo /usr/bin/gem install bundler

Install the following packages to compile source code.

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

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

sudo -u www-data RAILS_ENV=production /usr/local/bin/bundle config set path '/var/www/discourse/vendor/bundle/'

sudo -u www-data RAILS_ENV=production /usr/local/bin/bundle install

install discourse debian server

Copy the default configuration file to a new file.

sudo -u www-data cp config/discourse_defaults.conf config/discourse.conf

Edit the new file.

sudo -u www-data 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.

sudo -u www-data 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.

sudo -u www-data 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.

sudo -u www-data 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.

sudo -u www-data 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 393) 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.

sudo -u www-data RAILS_ENV=production /usr/local/bin/bundle exec rake assets:precompile

If you encounter the “To use ES6 syntax, harmony mode must be enabled” error, then simply run the same command again to continue.

harmony mode must be enabled

Next, edit the puma.rb file

sudo -u www-data 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.

sudo -u www-data mkdir /var/www/discourse/tmp/sockets/ /var/www/discourse/tmp/pids/

Start Discourse.

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

Sample output.

[28348] Puma starting in cluster mode...
[28348] * Puma version: 5.5.2 (ruby 2.7.4-p191) ("Zawgyi")
[28348] * Min threads: 8
[28348] * Max threads: 32
[28348] * Environment: production
[28348] * Master PID: 28348
[28348] * Workers: 4
[28348] * Restarts: (✔) hot (✖) phased
[28348] * Preloading application
[28348] * Listening on unix:///var/www/discourse/tmp/sockets/puma.sock
[28348] ! WARNING: Detected 2 Thread(s) started in app boot:
[28348] ! #<Thread:0x000055ee3c26d998 /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/message_bus-4.0.0/lib/message_bus.rb:720 sleep> - /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/redis-4.5.1/lib/redis/connection/ruby.rb:56:in `wait_readable'
[28348] ! #<Thread:0x000055ee3c26d790 /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/message_bus-4.0.0/lib/message_bus/timer_thread.rb:38 sleep> - /var/www/discourse/vendor/bundle/ruby/2.7.0/gems/message_bus-4.0.0/lib/message_bus/timer_thread.rb:130:in `sleep'
[28348] Use Ctrl-C to stop

Discourse is listening on Unix socket: /var/www/discourse/tmp/sockets/puma.sock. Press Ctrl+C to stop it now.

Step 6: Create Systemd Service for Discourse

We can create a systemd service for Discourse, so it can be automatically started at system boot time.

Edit the puma.rb file.

sudo -u www-data nano /var/www/discourse/config/puma.rb

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

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

Next, create a Systemd service for Discourse.

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

Add the following lines in the file.

[Unit]
Description=Discourse service

[Service]
Type=simple
User=www-data
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 enable --now discourse

Check the status. Make sure it’s running.

sudo systemctl status discourse

debian systemctl status discourse

Step 7: Configure Nginx Reverse Proxy

Install Nginx web server from the default Debian 11 software repository.

sudo apt install -y 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 8: 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 Debian 11 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.

discourse https

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 9: Create Admin Account

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

cd /var/www/discourse/

sudo -u www-data 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.

sudo systemctl restart discourse

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 can check the log to find out what went wrong.

sudo journalctl -eu discourse

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 10: 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.

sudo -u www-data 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

Step 11: 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.

sudo -u www-data 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.

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

[Service]
Type=simple
User=www-data
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 enable --now discourse-sidekiq

Check the status. Make sure it’s running.

sudo systemctl status discourse-sidekiq

The Sidekiq dashboard is available at https://community.example.com/sidekiq.

Step 12: 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.

sudo -u www-data 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]

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

After saving the SMTP settings, restart Discourse service.

sudo systemctl restart discourse discourse-sidekiq

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. Also, go to https://community.example.com/sidekiq/retries, it will show you the failed emails and why email-sending failed.

Sometimes, the discourse-sidekiq.service can fail and cause email not sending out, so you might want to check if this service is running

sudo systemctl status discourse-sidekiq

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.8.4

If you see the following error message.

Please commit your changes or stash them before you switch branches.
Aborting

Then run the following command

git stash

Then run the git checkout command again.

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
mv ~/discourse.conf /var/www/discourse/config/discourse.conf

Finally, start Discourse.

sudo systemctl start discourse

Now you can check Discourse version from the admin dashboard.

upgrade discourse without docker ubuntu

How to Move Discourse to A New Server

Discourse has a powerful backup and restore mechanism.

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 12. 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

sudo apt install rsync

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.

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

If the restore is successful, you should see the following message at the end.

Marking restore as finished...
Notifying 'system' of the end of the restore...
Finished!
[SUCCESS]
Restore done

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 Debian 11 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: 1 Average: 5]

One Response to “Install Discourse Forum Software on Debian 11 Without Docker

  • When I navigated to my domain.com/admin the site showed this message:
    “Oops! That page doesn’t exist or is private.”

    Do you know why this is happening?

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.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here