How to Deploy a Web Application on GCP?
Last Updated :
20 Sep, 2023
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.
- 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.
- 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.
- 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 .
- 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.
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 .
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is an Operating System? An Operating System is a System software that manages all the resources of the computing device. Acts as an interface between the software and different parts of the computer or the computer hardware. Manages the overall resources and operations of the computer. Controls and monitors the execution o
5 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read