Deploy Wagtail/Django to Heroku with Github
Once you’ve created an awesome local website with wagtail or django framwork, you’ll want to make it pulic to everybody or somebody over the internet. There are dozens of hosting providers that actively support with django. I am not going to discuss pros and cons for different hosting vendors in this post. Heroku is the platform I choose for my little wagtail project so I’ll just focus on how to deploy wagtail project to heroku here.
A little bit different from heroku official django deployment guid, I am using github instead of heroku CLI for deployment. The reason for doing this is heroku CLI adds a new heroku remote repository named heroku
on the heroku cloud. When you want to deploy your site, you push your changes to the heroku repository. I’d like to keep my code on github so I prefer to use github for deployment too.
Create new wagtail/django project
Create virtual environment
Virtual environment is recommended for all python projects. So create an virtual environment with venv
first:
On windows:
|
|
On unix or mac:
|
|
Install wagtail
|
|
Generate starter site
|
|
mysite
is the project name and second mysite
is the root directory for the project. If you already inside mysite
folder, just type wagtail start mysite .
instead.django-admin startproject
instead of wagtail start
for new project generation.Getting wagtail site ready to deploy
Create github project
<username>
needs to be replaced with your github username.
<repo-name>
needs to be replaced with your own repo name.
Create a new repository on the command line
|
|
Or push existing repository from the commmand line
|
|
Check critical settings
The default project created are configured for development. Many of the settings should be different for production, either for security or performance reasons.
The critical settings are:
DJANGO_SETTINGS_MODULE
: This is used to specify which settings module to use. Default is development if not present.DEBUG
: This should be set asFalse
in production. This stops debugu trace information from being displayed.SECRET_KEY
: This is a string for CSRF protection. It is hardcoded in default development settings module and can be pushed to source control, but that’s a risk for production. Best practice is keep it in a server only file or server environment variable and never push it to the internet including cource control.DATABASE_URL
: Default SQLite database is file based thus cannot be used on heroku(and not recommend to use on any other hosting vendor), because it would be deleted from eht ephemeral file system(simillar to docker file system) every time the applicaton restarts.MIDDLEWARE
: We needwhitenoise
to serve static files.NoteHeroku automatically adds a postgresql database addon to your dyno and supplies database connection information to the web dyno using a configuration variable namedDATABASE_URL
.
We need to install additional packages for above chagnes:
|
|
Then update DATABASES
in base.py
settings:
|
|
Update requirements file for installed python packages:
|
|
Update production.py
Add following to production.py
(production settings module):
|
|
from __future__ import absolute_import, unicode_literals
line should be at the top of production.py
file, before every other import.Procfile and runtime
Procfile
and runtime.txt
are two files needed by heroku. They are just plain text files(without any extension) that specify how to create dyno and which version of python to use.
Create procfile
file and copy the following text into it:
|
|
Create runtime.txt
file and copy the following text into it:
|
|
Runtime list for python can be found here: heroku python runtimes
Commit changes and push to remote
Create a .gitignore
file if not exist. Add following to it:
|
|
Use git add -A
to add all files to git.
Use git status
to check that all files about to commit are correct.
Now we are ready to roll, commit the files to the local repository:
|
|
After that, push all previous changes to the github remote repository:
|
|
master
is the remote repository branch name, yours may be different.Connect heroku with github
Create heroku app
In your heroku dashboard create a new app:
Add github connection
Then go to Deploy
tab of newly created app, add github connection to it:
Add environment variable
Follow the instructions to add existing github project to heroku. Two more environment variables need to be added to our app. Go to settings
tab and expand Config Vars
:
Add DJANGO_SETTINGS_MODULE
and SECRET_KEY
:
Deploy
Now go to Deploy
tab and scroll all the way down, click Deploy Branch
button for deployment.
If everything works as expected, the app is now running on the heroku cloud, but it won’t working properly because we haven’t set the database yet. Like the local development, we need to perform database migration and creating superuser for this app.
Run python manage.py migrate
:
Run python manage.py createsuperuser
: