Google Cloud Platform (GCP) offers a variety of services that can be used to deploy web applications, including Django projects. In this tutorial, we’ll walk you through the steps to deploy a Django project on Google Cloud for free using Google App Engine’s free tier.
Prerequisites:
- A Django project ready for deployment.
- A Google Cloud account. If you don’t have one, sign up for a free tier here.
- Google Cloud SDK installed on your machine.
Step-by-Step Guide:
1. Set Up a Google Cloud Project:
- Go to the Google Cloud Console.
- Click on the ‘Select a Project’ dropdown, then ‘New Project’.
- Name your project and click ‘Create’.
2. Configure Google App Engine:
- In the Cloud Console, navigate to ‘App Engine’ and click ‘Create Application’.
- Select the region closest to your target audience.
- Choose the ‘Python’ environment and click ‘Next’.
3. Prepare Your Django Project:
- Make sure your Django project uses a
requirements.txt
file to list all dependencies. - Create an
app.yaml
file in your project's root directory with the following content:
runtime: python310
entrypoint: gunicorn -b :$PORT your_project_name.wsgi
beta_settings:
cloud_sql_instances: your_cloud_sql_instance
runtime_config:
python_version: 3
instance_class: F1
automatic_scaling:
min_instances: 0
max_instances: 2
target_cpu_utilization: 0.8
min_pending_latency: automatic
max_pending_latency: automatic
handlers:
- url: /static
static_dir: static/
- url: /.*
secure: always
redirect_http_response_code: 301
script: auto
Let’s break down the provided YAML configuration:
1. Runtime Configuration
runtime: python310
This specifies that the application will run using Python version 3.10.
2. Entrypoint
entrypoint: gunicorn -b :$PORT your_project_name.wsgi
This defines the command to start the application. It uses gunicorn
as the web server, binds it to the port specified by the $PORT
environment variable, and uses the WSGI application defined in.wsgi
of your project.
3. Beta Settings
beta_settings:
cloud_sql_instances: your_cloud_sql_instance
These are settings that are currently in beta and might not be fully supported.
This configuration line is specific to Google Cloud’s App Engine and relates to the integration of your application with Google Cloud SQL.
- Runtime Config
runtime_config:
python_version: 3
Configuration specific to the runtime. This specifies that the Python version being used is from the Python 3 series.
5. Instance Class
instance_class: F1
This sets the instance type to F1
, which is a specific class of compute instance on Google Cloud with its own resource allocation.
6. Automatic Scaling
automatic_scaling:
Configuration for how the application should scale based on traffic.
min_instances: 0
: The minimum number of instances that should be running.max_instances: 2
: The maximum number of instances that can be running.target_cpu_utilization: 0.8
: The application will scale up when the average CPU utilization of all running instances exceeds 80%.min_pending_latency: automatic
: The minimum amount of time an incoming request should wait in the pending queue. Here, it's set to be determined automatically.max_pending_latency: automatic
: The maximum amount of time an incoming request can wait in the pending queue. Here, it's set to be determined automatically.
7. Handlers
handlers:
Define how URLs should be handled.
- For static content:
- url: /static
static_dir: static/
This means that any URL path starting with /static
will serve static files from the static/
directory.
- For all other URLs:
- url: /.*
secure: always
redirect_http_response_code: 301
script: auto
Any URL (due to the wildcard /.***
) will always use HTTPS (secure: always
). If a request comes over HTTP, it will be redirected with a 301 status code. The script: auto
indicates that Google Cloud should automatically route the request to the appropriate script/service.
This breakdown should provide a clearer understanding of how the configuration works for deploying a Python application on Google Cloud.
4. Set Up a Cloud SQL Database (Optional):
- If your Django project uses a database, navigate to ‘SQL’ in the Cloud Console.
- Click ‘Create Instance’ and choose ‘PostgreSQL’ or your preferred database.
- Follow the prompts to set up the database.
- Update your Django project’s
DATABASES
setting to use the Cloud SQL instance.
5. Deploy Your Django Project:
- Open a terminal or command prompt.
- Navigate to your Django project’s directory.
- Authenticate with Google Cloud:
gcloud auth login
- Set your Google Cloud project:
gcloud config set project YOUR_PROJECT_ID
- Deploy your application:
gcloud app deploy
6. Access Your Deployed Django Project:
- Once the deployment is successful, you’ll receive a link to your live Django project. It will be in the format:
[https://YOUR_PROJECT_ID.appspot.com](https://your_project_id.appspot.com/)
Tips:
- Always test your Django project locally before deploying to ensure everything works as expected.
- Monitor your usage in the Google Cloud Console to ensure you don’t exceed the free tier limits.
- Consider setting up a custom domain for a more professional look.
Conclusion:
Deploying a Django project on Google Cloud is straightforward, especially with the free tier of Google App Engine. By following the steps above, you can have your Django application up and running on a robust and scalable infrastructure in no time. Happy coding!