DevOps

How to Use Google Cloud to Host Your Web Application


Introduction

Google Cloud Platform (GCP) offers robust options for hosting web applications, catering to developers seeking scalability, flexibility and cost efficiency. Whether you’re deploying a simple static site or a dynamic Node.js app, GCP provides services like Compute Engine (virtual machines), App Engine (managed platform), and Cloud Run (serverless). This document guides you through hosting a web application on GCP using these methods, assuming a basic app (e.g., a Node.js Express server) as an example. By the end, you’ll have a deployed app accessible online from India or anywhere else.

Prerequisites

Before starting, ensure you have:

  • A GCP account (sign up at cloud.google.com) with billing enabled ($300 free credit for new users).

  • Google Cloud SDK installed (cloud.google.com/sdk).

  • A web app ready (e.g., Node.js with app.js, package.json).

  • Basic command line knowledge.

Method 1: Hosting with Virtual Machines

  1. Log into the GCP Console, go to Compute Engine > VM Instances.

  2. Click Create Instance:

    • Name: web-server.

    • Region: asia-south1 (Mumbai, for low latency in India).

    • Machine type: e2-micro (free tier eligible).

    • Boot disk: Ubuntu 22.04 LTS.

  3. Under Firewall, check “Allow HTTP traffic” and “Allow HTTPS traffic”.

  4. Click Create - your VM will spin up.

Step 2: Configure the VM

SSH into the VM via the Console’s “SSH” button or:

gcloud compute ssh web-server --zone asia-south1-a

Update the system:

sudo apt update && sudo apt upgrade -y

Install Node.js:

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -sudo apt install -y nodejs

Step 3: Deploy Your App

Transfer your app files (e.g., via gcloud compute scp or Git):

gcloud compute scp app.js package.json web-server:~/ --zone asia-south1-a

SSH back in, navigate to ~/, and install dependencies:

npm install

 

Start the app:

node app.js

Ensure your app listens on 0.0.0.0:80 (not localhost), e.g.:

const express = require('express');const app = express();app.get('/', (req, res) => res.send('Hello from GCP!'));app.listen(80, '0.0.0.0', () => console.log('Running'));

 

Step 4: Make It Persistent

Use a process manager like PM2:

sudo npm install -g pm2pm2 start app.js --name "web-app"pm2 startuppm2 save

 

Open port 80 in GCP Firewall if needed (Networking > Firewall).

Step 5: Access Your App

  • Get the VM’s external IP from the Console (e.g., 34.93.XX.XX).

  • Visit http://34.93.XX.XX in a browser. Done!

     

Method 2: Hosting with App Engine (Managed Platform)

Step 1: Prepare Your App

Add an app.yaml file in your app directory:

runtime: nodejs20env: standardinstance_class: F1handlers:- url: /.*  script: auto

 

Ensure a start script in package.json:

"scripts": {  "start": "node app.js"}

 

Step 2: Deploy to App Engine

From your app directory, authenticate:

gcloud auth login

Set project:

gcloud config set project YOUR_PROJECT_ID

 

Deploy:

gcloud app deploy

Confirm region (e.g., asia-south1) when prompted.

Step 3: Verify Deployment

Run:

gcloud app browse

Your app is live at https://YOUR_PROJECT_ID.asia-south1.appengine.cloud.

Method 3: Hosting with Cloud Run (Serverless)

Step 1: Containerize Your App

Create a Dockerfile:

FROM node:20WORKDIR /appCOPY package*.json ./RUN npm installCOPY . .EXPOSE 8080CMD ["npm", "start"]

 

Update app.js to use port 8080 (Cloud Run default):

app.listen(8080, '0.0.0.0');

 

Step 2: Build and Deploy

Build the container locally:

docker build -t gcr.io/YOUR_PROJECT_ID/web-app .

Push to Google Container Registry:

docker push gcr.io/YOUR_PROJECT_ID/web-app

Deploy to Cloud Run:

gcloud run deploy web-app \  --image gcr.io/YOUR_PROJECT_ID/web-app \  --platform managed \  --region asia-south1 \  --allow-unauthenticated

 

Get the URL from the output (e.g., https://web-app-XXXX.asia-south1.run.app).

 

Scaling and Cost Considerations

  • Compute Engine: Manual scaling, free tier up to 1 micro VM, then ~$5/month for e2-micro.
  • App Engine: Autoscales, free tier up to 28 instance hours/day, then usage-based (~$0.05/hour).
  • Cloud Run: Fully serverless, pay-per-request (first 2M requests free, then $0.40/M).

 

Troubleshooting

  • SSH Fails: Check firewall rules (gcloud compute firewall-rules list).
  • App Not Loading: Verify ports, logs (gcloud app logs tail), or container health.
  • Permissions: Run gcloud auth configure-docker if pushing to GCR fails.

 

Which AWS Hosting Type Should You Choose?

Hosting a web app on GCP offers flexibility across Compute Engine’s control, App Engine’s ease and Cloud Run’s serverless efficiency. For a beginner in India, App Engine is ideal for quick setups with minimal management, while Cloud Run suits modern, containerized apps with cost savings. Compute Engine fits custom needs but requires more upkeep. Choose based on your app’s complexity and budget, and leverage GCP’s Mumbai region for low latency. With this guide, your app can be live globally in hours.

 

Ready to transform your business with our technology solutions? Contact Us  today to Leverage Our DevOps Expertise. 

0

Devops

Related Center Of Excellence