Deploy python django app in heroku app
Mar 28, 2020 Durai Pandian
Heroku is a cloud platform which provides Platform as Service (PaaS) unlike Amazon Web Service (AWS) which is Infrastructure as a Service (IaaS). Heroku free tier provide free service with limitation. If you are planning to host for production in Heroku, you have to purchase Dynos. Dynos are something like docker container which is managed by Heroku.
Heroku removes the headache of maintaining Infrastructure by ourselves and letting us to focus on app development. I always use Heroku for prototyping app and get it visible quickly.
You can host up to five apps for free account. We will see how to deploy your Django app in Heroku by Heroku-cli and Git.
You can deploy your Django application in three ways.
- Deploying using Heroku-cli
- Deploying using GitHub
- Deploying using docker container
In this post, We will focus on how to deploy using GitHub and cli. My preferred method is using GitHub as I use GitHub for all my projects and It is easy to maintain as well. Both method requires creating git repository.
Regardless these two methods, app must be pre-configured to work with Heroku. We will see those deployment methods below.
Gunicorn and django_heroku packages needs to be installed for deploying in Heroku.
django_heroku - configures your django app for deploying in Heroku environment. It sets database URL from Heroku, environment variables set in Heroku.
gunicorn - is the application server for WSGI application.
(blog) ➜ thuruthuru git:(heroku_depolyment) pip install django_heroku gunicorn
Create requirements file
It is important to create requirements.txt file. Heroku will identity app as python app with requirements.txt file and install Django app dependencies. You can use below command to create requirements files.
(blog) ➜ thuruthuru git:(heroku_depolyment) pip freeze > requirements.txt
Open your settings.py from project directory and add below configuration at the end script. This sets database URL from Heroku, environment variables set in Heroku.
# Heroku deployment configuration
Procfile is important for deploying django application with gunicorn. Profile has gunicorn command to run the WSGI application. Since our application is Django web app. We need to define web app in Procfile. Replace YOUR_APP_NAME with your app name. Procfile should be without any extension.
web: gunicorn YOUR_APP_NAME.wsgi
So far what we have done will be general to heroku-cli and github deployment.
You need Git repo created and Heroku-CLI to be installed in your local machine. You will need Heroku-CLI to run migration scripts for Django. You can download Heroku client from official site. Login to Heroku account using command line with your email address.
(blog) ➜ thuruthuru git:(heroku_depolyment) ✗ heroku login
Creating Heroku app
You can create Heroku app from website and using CLI.
- Login to Heroku dashboard.
- Click New and Create New app.
- Provide unique app name to create.
You can skip this step if you already created your app. Replace YOUR_HEROKU_APP_NAME with your app name.
(blog) ➜ thuruthuru git:(heroku_depolyment) ✗ heroku apps:create YOUR_HEROKU_APP_NAME
Sample output of the above command.
Creating ⬢ thuruthuru... done
https://thuruthuru.herokuapp.com/ | https://git.heroku.com/thuruthuru.git
Deploying using Heroku-cli
Create GIT repo
If you haven't create a git repo, create git repository and add all the files to repository.
(blog) ➜ thuruthuru git:(heroku_depolyment) ✗ git init
(blog) ➜ thuruthuru git:(heroku_depolyment) ✗ git add .
(blog) ➜ thuruthuru git:(heroku_depolyment) ✗ git commit -m "initial commit"
Add Heroku repository to our local git to push code to Heroku remote repository.
(blog) ➜ thuruthuru git:(heroku_depolyment) ✗ heroku git:remote -a YOUR_HEROKU_APP_NAME
Above command output to my repo.
set git remote heroku to https://git.heroku.com/thuruthuru.git
Push code to Heroku
Once you mapped to your repository to Heroku remote repository, you can push to Heroku.
(blog) ➜ thuruthuru git:(heroku_depolyment) ✗ git push heroku master
Heroku needs master branch to deploy. If you are using different branch for deployment, you can map to master and push. Now I am pushing my heroku_deployment branch to master branch in Heroku.
(blog) ➜ thuruthuru git:(heroku_depolyment) git push heroku heroku_depolyment:master
As soon as you push the code to Heroku, Heroku identifies your app, installs dependencies and collectstatic. If you need any configurations from environment variables, collectstatic may fail as we didn't set any environment. We will set environment variables in next section and push the code again.
With this we have done our deployment with Heroku-CLI. Now we will see how to deploy from GitHub repo.
Setting Environment variables
- Go to your Heroku app dashboard. Click on your app and navigate to settings.
- Go to Config Vars and click Reveal Config Vars. Add your environment variables
- Now you deploy if you had failure while deploying using Heroku-CLI.
Deploying with GitHub
Let's say you have heroku_deployment repo in GitHub which you wish to deploy in Heroku.
- Go to deploy under your Heroku app and click "GitHub"
- Connect to your github repository.
- Once you connect your heroku app to github repository. You will have to options to deploy from.
1. Automatic deployment - automatically deploys your branch from github whenever you push new changes.
2. Manual deployment - You will have to pick branch to deploy.
Once the deployment is completed, you can run your python command as below.
Now application has been deployed to Heroku and accessible in the internet.
(blog) ➜ thuruthuru git:(heroku_depolyment) heroku run python manage.py migrate
Accessing Heroku App
URL will be your_heroku_app_name.herokuapp.com. You can get this link from app settings in Heroku website.
- You will be able to open your app from dashboard.
- You will be able to get the app url under settings from Domain.
Now your app will be ready to access online.
Debugging in Heroku
You will be able to view the logs of the deployment with below command in the shell.
(blog) ➜ thuruthuru git:(heroku_depolyment) heroku logs --tail --app YOUR_HEROKU_APP
You will be able to view same deployment logs in activity under your app. In case if you want to check your application errors, CLI will be helpful.
If your app needs user uploaded content to work, You need to use S3 for file storage. Heroku filesystem will not keep your media files, as free heroku account dyno shutsdown after some time of inactivity and redeployed when it becomes active. You can take a look at my other post to configure S3 for media files.
Enjoy your app now!! Let me know in comments if you follow some other strategy to deploy or have any other tips to share.