Skip to content

Setting up a production server

Özgün Babur edited this page Jul 25, 2022 · 10 revisions

Setting up a production server for CausalPath Newt webserver

This document describes how CausalPath webserver can be run in a production environment using PM2 and Nginx. We highly benefited from this guide during the preparation of this document.

Prerequisites

Follow the guidelines in the README to download and run the app in the environment that the server will be hosted.

These instructions are tested on a VM with Ubuntu 20.04.

Assuming the server code is checked out to

/home/sammy/Code/causalpath-newt-webserver

where sammy is your username and the directory is your current directory.

You have node version 14 installed under

/home/sammy/.nvm

The server runs fine on port 3000 with the below command

npm start

Now, stop the server with Ctrl+C and follow the below steps.

Step 1. Install PM2

npm install pm2@latest -g

Step 2. Run the app with PM2

pm2 start npm --name CausalPath -- start

Make sure the server is running without problems after this command on port 3000.

Check the PM2 status with

pm2 list

Now, if the app crashes, PM2 will restart it.

Step 3. Save the PM2 current state

pm2 save

After the save, PM2 can be recovered to this state using the resurrect command. Try it with.

pm2 kill
pm2 list

and then

pm2 resurrect
pm2 list

Step 4. Make PM2 a service.

Type

pm2 startup systemd

This will print a message on the shell similar to the below.

[PM2] Init System found: systemd
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/home/sammy/.nvm/versions/node/v14.20.0/bin /home/sammy/.nvm/versions/node/v14.20.0/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy

Copy and paste the last line (it will have your user name instead of sammy, and maybe other differences) on the shell.

sudo env PATH=$PATH:/home/sammy/.nvm/versions/node/v14.20.0/bin /home/sammy/.nvm/versions/node/v14.20.0/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy

After that, the PM2 service must start automatically when the system reboots. Test it with

sudo systemctl reboot -i

If the service fails, then edit the file

/etc/systemd/system/pm2-sammy.service

And change the line

After=network.target

into

After=multi-user.target

Try the reboot again.

Step 5. Install Nginx

sudo apt update
sudo apt install nginx

Step 6. Allow HTTP connection through Nginx

Type

sudo ufw app list

This will print available application profiles such as

Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

Allow HTTP by typing

sudo ufw allow 'Nginx HTTP'

Check if Nginx is up and running by

systemctl status nginx

Step 7. Use Nginx as a reverse proxy

Edit the file

/etc/nginx/sites-available/default

to make sure the location block has the below content

server {
...
    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;
    }
...
}

Make sure you didn't introduce any error after the edit by typing

sudo nginx -t

Restart Nginx by

sudo systemctl restart nginx

After this operation, you should be able to access CausalPath webserver through port 80.

Step 8. Check if it works

Reboot the system by

sudo systemctl reboot -i

and see if the server starts up automatically and port 80 works.