Deploying Node.js application on GCP

Ishaan Ohri
ADGVIT
Published in
5 min readJan 16, 2022

What we’ll be doing?

Node.js since its inception in 2009 as a server-side language has gained immense popularity due to its quick response time and its scalable network architecture. Although it’s quite easy to use, deploying it along with the other essential tools can sometimes be tricky.

Here, we’ll be deploying the Node.js application over a Virtual Machine on GCP along with essential tools and add ons like:

  • Web-server/ HTTP load-balancer (Nginx in our case)
  • SSL certificate
  • Package manager (pm2 in our case)

Let’s get started

1. Creating a VM on GCP

  • Open the navigation menu and select Compute > Compute Instance.
  • Click on Create and enter a name for the instance. Select a suitable machine configuration (I have selected f1-micro under N1). Select Ubuntu 18.04 LTS for the boot disk.
  • Under Firewall, check Allow HTTP traffic and Allow HTTPS traffic.
  • Click on Done and then Create.

2. Installing Node.js

Installing Node.js and npm onto the machine will be the initial steps. SSH into the VM.

Run the following commands:

Just to check everything is installed correctly, type in the following commands to check the version of node and npm.

Check Node.js version:

$ node -v

Check NPM version:

$ npm -v

You should expect an output as below:

3. Cloning the repository from GitHub

Here I’ll be using a basic Node.js server for demonstration purposes.

4. Installing and Configuring Nginx

We’ll be using Nginx as the reverse proxy. Initially, the application will be accessible via port 80, but later we’ll configure SSL and we’ll use port 443 instead of port 80.

Run the following command to install Nginx:

$ sudo apt-get install -y nginx

Just to check whether Nginx is correctly installed, run the following command:

$ curl localhost

You should be able to see some HTML text, which is the home page of Nginx.

Now we’ll configure Nginx according to our requirement. Navigate to the sites-available folder by running the following command:

$ cd /etc/nginx/sites-available

Open the default file:

$ sudo vim default

Clear the entire file. Place the following code in the file. The server_name can be either the IP address of the machine or the domain name. In proxy_pass, the port is the port on which the application is running.

Now restart Nginx to check that everything is correct:

$ sudo service nginx restart

If you see any errors, double-check for any missed semicolons or parentheses.

5. Configuring SSL certificate

To configure an SSL certificate you will require a domain name. I use this website to obtain free domains for testing purposes.

Make sure to add the DNS records before proceeding with this step.

We’ll be using the Let’s Encrypt SSL certificate and Certbot to install the certificate.

Execute the following commands to install Certbot and obtain an SSL certificate:

Enter the details as prompted.

Copy the path to the certificate and key file.

Now change the content of the default file in /etc/nginx/sites-available

Now restart Nginx again to apply the changes:

$ sudo service nginx restart

6. Starting application with pm2

Now although the application can be started by the basic npm start command, we want our application to restart in-case of any crash or reboot. For this, we’ll be using pm2, which is a process manager.

Install pm2 as a global package:

$ sudo npm install -g pm2

Navigate back to your project:

$ cd ~/VM-Deployment

Generate pm2 process file:

$ pm2 ecosystem

This will generate ecosystem.config.js

Open the file:

$ vim ecosystem.config.js

Clear the file and place the following code in the file.

Here name is the name of your process (it can be anything unique on the machine), script is the path to your starting script, env is the list of environment variables.

To start the application:

$ pm2 start ./ecosystem.config.js

Now, pm2 will restart your application in case of any crash but to enable it to restart in case of reboot run the following commands:

$ sudo pm2 startup
$ pm2 save

In order to view any logs from your application, execute the following:

pm2 logs nodejs-sample

Here nodejs-sample is the name of the application in ecosystem.config.js file. Change it if you have used a different name.

Conclusion

Now the Node.js application is setup on our VM and is available via port 443 where Nginx is acting like reverse proxy and pm2 restarts the application in case of crash or reboot. Now to test, navigate to your IP/domain via any browser and you should see the application running:

PS: To avoid unnecessary billing on my account, I’ll be deleting the VM right after completing the blog.

Cheers!

Below is the repository which I used through this article:

--

--

Ishaan Ohri
ADGVIT
Writer for

Executive Head @ ADG VIT | Node JS Developer | Android Developer