How to Install Discourse Forum with Nginx on CentOS 7

Discourse is a free and open source software for creating Internet forum community and mailing list. It was created in 2013, and written using Ruby and Javascript programming languages. Discourse forum software has been used by many organizations, including Codeacademy, Udacity, Twitter Developers, Docker Community Forum etc.

In this tutorial, we will show you step-by-step, how to install and configure Discourse Forum with Nginx Web server under the CentOS 7 server. Specifically, we will show you how to install the Discourse Forum Software, configure Nginx as the reverse proxy for the Discourse app, and secure the discourse forum software using free SSL from Letsencrypt.

What we will do

  1. Install Docker on CentOS 7
  2. Install and Configure Discourse Forum Software
  3. Generate SSL Letsencrypt on CentOS 7
  4. Install and Configure Nginx as a Reverse Proxy for Discourse
  5. Discourse Web Installer
  6. Discourse Additional Configuration

Prerequisites

  • CentOS 7
  • Root Privileges
  • SMTP Account - e.g. from Mailgun, SendGrid, or Mailjet

Step 1 - Install Docker on CentOS 7

The discourse forum will be installed under Docker container. So the first step we must do is to install Docker on our CentOS 7 server.

Install Docker on CentOS 7 using the following command.

wget -qO- https://get.docker.com/ | sh

If you do not have the wget command, install it from the repository.

yum -y install wget

If the installation is complete, start Docker service and enable it to launch at boot time using the following systemctl commands.

systemctl start docker
systemctl enable docker

The Docker engine has been installed and it's running on the CentOS 7 server. Check the service status using the following command.

systemctl status docker

The Docker service is active and running.

Docker service is running

Step 2 - Install and configure Discourse Forum Software

In this step, we will install and configure the Discourse software. We will download the discourse docker-compose script, configure it as we need, and then create a new Docker container for discourse forum software.

Before downloading discourse, we need to install the git command to the server.

yum -y install git

Now create a new directory '/var/discourse' and download/clone discourse docker script using the git command.

mkdir -p /var/discourse
git clone https://github.com/discourse/discourse_docker.git /var/discourse

Go to the discourse directory and copy a sample of docker-compose script 'standalone.yml' to the '/var/discourse/containers/' directory with the name 'app.yml'.

cd /var/discourse
cp samples/standalone.yml containers/app.yml

Clone discourse docker

Now edit the 'app.yml' file using vim.

vim containers/app.yml

- Docker Port Mapping

By default, all HTTP and HTTPS requests will be handled by the Docker proxy. And for this guide, we will use Nginx for this purpose - all HTTP and HTTPS will be handled by Nginx web server.

So we need to change the docker port mapping configuration. The Discourse container will only have the HTTP connection, and the host will be open new port '2045' and map to the container port 80.

Uncomment the HTTPS line and change the HTTP line with new port '2045', as shown below.

expose:
  - "2045:80"   # http
#  - "443:443" # https

- Discourse Domain Name Configuration

Type your own domain name for discourse installed on the 'DISCOURSE_HOSTNAME' line as below.

DISCOURSE_HOSTNAME: 'discourse.hakase-labs.co'

And type your email address in the 'DISCOURSE_DEVELOPER_EMAIL' line.

DISCOURSE_DEVELOPER_EMAILS: '[email protected]'

- SMTP Configuration

SMTP Configuration is the most important configuration for Discourse Software. Make sure you have the SMTP account for Discourse installation - you can buy or try free-tier SMTP account from Mailgun, Mailjet or SendGrid.

For this tutorial, we will be using the free-tier SMTP account from Mailgun. Register with Mailgun and configure your domain name, and make sure you get details of your SMTP account.

Then uncomment SMTP configuration and type your account details as below.

  DISCOURSE_SMTP_ADDRESS: smtp.mailgun.org
  DISCOURSE_SMTP_PORT: 587
  DISCOURSE_SMTP_USER_NAME: [email protected]
  DISCOURSE_SMTP_PASSWORD: mypassword

That's it. Save the changes and exit the editor.

Next, build new Discourse Docker image based on app.yml template configuration.

sudo ./launcher bootstrap app

Launch Discourse

If all is complete, start/initialize new Discourse container using the following command.

sudo ./launcher start app

initialize new Discourse container

And the Discourse Docker container is up and running - check it using the 'docker ps' command.

docker ps -a

Check the host open port using netstat, and make sure the new service docker-proxy with port 2045 is on the list.

netstat -plntu

Check if discourse is running

The Discourse Forum software has been installed under docker container on the host CentOS 7.

Step 3 - Generate SSL Letsencrypt on CentOS 7

For this tutorial, we will be running Discourse forum under Nginx web server and only accept the HTTPS connection. For this purpose, we need new SSL Certificates for the domain name, and so, we will be using free SSL certificate from Letsencrypt.

Install Letsencrypt command line tool using the yum command in the following way.

yum -y install letsencrypt

After the installation, add new HTTP and HTTPS service to the Firewalld configuration.

firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload

Generate Lets encrypt SSL certificate

Now generate new SSL certificates for Discourse forum using letsencrypt command below.

letsencrypt certonly

You will see two verifications from Letsencrypt. Type number '1' to spin up a temporary web server for authentication.

As part of this, type your email address for renewing notification, type 'A' to agree on the letsencrypt TOS (Terms Of Service).

Now type the discourse domain name 'discourse.hakase-labs.co'.

letsencrypt certonly command

And when the process is complete, you will get the result as below.

SSL certificate has been created

New SSL certificate from letsencrypt has been generated in the '/etc/letsencrypt/live' directory.

Step 4 - Install and Configure Nginx as a Reverse Proxy for Discourse

In this tutorial, we will be using the Nginx web server as a reverse proxy for Discourse that's running under the Docker container. The Nginx web server will run under HTTP and HTTPS port, and all client requests will be handled by Nginx.

Before installing the Nginx web server, we need to install the EPEL repository on the system.

yum -y install epel-release

Now install nginx from the EPEL repository using the following yum command.

yum -y install nginx

After all the installation is complete, go to the nginx configuration directory '/etc/nginx'.

cd /etc/nginx/

And create a new additional SSL configuration 'ssl.conf' using vim.

vim ssl.conf

Paste the following SSL configuration there.

ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;

ssl_protocols TLSv1.2;
ssl_ciphers EECDH+AESGCM:EECDH+AES;
ssl_ecdh_curve secp384r1;
ssl_prefer_server_ciphers on;

ssl_stapling on;
ssl_stapling_verify on;

add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;

Save the changes and exit the editor.

Next, create a new nginx virtual host file 'discourse.conf' for Discourse.

vim conf.d/discourse.conf

Paste the following configuration there.

server {
    listen 80; listen [::]:80;
    server_name discourse.hakase-labs.co;
 
    # Automatic Redirect HTTP to HTTPS Nginx
    return 301 https://$host$request_uri;
}
 
server {
    listen 443 ssl http2; 
    server_name discourse.hakase-labs.co;

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/discourse.hakase-labs.co/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/discourse.hakase-labs.co/privkey.pem;
    include /etc/nginx/ssl.conf;
 
    # Reverse Proxy Configuration
    location / {
        proxy_pass http://discourse.hakase-labs.co:2045/;
        proxy_set_header Host $http_host;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect http://discourse.hakase-labs.co:2045/ https://discourse.hakase-labs.co;
    }
}

Save and exit.

Now test the nginx configuration and make sure there is no error. Then restart the Nginx service.

nginx -t
systemctl restart nginx

Test nginx config and restart nginx

Nginx installation and configuration as a reverse proxy for Discourse container has been completed.

Step 5 - Discourse Web Installer

Open your web browser and visit your discourse URL, mine is:

http://discourse.hakase-labs.co

And you will be redirected to the HTTPS connection.

Discourse web installer

Click the 'Register' button.

Now we need to create a new 'Administrator' account.

Register admin account

Type your admin username and password, then click 'Register'.

And you will get the page for email confirmation. Check your email inbox and make sure you get the email configuration from the Discourse forum as below.

Email from discourse which contains the password

Click on the 'Link'.

And you will get the 'Discourse Welcome' page.

Welcome to discourse

Click on the button 'click here to activate your account'. And now you should get to the following page.

activate account

Click the 'Maybe Later' button.

The Discourse Forum has been installed, and we get the default homepage as shown below.

Discourse successfully installed

You can go the 'Admin Dashboard', and you will get the Discourse Admin Dashboard.

Discourse dashboard

The Discourse Forum Software has been installed with Nginx as a reverse proxy on CentOS 7 server.

Step 6 - Discourse Additional Configuration

During the Discourse installation, some people say they don't get the email confirmation for admin user activation.

For this issue, make sure you have the right SMTP account on the configuration app.yml. Or you can activate the admin account manually from your server.

To activate your admin account manually, go to the '/var/discourse' directory.

cd /var/discourse

Now access the discourse container with the following command.

./launcher enter app

And activate the first admin user using the rails command as shown below.

rails c
u = User.last
u.admin = true
u.activate
u.save

Now you can log into the Discourse forum with your username and password.

Reference

Share this page:

4 Comment(s)