How to Install Rocket.Chat Server with Nginx on Ubuntu 20.04

Rocket.Chat is a web-based chat application written in JavaScript, using the Meteor full-stack framework. It is an open-source chat collaboration platform that allows you to communicate securely in real-time across multiple devices. It is self-hosted and supports voice and video chat, video conferencing, file sharing and many more. It has client application available for all platforms including, Windows, macOS, Linux, Android and iOS.

In this tutorial, we will show you how to install Rocket.Chat on Ubuntu 20.04 server.

Prerequisites

  • A server running Ubuntu 20.04.
  • A valid domain name pointed with your server IP.
  • A root password is configured the server.

Getting Started

Before starting, it is recommended to update your system packages to the latest version. You can update them with the following command:

apt-get update -y

Once your system is updated, install other required dependencies with the following command:

apt-get install nginx gnupg2 git unzip build-essential curl software-properties-common graphicsmagick -y

Once all the packages are installed, you can proceed to the next step.

Install Node.js

Next, you will need to install Node.js version 12 in your system. By default, it is not available in the Ubuntu 20.04 standard repository. So you will need to add the Node.js official repository to your system. You can add it with the following command:

curl -sL https://deb.nodesource.com/setup_12.x | bash -

Once the repository is added, install the Node.js with the following command:

apt-get install nodejs -y

After installing Node.js, verify the installed version of Node.js with the following command:

node -v

You should get the following output:

v12.19.0

Once you are finished, you can proceed to the next step.

Install and Configure MongoDB

Next, you will need to install the MongoDB server in your system. By default, the latest version of MongoDB is not available in the Ubuntu 20.04 default repository. So you will need to add the MongoDB repository in your system. First, download and add the GPG key with the following command:

apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

You should get the following output:

Executing: /tmp/apt-key-gpghome.Ku2thT4UnL/gpg.1.sh --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
gpg: key 68818C72E52529D4: public key "MongoDB 4.0 Release Signing Key <[email protected]>" imported
gpg: Total number processed: 1
gpg:               imported: 1

Next, add the MongoDB official repository with the following command:

add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse'

Once the repository is added, install the MongoDB with the following command:

apt-get install mongodb-org -y

After installing MongoDB, start the MongoDB service and enable it to start at system reboot:

systemctl start mongod
systemctl enable mongod

Next, you will need to edit MongoDB default configuration file and define the replica set. You can do it with the following command:

nano /etc/mongod.conf

Add the following lines:

replication:
  replSetName: "replica01"

Save and close the file then restart the MongoDB service to apply the configurationg:

systemctl restart mongod

Next, login to the MongoDB shell and enable the replica with the following command:

mongo
> rs.initiate()

You should get the following output:

{
	"info2" : "no configuration specified. Using a default configuration for the set",
	"me" : "127.0.0.1:27017",
	"ok" : 1,
	"operationTime" : Timestamp(1605081439, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1605081439, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

Next, exit from the MongoDB shell with the following command:

> exit

Once you are finished, you can proceed to the next step.

Install Rocket.Chat

Next, you will need to create a new user for Rocket.Chat and add it to www-data group. You can do it using the following command:

useradd -m -U -r -d /opt/rocket rocket --shell /bin/bash
usermod -a -G rocket www-data

Next, give proper permissions to the /opt/rocket directory:

chmod 750 /opt/rocket

Next, switch the user to Rocket.Chat and download the latest version of Rocket.Chat with the following command:

su - rocket
curl -L https://releases.rocket.chat/latest/download -o rocket.chat.tgz

Once the download is completed, extract the downloaded file with the following command:

tar -xvzf rocket.chat.tgz

Next, rename the extracted directory with Rocket.Chat with the following command:

mv bundle Rocket.Chat

Next, change the directory to Rocket.Chat server and install all Node.js dependencies using the following command:

cd Rocket.Chat/programs/server
npm install

Once all the dependencies are installed, you can exit from the Rocket.Chat user using the following command:

exit

Create a Systemd Service File For Rocket.Chat

Next, you will need to create a systemd service file to manage the Rocket.Chat service. You can create it with the following command:

nano /etc/systemd/system/rocketchat.service

Add the following lines:

[Unit]
Description=Rocket.Chat server
After=network.target nss-lookup.target mongod.target

[Service]
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=rocketchat
User=rocket
Environment=MONGO_URL=mongodb://localhost:27017/rocketchat ROOT_URL=http://rocket.yourdomain.com PORT=3000
ExecStart=/usr/bin/node /opt/rocket/Rocket.Chat/main.js

[Install]
WantedBy=multi-user.target

Save and close the file the reload the systemd daemon with the following command:

systemctl daemon-reload

Next, start the Rocket.Chat service and enable it to start at system reboot with the following command:

systemctl start rocketchat
systemctl enable rocketchat

You can now check the status of the Rocket.Chat with the following command:

systemctl status rocketchat

If everything is fine, you should get the following output:

? rocketchat.service - Rocket.Chat server
     Loaded: loaded (/etc/systemd/system/rocketchat.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2020-11-11 07:58:01 UTC; 24s ago
   Main PID: 19951 (node)
      Tasks: 11 (limit: 4691)
     Memory: 508.7M
     CGroup: /system.slice/rocketchat.service
             ??19951 /usr/bin/node /opt/rocket/Rocket.Chat/main.js

Nov 11 07:58:21 ubuntu2004 rocketchat[19951]: ? |      MongoDB Version: 4.0.21                             |
Nov 11 07:58:21 ubuntu2004 rocketchat[19951]: ? |       MongoDB Engine: wiredTiger                         |
Nov 11 07:58:21 ubuntu2004 rocketchat[19951]: ? |             Platform: linux                              |
Nov 11 07:58:21 ubuntu2004 rocketchat[19951]: ? |         Process Port: 3000                               |
Nov 11 07:58:21 ubuntu2004 rocketchat[19951]: ? |             Site URL: http://rocket.yourdomain.com  |
Nov 11 07:58:21 ubuntu2004 rocketchat[19951]: ? |     ReplicaSet OpLog: Enabled                            |
Nov 11 07:58:21 ubuntu2004 rocketchat[19951]: ? |          Commit Hash: e37d7ba8ed                         |
Nov 11 07:58:21 ubuntu2004 rocketchat[19951]: ? |        Commit Branch: HEAD                               |
Nov 11 07:58:21 ubuntu2004 rocketchat[19951]: ? |                                                          |
Nov 11 07:58:21 ubuntu2004 rocketchat[19951]: ? +----------------------------------------------------------+

Configure Nginx for Rocket.Chat

Next, you will need to configure the Nginx as a reverse proxy for Rocket.Chat. To do so, create an Nginx virtual host configuration file with the following command:

nano /etc/nginx/sites-available/rocketchat.conf

Add the following lines:

upstream myrocketchat {
  server 127.0.0.1:3000;
}

server {
    listen 80;
    server_name rocket.yourdomain.com;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        proxy_pass http://myrocketchat/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forward-Proto http;
        proxy_set_header X-Nginx-Proxy true;

        proxy_redirect off;
    }
}

Save and close the file then activate the Nginx virtual host with the following command:

ln -s /etc/nginx/sites-available/rocketchat.conf /etc/nginx/sites-enabled/

Next, verify the Nginx for any systex error with the following command:

nginx -t

You should get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Finally, restart the Nginx service to apply the configuration changes:

systemctl restart nginx

You can also verify the status of the Nginx service with the following command:

systemctl status nginx

You should get the following output:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2020-11-11 07:59:39 UTC; 4s ago
       Docs: man:nginx(8)
    Process: 20034 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 20047 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 20051 (nginx)
      Tasks: 3 (limit: 4691)
     Memory: 3.5M
     CGroup: /system.slice/nginx.service
             ??20051 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??20052 nginx: worker process
             ??20053 nginx: worker process

Nov 11 07:59:39 ubuntu2004 systemd[1]: Starting A high performance web server and a reverse proxy server...
Nov 11 07:59:39 ubuntu2004 systemd[1]: Started A high performance web server and a reverse proxy server.

Once you are finished, you can proceed to the next step.

Access Rocket.Chat

At this point, Rocket.Chat is installed and configured. It's time to access the Rocket.Chat web interface. Open your web browser and access the Rocket.Chat using the URL http://rocket.yourdomain.com.You will be redirected to the following page:

Rocket.Chat Setup Wizard

Provide your admin name, email, password and click on the Continue button. You should see the following page:

Organization info

Provide your organization type, name, industry, size, country and click on the Continue button. You should see the following page:

Server info

Provide your site name, language, server type, and click on the Continue button. You should see the following page:

Register server

Agree the Terms and Privacy Policy then click on the Continue button. You should see the following page.

Rocket.Chat server ready

Click on the Go to your workspace button. You will be redirected to the Rocket.Chat dashboard in the following page:

Rocket.Chat Dashboard

Conclusion

Congratulations! you have successfully installed and configured Rocket.Chat with Nginx as a reverse proxy. You can now install Rocket.Chat in the production environment and start chatting with your friends and family.

Share this page:

3 Comment(s)