Last updated on November 24th, 2018 at 09:10 pm
In this tutorial, I’ll explain how you can set up setup a NodeJs Application from development mode to production using PM2 and Nginx. I think you already setup your NodeJs server. For this tutorial, I am using Digital Ocean Cloud . Now without wasting any time you simply follow below steps.
Node.Js Installation
Node.js installation is very simple in cloud, you need to login to your terminal and run below command.
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - sudo apt-get install -y nodejs
If curl is missing on your server you can install using below command
sudo apt-get install curl
to check Node.Js version
nodejs -v
Now move to loacation where you want to create project
cd /var/www/node
Run below command to install NPM and Express one by one
npm install npm install express --save npm install express-generator -g
Create project using Express generator
express --view=ejs strangerinchat.com cd strangerinchat.com && npm install
to debug application run below command in terminal
DEBUG=strangerinchat.com:* npm start
Now open a browser and run your application (https://YOUR_IP_ADDRESS:3001). If you see the Wxpress page it means every thing is working fine and now start installing PM2 as shown in next step.
PM2 Installation
PM2 is a process manager for Node.js applications. PM2 provides an easy way to manage and daemonize applications (run them as a service).
We will use Node Packaged Modules (NPM), which is basically a package manager for Node modules that installs with Node.js, to install PM2 on our app server. Use this command to install PM2:
npm install pm2 -g
Add your Application in PM2 to manage
Step 1. You need to add your application into PM2 manager if you are using express generator then you need to add your application like this
pm2 start ./bin/www
Or if you create an app.js or any other file from where your application is launch you can add like this
pm2 start app.js
Step 2. If you want list of managed application you can run below command
pm2 list
If you want to remove your application use below command
pm2 delete www
If you want to reload your application
pm2 reload www
If you want to learn more about PM2 command you can check here.
Nginx Installation
Now we can install Nginx server and use it as a reverse proxy server. To install Nginx server you need to run below commands.
To install Nginx on server run this command:
sudo apt-get install nginx
Add firewall to server and allow port you want open to access using below commands
Get lists of an available application run this command:
sudo ufw app list
Allow any application to run on specific ports you can use below commands. We first reset all setting and allow one by one
sudo ufw reset sudo ufw app list sudo ufw app info OpenSSH sudo ufw allow 22/tcp sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw allow 80/udp sudo ufw allow 443/udp sudo ufw disable sudo ufw enable
You can run above commands one by one. To check status firewall run bellow command
sudo ufw status
Setting virtual host in Nginx
Now you need to setup virtual host for your application in Nginx so your application is open when some one tries to open your domain. To setup virtual ost follow bellow setups
Step 1. Open terminal and copy default Nginx configuration to your new website in my case I already point my domain to my server it is very easy you simply need to add DNS information in your domain (strangerinchat.com). Now to copy default configuration run below command
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/strangerinchat.com
Step 2. Open file in editor and update its setting
sudo nano /etc/nginx/sites-available/strangerinchat.com
Step 3. You can update or add below code in it
server { listen 80; server_name strangerinchat.com; location / { proxy_pass http://YOUR_IP_ADDRESS:3001; 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; } }
Step 4. Enable your website config file
sudo ln -s /etc/nginx/sites-available/strangerinchat.com /etc/nginx/sites-enabled/
Step 5. Restart your Nginx server using below command
sudo systemctl restart nginx
Step 6. Try to open your web application by typing your domain on the address bar. If in case your site not open then try to allow port to access from anywhere you can do that using below command simply replace port with your application port
sudo ufw allow 3001/tcp
Hope this tutorial helps you. Please share and like our social page. 🙂