Deployment on Heroku
1. Introduction
Heroku is a cloud platform that allows developers to build, run, and operate applications entirely in the cloud. Deploying a Django application on Heroku is straightforward and involves several key steps. This tutorial will guide you through the entire process from start to finish.
2. Prerequisites
Before you start, ensure you have the following:
- A Heroku account
- Heroku CLI installed on your machine
- Git installed on your machine
- A Django project ready to be deployed
3. Setting Up Your Django Project
First, ensure your Django project is ready for deployment. This involves several configurations:
Example: settings.py
Update your settings.py
file:
import os import django_heroku # Other settings... # Activate Django-Heroku. django_heroku.settings(locals())
4. Creating a Procfile
Create a file named Procfile
in the root directory of your project. This file tells Heroku how to run your application.
Example: Procfile
web: gunicorn myproject.wsgi --log-file -
5. Installing Gunicorn
Gunicorn is a Python WSGI HTTP Server for UNIX. It is a pre-fork worker model, which means it forks multiple worker processes to handle multiple requests simultaneously.
Command to Install Gunicorn
pip install gunicorn
6. Creating a requirements.txt File
Create a requirements.txt
file in the root directory of your project. This file should list all the dependencies of your project.
Example: requirements.txt
Django==3.2.7 gunicorn django-heroku
7. Initialize a Git Repository
If you haven't already, initialize a Git repository in your project directory:
Git Commands
git init git add . git commit -m "Initial commit"
8. Deploying to Heroku
Login to Heroku using the Heroku CLI:
Command to Login
heroku login
Create a new Heroku app:
Command to Create Heroku App
heroku create my-django-app
Deploy your code to Heroku:
Git Commands to Deploy
git push heroku master
9. Running Migrations
Run the database migrations on Heroku:
Command to Run Migrations
heroku run python manage.py migrate
10. Accessing Your Application
Once the deployment is complete, you can access your application using the URL provided by Heroku:
Example URL
https://my-django-app.herokuapp.com/
11. Conclusion
Congratulations! You have successfully deployed your Django application on Heroku. With Heroku, you can easily manage and scale your applications with minimal effort.