Home / How To / How to install NodeBB Forum with Nginx and Let’s Encrypt SSL on Ubuntu 20.04 LTS

How to install NodeBB Forum with Nginx and Let’s Encrypt SSL on Ubuntu 20.04 LTS



NodeBB is a modern forum software powered by Node.js that uses MongoDB / Redis as database platforms. It uses web sockets for instant interactions and real-time messaging.

In this tutorial we will teach you how to install NodeBB forums on an Ubuntu 20.04 based server.

conditions

  • An Ubuntu 20.04 based server with non-root users with sudo privileges.

  • At least 2 GB of RAM. If your server only has 1 GB of RAM, it is best to enable a swap partition.

  • Make sure everything is up to date.

    $ sudo apt update && sudo apt upgrade
    
  • Get important packages. Some of these are already on your server.

    $ sudo apt install curl wget nano ca-certificates gnupg2 lsb-release
    

Step 1 – Configure the firewall

The first step is to configure the firewall. Ubuntu comes standard with ufw (uncomplicated firewall).

Check if the firewall is running.

$ sudo ufw status

You should get the following output.

Status: inactive

Leave the SSH port so that the firewall does not disconnect the current connection when you activate it.

$ sudo ufw allow OpenSSH

Allow 27017 port for the MongoDB server and 4567 port to run the NodeBB forum. Allow HTTP and HTTPS also ends up.

$ sudo ufw allow 27017
$ sudo ufw allow 4567
$ sudo ufw allow 80
$ sudo ufw allow 443

Activate the firewall.

$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

Check the firewall status again.

$ sudo ufw status

You should see a similar outcome.

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
27017                      ALLOW       Anywhere
4567                       ALLOW       Anywhere
80                         ALLOW       Anywhere
443                        ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
27017 (v6)                 ALLOW       Anywhere (v6)
4567 (v6)                  ALLOW       Anywhere (v6)
80 (v6)                    ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)

Step 2 – Install Node.js

We will install the LTS version of Node.js here. Run the following commands to install Node.js.

$ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
$ sudo apt install nodejs

Check if Node is installed correctly.

$ node --version

You should see a similar outcome.

v12.18.3

Also check npm.

$ npm --version
6.14.6

Step 3 – Install MongoDB

MongoDB is the default database for NodeBB. You can also use Redis instead of MongoDB. We will cover both databases in our guide.

MongoDB’s archive for Ubuntu 20.04 is not yet ready and Ubuntu itself has a very old version. For our purposes, we will use the MongoDB archive for 18.04, which works well. You can update the package list with the latest archive when official support is available.

Import the MongoDB public key.

$ wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

Add the MongoDB archive.

$ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

Update the local package database.

$ sudo apt update

Install MongoDB.

$ sudo apt install mongodb-org

Verify the installation of MongoDB.

$ mongod --version
db version v4.2.8
...

Start the MongoDB service.

$ sudo systemctl start mongod

Activate the MongoDB service.

$ sudo systemctl enable mongod

Check the status of the MongoDB service.

$ sudo systemctl status mongod

Step 4 – Configure MongoDB

The Mongo administration is done through the MongoDB shell. The default installation of MongoDB listens on port 27017.advertisement

Access the MongoDB shell.

$ mongo

Switch to the built-in admin database.

> use admin

Create an administrative user. This is not the same as the NodeBB administrator.

> db.createUser( { user: "admin", pwd: "yourpassword", roles: [ { role: "root", db: "admin" } ] } )

Replace the seat holder “yourpassword” to your own password.

Add a new database for NodeBB.

> use nodebb

Then create nodebb users to manage nodebb database.

> db.createUser( { user: "nodebb", pwd: "yourpassword", roles: [ { role: "readWrite", db: "nodebb" }, { role: "clusterMonitor", db: "admin" } ] } )

The readwrite permission allows NodeBB to store and retrieve data from nodebb database. The clustermonitor authorization gives NodeBB read-only access to database statistics visible via its admin panel.

Exit the Mongo shell.

> quit()

Open the MongoDB configuration file for editing.

$ sudo nano /etc/mongod.conf

Add the following line at the end of the file.

security:
  authorization: enabled

Restart MongoDB and verify the previously created administrative user.

$ sudo systemctl restart mongod
$ mongo -u admin -p yourpassword --authenticationDatabase=admin

You should see the Mongo prompt if everything was configured correctly.

Step 5 – Install Git

Before we continue to install NodeBB, we need to install Git.

Run the following command to install Git.

$ sudo apt install git

Run the following commands to perform the initial configuration of Git.

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"

Step 6 – Install NodeBB

It is not advisable to run NodeBB as a root user.

Create a privileged user.

$ sudo adduser nodebb

Set a password of your choice and skip other options.

Create the directory where your NodeBB forum will reside.

$ sudo mkdir /var/www/nodebb

Change the ownership of the folder to the newly created user.

$ sudo chown -R nodebb:nodebb /var/www/nodebb

Log in to the newly created user.

$ su nodebb

Switch to the NodeBB installation directory.

$ cd /var/www/nodebb

To install NodeBB, we must first clone its Github archive.

Clone NodeBB to /var/www/nodebb directory. The point at the end of the command refers to the current directory.

