Steps to deploy a Node.js app to DigitalOcean using PM2, NGINX as a reverse proxy and an SSL from LetsEncrypt
Sometimes we need to run multiple nodejs app on same server with different domain or sub domain like admin.domain.com, api.domain.com and also need to run both nodejs or reactjs app concurrently. We can do this using 2 things , first of all we need to install nginx in our server for reverse proxy to connect different domain, and for running multiple nodejs app concurrently we can use PM2 NodeJs Process Manager
- NodeJS
- Git
I will be using the root user, but would suggest creating a new user
sudo apt-get update
sudo apt-get install nginx
If you want to installed lates nodejs version or others version visit this url and there is available all of the version of nodejs . https://github.com/nodesource/distributions#debinstall
sudo nano /etc/nginx/sites-available/default
It will open a code nano code editor on server . Now delete all of the lines from here and then copy this below code and paste there and save.
# This will connect with your domain and please make sure in which port your app is running and change the port
server {
server_name domainname.com www.domainname.com;
location / {
proxy_pass http://localhost:5000; #whatever port your app runs on
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;
}
}
# Main Client Website
server {
server_name domain.xyz www.domain.xyz;
location / {
proxy_pass http://localhost:5000; #whatever port your app runs on
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;
}
}
# Backend API
server {
server_name api.domain.xyz;
location / {
proxy_pass http://localhost:3000; #whatever port your app runs on
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;
}
}
# Admin Dashboard
server {
server_name admin.domain.xyz;
location / {
proxy_pass http://localhost:5001; #whatever port your app runs on
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;
}
}
# Manager Dashboard
server {
server_name manager.domain.xyz;
location / {
proxy_pass http://localhost:5002; #whatever port your app runs on
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;
}
}
sudo service nginx restart
sudo service nginx reload
Install PM2 globally in your server then in your root directory create a configuration file called process.json and copy this code and paste it on that file and then save
{
"apps": [
{
"name": "Backend API",
"cwd": "./backend", // this is directory url of your app
"script": "npm start",
"env": {
"NODE_ENV": "production"
}
},
{
"name": "Admin Dashboard",
"cwd": "./admin", // this is directory url of your app
"script": "serve -s build -l 5001",
"env": {
"NODE_ENV": "production"
}
},
{
"name": "Manager Dashboard",
"cwd": "./manager", // this is directory url of your app
"script": "serve -s build -l 5002",
"env": {
"NODE_ENV": "production"
}
}
]
}
pm2 start process.json
Now visit your domain and check. Thank you