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. 

If curl is missing on your server you can install using below command

to check Node.Js version

Now move to loacation where you want to create project

Run below command to install NPM and Express one by one

Create project using Express generator

to debug application run below command in terminal

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:

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

Or if you create an app.js or any other file from where your application is launch you can add like this

Step 2. If you want list of managed application you can run below command

If you want to remove your application use below command

If you want to reload your application

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:

Add firewall to server and allow port you want open to access using below commands

Get lists of an available application run this command:

Allow any application to run on specific ports you can use below commands. We first reset all setting and allow one by one

You can run above commands one by one. To check status firewall run bellow command

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 

Step 2. Open file in editor and update its setting

Step 3. You can update or add below code in it

Step 4. Enable your website config file

 

Step 5. Restart your Nginx server using below command

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 

Hope this tutorial helps you. Please share and like our social page. 🙂