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
After installation, open a terminal and log into your Heroku account you created earlier:
$ heroku login
Enter your Heroku credentials
Password (typing will be hidden):
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.
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 -
Add this file in your project root directory and specify the python version used in the project as given.
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.
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.
Import django_heroku in the settings file
git add .
git commit -m 'Changes'
git push heroku master
heroku ps:scale web=1
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