Publishing Django App on Heroku
Heroku is a cloud application platform that provides hosting for many programming languages, including python. It is basically a Platform-as-a-Service (PaaS). There is a free plan on Heroku which offers you hosting your site, though very limited, but very good to get started and to host Django sites as a demo. Learn more about Heroku here.
The blog aims on publishing a Django app on Heroku.
Install Heroku Toolbelt
First, get yourself signed up on Heroku, sign up on Heroku. Signing up is necessary for hosting on Heroku. Next, Install the Heroku Toolbelt. It is a cli to manage your Heroku apps from the terminal.
After installation, open a terminal and log into your Heroku account you created earlier:
$ heroku login
Enter your Heroku credentials
Email: yourdomain@gmail.com
Password (typing will be hidden):
Authentication successful.
Configuring Django App
It is understood that you have a basic Django app to publish if you don’t have one follow below steps
Clone a sample app for deployment to Heroku, execute the following command
$ git clone https://github.com/heroku/python-getting-started.git
$ cd python-getting-started
Heroku requires some configuration files to deploy your app if you have cloned the starter app by Heroku they would already be there else you have to add them with the given steps.
These are:
1. Procfile
Add a Procfile in the root directory of your project to define process types and you need to explicitly declare what command should be executed to start your app.
$ touch Procfile
This will initiate an empty file Procfile (Execute it in your project root directory)
Add following line to Procfile
web: gunicorn djangoherokuapp.wsgi — log-file -
2. runtime.txt
Add this file in your project root directory and specify the python version used in the project as given.
python-3.7.12
3. requirements.txt
Add your app dependencies in this file, even if you don’t have any add an empty requirements.txt file in the project root directory.
django
gunicorn
django-heroku
Specifying version isn’t necessary but advised to add for the proper working of your app in case any module got some errors in the future version.
4.Settings (Optional)
Import django_heroku in the settings file
Deploy
heroku create
git add .
git commit -m 'Changes'
git push heroku master
heroku ps:scale web=1
heroku open
Things to keep in mind
Heroku is a great service but it’s expensive too. You may look at other services too. There are some limitations with the free tier of Heroku:
- The application sleeps after 30 minutes of inactivity. Thus, the first access after being inactive might be slow.
- The free PostgreSQL database has a limitation of 10K rows.
For more details, python and Heroku