Install Rocket.Chat on Ubuntu 16.04

Rocket.Chat on AWS

Introduction

Rocket.Chat is a messaging system for team communication, like Slack. It has various features, including:

    • Video conferences
    • Help desk chat
    • File sharing
    • Voice messages
    • Link previews

This tutorial explains how to install and configure your Rocket.Chat Server on a Ubuntu 16.04 system.

Getting started

First, install all dependencies required by Rocket.Chat:

# apt install graphicsmagick build-essential

Install MongoDB

This system requires MongoDB, so let’s install that first. MongoDB provides packages for Ubuntu LTS. First, add its keyserver:

# sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

Next, add the repository with:

# echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Update apt repositories and install the database:

# apt update
# apt install mongodb-org

Open MongoDB and set it to run automatically at boot time, with systemd:

# systemctl start mongod
# systemctl enable mongod

Install Node.js and npm

Node.js and npm are required by Rocket.Chat and are both available on Ubuntu repositories. Install using the following codes:

# apt install nodejs
# apt install npm

Next, install n (a tool which let admins change the node version) using npm:

# npm install -g n

The messaging system requires Node.js 4.5+, so ensure that you choose 4.5:

# n 4.5

Check that everything is working well so far using the following code:

# node --version

Configure MongoDB Replica set (OPTIONAL)

This is an optional step, but those who want performance improvements should follow it. Rocket.Chat Server uses a MongoDB replica set. The concept behind this replica is that it forms a group of MongoDB processes all working on the same data set, which provides high availability.
To enable these replicas, edit the mongod.conf configuration file:

# $EDITOR /etc/mongod.conf

There, add this section:

replication:
      replSetName:  "001-rs"

Save, exit and restart MongoDB:

# systemctl restart mongod

Next, run its shell and initiate the replica set:

# mongo
> rs.initiate()

The output should look something like this:

{
  "info2" : "no configuration explicitly specified -- making one",
  "me" : "localhost:27017",
  "info" : "Config now saved locally.  Should come online in about a minute.",
  "ok" : 1
}

Pay attention to the last line! It’s important that “ok” is exactly “1”. Any other number would mean that an error has occurred.
The prompt should become 001-rs:PRIMARY>, which signifies that MongoDB is using the replica set.

Exit and add the following environment variable to the system:

MONGO_OPLOG_URL=mongodb://localhost:27017/local?replicaSet=001-rs

This can be done, for example, editing the ~/.bashrc file and adding the following:

export MONGO_OPLOG_URL=mongodb://localhost:27017/local?replicaSet=001-rs

Next, restart MongoDB:

# systemctl restart mongod

Installing Rocket.Chat

Now, it’s time to install Rocket.Chat, in the /var/www directory. Download the latest version with:

# cd /var/www
# curl -L https://rocket.chat/releases/latest/download -o rocket.chat.tgz

Extract the archive:

# tar xzf rocket.chat.tgz

Rename the extracted folder:

# mv bundle Rocket.Chat

Next, set environment variables and run the Rocket.Chat server with the following commands:

# cd Rocket.Chat/programs/server
# npm install
# cd ../..

# export ROOT_URL=http://your-host-name.com-as-accessed-from-internet:3000/
# export MONGO_URL=mongodb://localhost:27017/rocketchat
# export PORT=3000

# node main.js

Those who are using the replica set should set the MONGO_URL variable with this content: mongodb://localhost:27017/rocketchat?replicaSet=001-rs

Rocket.Chat is installed and configured, but it requires configuration behind a web server. In this tutorial we’ll be using NGINX.

Installing and configuring NGINX

Install the web server:

# apt install nginx

Create a new SSL directory, in which certificates will be stored:

# mkdir -p /etc/nginx/ssl/

In this directory, generate a new directory:

# cd /etc/nginx/ssl
# openssl req -new -x509 -days 365 -nodes -out /etc/nginx/ssl/rocketchat.crt -keyout /etc/nginx/ssl/rocketchat.key
# chmod 400 rocketchat.key

Next, create a Virtual Host configuration:

# $EDITOR /etc/nginx/sites-available/rocketchat

There, paste the following configuration:

# Upstreams
upstream backend {
    server 127.0.0.1:3000;
}
 
# Redirect Options
server {
  listen 80;
  server_name chat.mydomain.com;
  # enforce https
  return 301 https://$server_name$request_uri;
}
 
# HTTPS Server
server {
    listen 443;
    server_name chat.mydomain.com;
 
    error_log /var/log/nginx/rocketchat.access.log;
 
    ssl on;
    ssl_certificate /etc/nginx/ssl/rocketchat.crt;
    ssl_certificate_key /etc/nginx/ssl/rocketchat.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # dont use SSLv3 ref: POODLE
 
    location / {
        proxy_pass http://192.168.1.110:3000/;
        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, exit and activate this configuration:

# ln -s /etc/nginx/sites-available/rocketchat /etc/nginx/sites-enabled/rocketchat

Then, test NGINX:

# nginx -t

If no errors occur, restart NGINX:

# systemctl restart nginx

Update the environment variables and run Rocket.Chat:

#cd /var/www/Rocket.Chat/
# export ROOT_URL=https://chat.mydomain.com
# export MONGO_URL=mongodb://localhost:27017/rocketchat?replicaSet=001-rs
# export PORT=3000
# node main.js

The final step is to insert the following URL into a web browser: https://chat.mydomain.com to register a new admin account and finish the graphical configuration. At the end, the Rocket.Chat messaging system will be ready for daily usage.

Conclusion

There you have it! We’ve just explained how to install and configure your Rocket.Chat Server on a Ubuntu 16.04 system using NGINX. This useful online communication program can help your team work more efficiently and with more collaboration! Don’t forget that you can use various web servers for running Rocket.Chat, no need to stick with NGINX!