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

Rocket.Chat is a free and open source online chat solution for team communication, it allows you to build your own slack like online chat. Rocket.Chat is based on Javascript, developed using the Meteor full stack framework. Rocket.Chat is a great solution for companies that wants to build their own private chat platform.

In this tutorial, I will show you how to build your own chat server using Rocket.Chat. I will use the latest Ubuntu LTS 16.04 server for the installation and Nginx as reverse proxy for the Rocket.Chat application.

Prerequisite

  • Ubuntu 16.04
  • Root Privileges

Step 1 - Install the Rocket.Chat Dependencies

Log in to the server using ssh (or the terminal) and type 'sudo su' to gain root access:

ssh [email protected]
sudo su

Update the Ubuntu repository:

apt-get update

Next, install new packages needed by Rocket.Chat:

apt-get install curl graphicsmagick build-essential

Step 2 - Install MongoDB

Rocket.Chat requires MongoDB for the installation. In this step, we will install MongoDB 3.2 from the MongoDB repository.

Add the MongoDB keyserver so we can access the packages:

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

Then add the MongoDB repository with the command below:

echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Update the repository and install MongoDB with the apt command:

apt-get update
apt-get install mongodb-org

Add MongoDB to run at the boot time and start it:

systemctl enable mongod
systemctl start mongod

MongoDB has been started on port 27017.

Install MongoDB and start it.

Step 3 - Configure MongoDB ReplicaSet

To improve the performance, Rocket.Chat uses a MongoDB ReplicaSet. In this step, we will configure a simple MongoDB ReplicaSet by editing the MongoDB configuration file.

Edit mongod.conf file with vim:

vim /etc/mongod.conf

Add a "#" at the beginning of line 24 to disable MongoDB running on the localhost IP only.

net:
    port: 27017
    #bindIp: 127.0.0.1

Add the ReplicaSet configuration below at line 34.

#replication:                                                              
replication:                                                               
   oplogSizeMB: 1                                                          
   replSetName: rs0

Save and exit.

Restart the MongoDB service:

systemctl restart mongod

Next, start the MongoDB shell and initiate the ReplicaSet:

export LC_ALL=C
mongo
rs.initiate()

Initiate results:

{
  "info2" : "no configuration specified. Using a default configuration for the set",
  "me" : "nyanko-sensei:27017",
  "ok" : 1
}

Make sure the 'ok' value is 1. If the result is another number, then this means that something is wrong.

Configure MongoDB ReplicaSet

Step 4 - Install npm and nodejs

In this step, we will install nodejs and npm from the Ubuntu repository. Then we will install the n package with the npm command to get a specific nodejs version. Latest Rocket.Chat version needs nodejs 4.5.

Install nodejs and npm from the Ubuntu repository:

apt-get install nodejs npm

Next install the n package globally to the system with the npm command:

npm install -g n

Use the n command to download and set nodejs version 4.5.

sudo n 4.5

Finally, check the nodejs version:

node --version
npm -v

Manage the NodeJS version with the n command

Step 5 - Install Rocket.Chat Server

We will install Rocket.Chat (latest version) in the /var/www/ directory.

Download the latest version of Rocket.Chat and extract it:

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

Create a new /var/www/ directory and rename the bundle directory to 'Rocket.Chat' and then move it:

mkdir -p /var/www/
mv bundle Rocket.Chat
mv Rocket.Chat /var/www/

Go to the Rocket.Chat directory, set some required environment variables and start the Rocket.Chat server.

cd /var/www/Rocket.Chat/
cd programs/server/
npm install

cd ../../
export ROOT_URL=http://192.168.1.110:3000/
export MONGO_URL=mongodb://nyanko-sensei:27017/rocketchat?replicaSet=rs0
export PORT=3000
node main.js

Install the Rocket.Chat server on Ubuntu 16.10

Rocket.Chat is installed, open your web browser and visit the server IP address on port 3000. - 192.168.1.110:3000.

Rocket Chat is installed and working fine.

Step 6 - Install and Configure Nginx as Reverse Proxy for Rocket.Chat

In step 5, we installed Rocket.Chat as standalone application. In this tutorial, we will run Rocket.Chat behind the Nginx web server on https connections to get a secure and fast setup.

Install Nginx with the apt command:

apt-get install nginx

Create a new SSL directory:

mkdir -p /etc/nginx/ssl/
cd /etc/nginx/ssl/

Generate the SSL certificate file and change permission of the key file:

openssl req -new -x509 -days 365 -nodes -out /etc/nginx/ssl/rocket-chat.crt -keyout /etc/nginx/ssl/rocket-chat.key
chmod 400 rocket-chat.key

Next, create a new rocket-chat virtual host file in the sites-available directory.

cd /etc/nginx/sites-available/
vim rocket-chat

Paste the new virtual host configuration below:

# Upstreams
upstream backend {
    server 127.0.0.1:3000;
}

# Redirect Options
server {
  listen 80;
  server_name rocket-chat.co;
  # enforce https
  return 301 https://$server_name$request_uri;
}

# HTTPS Server
server {
    listen 443;
    server_name rocket-chat.co;

    error_log /var/log/nginx/rocketchat.access.log;

    ssl on;
    ssl_certificate /etc/nginx/ssl/rocket-chat.crt;
    ssl_certificate_key /etc/nginx/ssl/rocket-chat.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 and exit.

I will run Rocket.Chat with new domain name 'rocket-chat.co'. Please use your own domain name and replace it in the configuration files wherever it appears.

Activate the Rocket.Chat virtual host and test the configuration file:

ln -s /etc/nginx/sites-available/rocket-chat /etc/nginx/sites-enabled/rocket-chat
nginx -t

Make sure there are no error messages.

Restart the Nginx web server:

systemctl restart nginx

Step 7 - Testing Rocket.Chat

Nginx has been configured as reverse proxy for the Rocket.Chat server and we need to update the environment variables to run Rocket.Chat now.

Run Rocket.Chat with the new variables below:

cd /var/www/Rocket.Chat/
export ROOT_URL=https://rocket-chat.co
export MONGO_URL=mongodb://nyanko-sensei:27017/rocketchat?replicaSet=rs0
export PORT=3000
node main.js

Rocket.Chat with Nginx Web Server

In your web browser, open the URL: rocket-chat.co (or your custom domain nam) - you will be redirected to the https connection.

Create your first admin account by clicking on 'Register a new account' link.

Rocket.Chat over SSL

Enter your name, email and password, and then click on 'REGISTER A NEW ACCOUNT'.

Create an admin account

Confirm to use the new 'SITE-URL' with the domain name. Click 'Yes'.

Confirm to use the new URL

Register the username, click 'USE THIS USERNAME'.

Register the username

The admin user has been created and here is the Rocket.Chat Dashboard. You can start chatting and create your own channel for your team.

Rocket.Chat has been installed with Nginx web server on ubuntu 16.04.

Rocket.Chat Dashboard

Results after we used the new environment variables:

Variable update

Reference

Share this page:

0 Comment(s)

Add comment