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.
How to Deploy a Web Application on GCP?
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.