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.
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.
Log into the GCP Console, go to Compute Engine > VM Instances.
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.
Under Firewall, check “Allow HTTP traffic” and “Allow HTTPS traffic”.
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-aUpdate the system:
sudo apt update && sudo apt upgrade -yInstall Node.js:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -sudo apt install -y nodejsStep 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.jsEnsure 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!
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 deployConfirm region (e.g., asia-south1) when prompted.
Step 3: Verify Deployment
Run:
gcloud app browseYour app is live at https://YOUR_PROJECT_ID.asia-south1.appengine.cloud.
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-appDeploy 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).
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.
Contact Us