Deploying Nodejs pm2 to Ubuntu 20.0

neonexxa
3 min readMar 28, 2021

psst: this is my personal docs for deploying my projects..

  1. make release branch

For easier and automatic deployment lets make a release branch first, just create a new branch in your git repository and named it release

2. Ssh into your new ubuntu 20.0 and follow below setup

with yarn

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
cd ~
curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install -y nodejs
sudo apt install -y build-essential
sudo apt install -y yarn
sudo yarn global add pm2

or with npm install yarn

sudo apt update
cd ~
curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install -y nodejs
sudo apt install -y build-essential
sudo npm install pm2@latest -g
sudo npm install yarn -g

Setup Nginx (If not yet)

sudo apt install nginx

3. Setup & clone your repo in the server

sudo git clone https://gitlab.com/USERNAME/YOUR_REPO.git
cd YOUR_REPO
sudo yarn

4. Copy your .env local content into .env file in your server (create a .env file in the repo that you clone)

NODE_ENV=test
PROJECT_JWT_SECRET=top_secret
APPLICATION_DEBUG=false
PORT=3000
DATABASE_USERNAME=USERNAME
DATABASE_PASSWORD=PASSWORD
DATABASE_HOST=IP_OR_DOMAIN
DATABASE_PORT=PORT
DATABASE_NAME=DB_NAME
DATABASE_ENGINE=mysql
MAIL_HOST=mail.domain.my
MAIL_USERNAME=noreply@domain.my
MAIL_PASSWORD=passwordsadasd
MAIL_PORT=465

5. Start your app with pm2 engine

sudo pm2 start app.js --name=myapp

Your app should be up on localhost:3000, can test it with curl if you have something serve on the index /.. Should be able get enough response to get it going.

curl localhost:3000

6. Setup nginx proxy

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

Copy and paste below code:

server {
listen 80;
listen [::]:80;
index index.html index.htm index.nginx-debian.html;
server_name ip;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

Incase of new instance/server , make sure to delete any other config in /etc/nginx/sites-available and /etc/nginx/sites-enable

8. Make a symlink and test the conf

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enable/
sudo nginx -t
sudo systemctl reload nginx

9. Test your project availability on http://YOUR_IP , should get some response (again if you have something serve on the index /)

10. (optional incase want to use domain instead of ip) If all those above is ok, then point your domain here and sign your domain with let’s encrypt.

Point your domain to the server (follow the guide on your dns provider) (tips: put the TTL as low as possible)

After sometime validate your domain pointer already resolve to the address or not, can test on any online tools, my preference is https://www.nslookup.io/

After its pointed, fix the nginx config with the domain pointed.

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

Adjust the server_name ip to server_name domain and restart nginx

sudo nginx -t
sudo systemctl reload nginx

Fix your ufw firewall

sudo ufw enable
sudo ufw allow ‘Nginx Full’
sudo ufw allow OpenSSH

Test your domain in the browser if it can reach the server, if its still not reaching, reload your firewall

sudo ufw reload

Once its ok can setup our https with lets encrypt, installing certbot:

sudo apt install certbot python3-certbot-nginx

Adjust your firewall accordingly

sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'
sudo ufw reload

Sign your domain, (change example.com with your domain)

sudo certbot --nginx -d example.com

Follow the questions ask, its up to your decision on the options to choose (i choose 2 : redirect all to https)..

Thats all, hehe.. in the future i will add some more guides on how to incorporate buddy.works into the CICD pipelines to automatically watch changes in your repo and updates accordingly

--

--