Deploying a Django app to production for the first time is not the easiest thing in this world.
In order to make this process more clear and less painful, I made this guide as small as possible to successfully deploy your app without losing the understanding of how it works.
Here is the basic workflow of the Django web-application in production:
[user's web-browser] <--> [web-server] <--> [wsgi-server] <--> [your app's code]
Let's roll 🚴♀️
- In your app's python environment install Gunicorn. It's a light-weight pure-Python WSGI(web server gateway interface)-server, a gateway between web-server and your application's code.
$ pip install gunicorn
Another WSGI-server's options you can get here
- There are several flavors to free host your Django app. The most popular nowadays are Amazon AWS, Pythonanywhere, and Heroku. Our choice today will be Heroku as it is specialized at web-apps and brings you an opportunity to deploy a production/dev/staging environment almost in a couple of clicks.
So let's pip install a utility that automatically configures your Django app to work with Heroku:
$ pip install django-heroku
import to your Django app's settings (
settings.py in the default case):
and activate Django-Heroku config at the end of the same settings-file:
- In your app's root folder create
runtime.txtfile with just one string, representing your Python's version
- Next, create
Procfilein the same directory with the string declaring how to deploy your app
web: gunicorn project_name.wsgi --log-file -
project_name is obviously your Django project's name
- Now let's review our Django
settings.pywith "security, performance, and operations in mind".
- Make sure that your
SECRET_KEYused in production isn't mentioned anywhere else and is not committed to a version control system.
DEBUG = False
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '[::1]']
Explicitly set sensitive data cookies transmitting over HTTPS:
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
Now you can run
python manage.py check --deploy
command and check if you can apply another security/performance tweaks, recommended by Django, to your app. These tweaks are different for each specific app.
DEBUG_PROPAGATE_EXCEPTIONS = True
settings.py to see Django's errors in Heroku logs
- Signup to Heroku
- Download/install Git if you didn't have it already
- Download/install Heroku CLI (if path error then add to path manually)
- After Heroku CLI setup opens new Terminal/Command Prompt window, go to your Django project's path and login to Heroku:
$ heroku login
- If you didn't have a Git repository initialized in your Django project's root dir, initialize it:
$ git init
*.pyc string to
.gitignore file (create if you don't have it yet), commit changes to local repo:
$ git add * $ git commit -m 'initial'
- Create your Heroku app
First of all you have to choose the region where you want to deploy the app:
$ heroku regions ID Location Runtime ───────── ─────────────────────── ────────────── eu Europe Common Runtime us United States Common Runtime dublin Dublin, Ireland Private Spaces frankfurt Frankfurt, Germany Private Spaces oregon Oregon, United States Private Spaces sydney Sydney, Australia Private Spaces tokyo Tokyo, Japan Private Spaces virginia Virginia, United States Private Spaces
$ heroku create app-name --region eu --buildpack heroku/python
Your app's url will be something like this
When you create an app, a Git remote (called
heroku) is also created and associated with your local Git repository.
- Deploy the app to Heroku:
$ git push heroku master
- Migrate the database:
$ heroku run python manage.py migrate
- Create the superuser:
$ heroku run python manage.py createsuperuser
- Optionally: if you have a "Missing staticfiles manifest entry for 'favicon.ico'" error on Django admin page bundle static files locally:
$ python manage.py collectstatic
add this to
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
and push changes to Heroku:
$ git push heroku master
Now you can watch your app in the browser:
$ heroku open
🎉 Congratulations! Your app's web-debut had happened successfully !🍹