$ git clone -b v1.14.2 https://github.com/NodeBB/NodeBB.git .

Here we have cloned v1.14.2 for NodeBB, which was the latest stable version when writing tutorials. You will find the latest stable branch from NodeBB’s latest version of the page.

NodeBB comes with a command line tool. Use the following command to install NodeBB.

$ ./nodebb setup

You can press enter to select the default value.

For the value of URL used to access this NodeBB Select the final URL you want to access the forum on. If you access the forum via your server IP, enter it or enter the entire domain of the forum. Here we come in http://forum.example.com.

Continue selecting default values ​​until prompted to do so MongoDB user which is when you enter nodebb and the password you previously selected for that username when configuring MongoDB. Your database nodebb should be selected. You will also be asked to create an administrator user and its details.

When the installation is complete, run the following command to start NodeBB.

$ ./nodebb start

Your forum is now running. You should be able to access it via http://:4567.

NodeBB standard website

You may see an error message saying It looks like your connection to NodeBB has been lost, please wait while we try to connect again. This is because we chose the default URL for NodeBB http://forum.example.com and not http://. However, if you enter the IP address during installation, you will no longer receive the error but will need to configure it again later once you have selected a domain for your forum.

Leave the NodeBB user.

$ exit

Step 7 - Install Nginx

By default, Ubuntu 20.04 has the latest stable version of Nginx. We will install it.

$ sudo apt install nginx

Check if it is installed correctly.

$ nginx -v
nginx version: nginx/1.18.0 (Ubuntu)

Launch and activate Nginx.

$ sudo systemctl start nginx
$ sudo systemctl enable nginx

Open the server's IP address in your browser. You should see the following page, which means your server is up and running.

Nginx default page

Step 8 - Configure Nginx

Run the following command to add a configuration file to your site.

$ sudo nano /etc/nginx/sites-available/nodebb.conf

Paste the following code into the editor.

server {
    listen 80;

    server_name forum.example.com;
    
    access_log /var/log/nginx/forum.example.com.access.log;
    error_log /var/log/nginx/forum.example.com.error.log;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:4567;
        proxy_redirect off;

        # Socket.IO Support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

Print Ctrl + X to leave the editor and enter Y when prompted.

When done, verify that the Nginx configuration is correct.

$ sudo nginx -t

Reload the Nginx service to activate the configuration.

$ sudo systemctl reload nginx

Visit http://forum.example.com in your browser to open the NodeBB forum.

NodeBB forum website

Step 9 - Run NodeBB as a system service

The NodeBB service does not run after restarting the system. To avoid starting NodeBB every time, we need to install it as a system service.

Stop the NodeBB service first.

$ ./nodebb stop

Run the following command to create and edit nodebb.service systemd device configuration file.

$ sudo nano /etc/systemd/system/nodebb.service

Paste the following code into the editor.

[Unit]
Description=NodeBB
Documentation=https://docs.nodebb.org
After=system.slice multi-user.target mongod.service

[Service]
Type=forking
User=nodebb

StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodebb

Environment=NODE_ENV=production
WorkingDirectory=/var/www/nodebb
PIDFile=/var/www/nodebb/pidfile
ExecStart=/usr/bin/env node loader.js
Restart=always

[Install]
WantedBy=multi-user.target

Here we have chosen username as nodebb which we created in step 6 and the path we have chosen to install NodeBB in it.

Activate the NodeBB service.

$ sudo systemctl enable nodebb

Start the NodeBB service.

$ sudo systemctl start nodebb

Check the status of the service.

$ sudo systemctl status nodebb

Step 10 - Obtain a Let's Encrypt SSL Certificate

Securing your NodeBB forum with HTTPS is a necessary step to secure your website traffic. For this tutorial we will use the Certbot client to install SSL.

First, install the Certbot tool.

$ sudo apt install certbot python3-certbot-nginx

Generate the certificates.

$ sudo certbot --nginx -d forum.example.com

If this is the first time you are running Certbot on your system, you will be asked for an email address and accept the terms of use. You will also be asked if you agree to share data with the EFF Foundation, to which you can say no. After doing so, Certbot will communicate with Let's Encrypt servers and run a challenge to verify your domains.

If successful, you will be asked how to handle HTTPS redirects.

Please choose whether HTTPS access is required or optional.
-------------------------------------------------------------------------------
1: Easy - Allow both HTTP and HTTPS access to these sites
2: Secure - Make all requests redirect to secure HTTPS access
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

Select Secure Select and press Enter. This ensures that all requests to the http domain are redirected to the https version. Your certificates are then created and your Nginx configuration files are updated with SSL settings.

Your certificates are ready and you can open your website now by going to https://forum.example.com

Step 11 - Verify SSL Auto-Renewal

This is the last step before we end this tutorial.

Control the renewal process by doing a dry run of the renewal process.

$ sudo certbot renew --dry-run

If you do not get any errors, it means that you are set. Certbot automatically renews your certificates for you. You will be sent an email warning that the certificate is expiring.

Conclusion

That's all for this tutorial. Your NodeBB forum set is complete. If you have any questions, post them in the comments below.


Source link