Configure Nginx Server + Reverse Proxy + Nodejs App + PM2 process manager + Google Cloud + Debian

Choosing a Web Server

I was trying to figure out which server i have to go for. It is no secret that Nginx consumes less memory and is faster than Apache. It uses asynchronous, event-driven architecture to handle the massive amount of connections. If you are more comfortable in using Apache it’s better to choose a slightly larger virtual machine.

Also read: Laravel Mix Vuejs component Syntax error

Installing Nginx

$ sudo apt-get update
$ sudo apt-get upgrade -y
$ sudo apt-get install nginx -y

downloads the package lists from the repositories and get information on the newest versions of packages and their dependencies & install Nginx

Setup Nginx

$ sudo systemctl status nginx    # To check the status of nginx
$ sudo systemctl start nginx # To start nginx

After installation, we can check the status of Nginx using the above commands. After successful running of Nginx try running your server IP address on the browser. If all is good we will get a screen like below

Nginx welcome screen

Setting up Nodejs

Now we need to install node & npm. Check whether it is already installed on the server using the following command for version check

$ node -v  # Checking nodejs version
$ npm -v # Checking npm version

If the above command fails we need to install node & npm using the following command and make sure it is installed by running the version command.

$ sudo apt-get install nodejs
$ sudo apt-get install npm

Yes!! our server is almost set. Now i need to clone my nodejs app repo to the server. For that we need to install git to the server then clone the project to /var/www/html/

$ sudo apt install git
$ git clone <Repo URL>

Voila!!

Ngnix & nodejs website is set up in our server.

The next step we need to look at is pointing this website ip to a domain. For that, we need to set up a reverse proxy. A reverse proxy is nothing but redirecting a domain to our particular root folder. To do that go to the given location

$ sudo nano /etc/nginx/sites-enabled/default

Now go to server block and start adding some tweaks. It will look something like this

server {
listen 80 default_server;
listen [::]:80 default_server;
 root /var/www/html;
 # Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
 server_name _;
location / {
proxy_pass http://localhost:8080;
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;
}

Need one more step to make our website working. Since here we are given the port number as 8080 in the nodejs project, we need to open a port in google cloud to allow the connection.

Go to google cloud console > VM instances > View network details (of one instance)

You will see a window like below. Click on Firewall and select create firewall menu

Here you will create a firewall with some name and give the source ip address to 0.0.0.0/0 so that it will allow default ports.

Add tcp port number as our 8080 port and click create. Now the port is open for use.

All set. Now for running your nodejs web app we need PM2 process manager. Let’s install

$ sudo npm install pm2 -g

Now you need to go project folder and start your server.js file using PM2.

$ pm2 start server.js

You will see a message saying started 8080, which means your server started working.

Hurraayy!!

Our website is live.!! Good luck with building your nodejs application. Let me know if you face any issues while building your application.

Leave a Reply

Your email address will not be published. Required fields are marked *