Open In App

How to Deploy a Web Application on GCP?

Last Updated : 20 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Google Cloud Platform is one of the cloud service providers that provide various cloud services for a seamless experience. It provides various services like storage, networks, development tools, Analytics tools, infrastructure, and many more.

Benefits Of Deploying Web Applications on Google Cloud Platform

  • GCP provides high availability which makes the application provide services to users seamlessly.
  • It also provides scalability so that applications can scale depending on user traffic.
  • It provides various managed services for the deployment of web applications which helps in easy deployment.

App Engine: This is a platform as a service of GCP that provides a fully managed server-less platform for application deployment.

Django: Django is a Python web application framework for easy and scalable web application development.

How to Deploy Web Applications on GCP?

  • We are going to use a simple Django project. Below are views.py and urls.py files for the project.

GFG-Instance---ec2-3-85-221-32compute-1amazonawscom---Remote-Desktop-Connection-10-08-2023-14_37_17-(2)

  • We are not going to setup any database. Also we are not using any static files or media storage in this project . If you are going to use it configure the project accordingly using google cloud storage which is beyond the scope of this article.

GFG-Instance---ec2-3-85-221-32compute-1amazonawscom---Remote-Desktop-Connection-10-08-2023-14_38_47-(3)

  • We are not deploying in production so we are not using environment variables here . If you want to deploy as production build then make sure you are using GCP secret manager and using proper environment variables.
  • Now for deploying firstly download and install google cloud sdk on your machine . After successful installation you should see similar output by running “gcloud version” command.

GCP CLI

  • For django we will require production server like gunicorn so install gunicorn.
  • Google App Engine uses app.yaml file for configuring app engine instance so lets create app.yaml in folder where manage.py file is located. Insert following code in app.yaml file. Change ‘gfg.wsgi:application’ to <YOUR_APP_NAME>.wsgi:application .
runtime: python310
env: standard
entrypoint: gunicorn -b :8081 gfg.wsgi:application

handlers:
- url: /.*
script: auto

runtime_config:
python_version: 3

  • create a .gcloudignore file which specifies which files to be not copied to instance . The file should look like below
# .gcloudignore

.gcloudignore

# Ignore local .env file
.env

# If you would like to upload your .git directory, .gitignore file, or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore

# Python pycache:
__pycache__/

# Ignore collected static and media files
mediafiles/
staticfiles/

# Ignore the local DB
db.sqlite3

# Ignored by the build system
/setup.cfg
venv/

# Ignore IDE files
.idea/
  • Now initialize gcloud CLI if not already initialized by below command.
gcloud init
  • select your project after logging in or create a new one.
  • Now create a new app in app engine using below command . select region for application creation. This will create a new app in specified region.
gcloud app create
  • Now create a requirements.txt file for your project. For our project we are using below requirements.txt file with only two requirements.
Django==4.1.1
gunicorn==21.2.0
  • Run below command where app.yaml file is located which will deploy our django application .
gcloud app deploy
  • Review details press Y to continue . After successful deployment you will see below output .

Cloud App deploy

  • Now to go to browser and go to service url of application OR run below command .
gcloud app browse
  • This will open browser with app URL. If you see “Hello World” then congratulations your app is deployed successfully.

Deployed application

Troubleshooting A Deployed Web Application On GCP

  • If you get ‘502 Bad Gateway’ error go to logs explorer to view what is wrong .
  • If you cannot access the page make sure you have added ‘*’ to Allowed Hosts in settings.py . If you are deploying in production then add your app engine url to allowed hosts.
  • If there is any other error check your project for configurations and make necessary changes.

Conclusion

From this article we have learnt about deployment of web applications on google cloud platform. We have also seen step by step process to deploy an application .

Frequently Asked Questions

1. What Are Other Options To Deploy a Web Application In GCP ?

You can the other services like Google App Engine,Google Kubernetes Engine (GKE) , Google Cloud Run and so on to deploy the web application in gcp.

2. Why App Engine Is Better Option Over Other Services In GCP ?

GAE is a fully managed platform, which means that Google takes care of the underlying infrastructure.

3. How To Monitor Performance Of Web Application In GCP ?

Cloud monitoring will helps to monitor the logs and metrics of the application by keep tracking it.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads