Node to Google Cloud Compute Engine in 25 Minutes

Cole Murray
Jun 30, 2016 · 5 min read

Overview

  • Introduction
  • Setting Up Compute Engine
  • Node in Production
  • Closing Remarks + Next Steps

Introduction

I’m going to show you how to configure Google’s Compute Engine server to run your Node.js project in production in less than 30 minutes.

Prerequisites:

Things I won’t be demonstrating:

  • how to setup your node / react project
  • Gulp

For this tutorial, I’ll be working with my starter kit. It has react + express + es6 already configured.


Setting up Compute Engine

To get started, let’s create a new project on compute engine.

navigate to: https://console.cloud.google.com/iam-admin/projects

and click Create a New Project

After creating your project, navigate to Compute Engine in the Menu.

Here, we’ll create our VM.

Configuration:

Sweet! We have our VM configured ready for our project.

Install Node

For this tutorial, I’ll be using the web ssh client.

We need to install Node.js and Npm to setup and run our project. SSH into our machine and enter these commands:

After installing, confirm everything installed correctly. You should see output similar to below.

Clone and Setup Our Project:

We need to download our source code and task runner for our project.

After a minute or two, we’ll have all our dependencies and task runner, gulp, installed. Gulp is our task running we will be using to create our builds. Read more here https://github.com/gulpjs/gulp

Configure Nginx

Nginx will serve as our reverse proxy. This allows our node application to be accessed from port 80

we can test that it installed correctly by doing

We now need to configure nginx to serve as our reverse proxy for our node server.

Navigate to Nginx’s sites-available folder. This folder contains configurations for nginx and will be where we create our new configuration.

Optional: Backup your current default file

Create our new default file:

Now within our default file (/etc/nginx/sites-available/default):

Restart nginx

Great, now we’ve got nginx configured. Our next step is to setup node for production.


Node in Production

We want our node application to be able to handle any crashes and restart in the event of one. For this we’ll configure PM2. PM2 is a process manager that will allow our application to live forever.

Navigate back to our project:

Install PM2:

We’ll be using our pm2config to start our Node project. Note the production and production port.

react-express-starter-kit/pm2config.json:


Final Step:

We’ve setup nginx, setup our dependencies, created a config file for pm2. Our final step: build and start up node app.

Navigate to the IP Address of your server found here:

Navigating to the YOUR_SERVER_IP_Address above in our browser, we load our Hello World example.:

Now you may be wondering, “Pm2 will restart my process if my node server crashes, but what if my compute engine instance crashes?”

We’ll enter two more commands that will allow our server to restart in the event of a reboot:

That’s it! We’re done. Our server is now in production and will survive through any crashes / restarts. To confirm, let’s test it:

After our server reboots, we should be able to navigate back to our IP address and see our node server up and running.

Clean Up

To avoid billing charges, let’s clean up this example project.

Navigate to our menu bar and click Manage All Projects:

Select our project and delete it:

Closing Remarks:

In this tutorial we learned how to deploy a node application to production. We’ve used Nginx to reverse proxy our node server and PM2 to ensure it survives any crashes / restarts.

This is a great starting point into building a scalable production-ready application.

Potential Next Steps from Here:

Create Todo List App and Deploy it:

Setup a Database:

Secure our production App:

SSL: https://www.digitalocean.com/community/tutorials/how-to-create-an-ssl-certificate-on-nginx-for-ubuntu-14-04

Helmet: An node module to secure our Http Headers: https://github.com/helmetjs/helmet

Redux:


If you enjoyed this tutorial, please recommend, share, or comment below! As always, feel free to contact me on Twitter for problems, suggestions, chit-chat.

Google Cloud - Community

A collection of technical articles published or curated by…

Cole Murray

Written by

Machine Learning Engineer | Personalization @ Amazon | https://murraycole.com

Google Cloud - Community

A collection of technical articles published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

More From Medium

More from Google Cloud - Community

More from Google Cloud - Community

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade