Deploy RocketChat to EC2 Ubuntu instance

Wajeeh Ahsan
3 min readSep 30, 2020

Prerequisite
OS: Ubuntu 18.04 LTS, Ubuntu 19.04 and Ubuntu 20.04(Latest)

This installation guide was tested in the following environment:

  • Rocket.Chat 3.3.0
  • OS: Ubuntu 18.04 LTS, Ubuntu 19.04 and Ubuntu 20.04
  • Mongodb 4.0.9
  • NodeJS 12.14.0

Install necessary dependency packages

Update package list.

sudo apt-get -y update

The configure apt to install the official MongoDB packages with the following repository file:

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

And

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

Configure Node.js to be installed via package manager:

sudo apt-get -y update && sudo apt-get install -y curl && curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -

Install build tools, MongoDB, nodejs and graphicsmagick:

sudo apt-get install -y build-essential mongodb-org nodejs graphicsmagick

Using npm install inherits and n, and the node version required by Rocket.Chat:

sudo npm install -g inherits n && sudo n 12.14.0

Install Rocket.Chat

Download the latest Rocket.Chat version:

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

Or you may build your meteor app’s image with following command. Make sure you run this command while sitting inside your app/project directory.

meteor build --directory output_directory_name

In my case, my app name was rocketchat and I used rocketchat-build as output directory. Note that you can’t make this output directory residing inside your project directory. So after this command run successfully, I got a directory rocketchat-build having rocketchat.gtz . The directory structure was something like this:

rokcetchat-build/rocketchat.tgz

Then I pushed this .tgz file on a git repo and then cloned it onto my EC2 Ubuntu instance in the home directory.

Either you download the pre-build app through curl from Rocketchat repo or you build your own app, push it on git and clone it, in either case, the next step is:

tar -xzf /tmp/rocketchat.tgz -C /tmp

Install (this guide uses /opt but feel free to choose a different directory):

cd /tmp/bundle/programs/server && npm install

Then move this bundle folder as

sudo mv /tmp/bundle /opt/Rocket.Chat

Now, Your app is ready to be run. Let’s head to that.

Configure the Rocket.Chat service

Add the rocketchat user, set the right permissions on the Rocket.Chat folder and create the Rocket.Chat service file:

sudo useradd -M rocketchat && sudo usermod -L rocketchat

Then grant permissions to this newly created user as:

sudo chown -R rocketchat:rocketchat /opt/Rocket.Chat

Then

cat << EOF |sudo tee -a /lib/systemd/system/rocketchat.service
[Unit]
Description=The Rocket.Chat server
After=network.target remote-fs.target nss-lookup.target nginx.target mongod.target
[Service]
ExecStart=/usr/local/bin/node /opt/Rocket.Chat/main.js
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=rocketchat
User=rocketchat
Environment=MONGO_URL=mongodb://localhost:27017/rocketchat?replicaSet=rs01 MONGO_OPLOG_URL=mongodb://localhost:27017/local?replicaSet=rs01 ROOT_URL=http://localhost:3000/ PORT=3000
[Install]
WantedBy=multi-user.target
EOF

Open the Rocket.Chat service file just created (/lib/systemd/system/rocketchat.service) using sudo and your favourite text editor, and change the ROOT_URL environmental variable to reflect the URL you want to use for accessing the server. ROOT_URL must be Public IPv4 DNS. To get this, go to EC2 Dashboard on AWS console, click on Runnin Instances then click Instance ID of your instance and copy Public IPv4 DNS (optionally change MONGO_URL, MONGO_OPLOG_URL and PORT):

MONGO_URL=mongodb://localhost:27017/rocketchat?replicaSet=rs01
MONGO_OPLOG_URL=mongodb://localhost:27017/local?replicaSet=rs01
ROOT_URL=http://Public_IPv4_DNS
PORT=3000

Setup storage engine and replication for MongoDB (mandatory for versions > 1), and enable and start MongoDB and Rocket.Chat:

sudo sed -i "s/^#  engine:/  engine: mmapv1/"  /etc/mongod.conf

Then

sudo sed -i "s/^#replication:/replication:\n  replSetName: rs01/" /etc/mongod.conf

Now enable and run monog as service.

sudo systemctl enable mongod && sudo systemctl start mongod

Then

mongo --eval "printjson(rs.initiate())"

Time to enable and run rocketchat as service

sudo systemctl enable rocketchat && sudo systemctl start rocketchat

Now time to setup NGNIX server who will serve requests with rocketchat service.

Run this as root:

apt-get install nginx

Now edit /etc/nginx/sites-available/default with this command

sudo nano /etc/nginx/sites-available/default

and make the file as follow:

upstream backend {
server 127.0.0.1:3000;
}
server {
listen 80 default_server;
server_name _;
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $remote_addr;
proxy_redirect off;
}
}

Save it and restart Nginx: service nginx restart .

Now in the browser address bar, type in your Public IPv4 DNS and hit enter. This will take you to your app.

BOOM :)

--

--