Wagtail is a powerful and highly extensible open-source CMS based on Django web framework.
In this tutorial you will read how to prepare your project for production deployment and how to get your site deployed on AWS EC2 and Digital Ocean.
If you are looking for other deployment tutorials checkout out one of these:
- Deploy Django on Hetzner Cloud
- Deploy Django to Digital Ocean Droplet
- Deploy Django to AWS Lightsail
- Deploy Django to AWS EC2
- Deploy Django on Linode
- Deploy Django to Google Cloud Platform
In this post:
In order to create a blog project let's run these commands in terminal.
They will create a directory, python virtual environment and spin up a wagtail project from the template in the current directory.
mkdir myblog
cd myblog
python3 -m venv env
source env/bin/activate
pip install wagtail
wagtail start myblog .
Before we continue with installing dependencies open the requirements.txt
in your code editor and add 2 new lines: whitenoise
and django-environ
.
Your requirements.txt
would look like this:
Django>=4.0,<4.1
wagtail>=3.0,<3.1
whitenoise==6.5.0
django-environ==0.10.0
psycopg2-binary==2.9.6
gunicorn==21.2.0
Then run this to install all dependencies:
pip install -r requirements.txt
Now open the file myblog/settings/base.py
Find the MIDDLEWARE
and let's add whitenoise
middleware right after SecurityMiddlware
.
MIDDLEWARE = [
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware", # new
"wagtail.contrib.redirects.middleware.RedirectMiddleware",
]
Also add WHITENOISE_USE_FINDERS=True
line to the same file, you can add it to the end of the file.
Now open the myblog/settings/production.py
and add these lines to make sure our project respects environment variables when deployed in production.
We need to make sure that ALLOWED_HOSTS, SECRET_KEY and DATABASES are set correctly according to environment variables, because you never should hardcode any of those values and place them in version control.
Your myblog/settings/production.py
should now look like this:
import environ
import os
from .base import *
DEBUG = False
try:
from .local import *
except ImportError:
pass
env = environ.Env(
# set casting, default value
DEBUG=(bool, False)
)
SECRET_KEY = env('SECRET_KEY')
ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', default=['*'])
DATABASES = {
'default': env.db(),
}
In the root of our project, next to requirements.txt
create a file called Procfile
with the following content:
web: gunicorn myblog.wsgi:application --log-file -
Also create .gitignore
file, which is useful to avoid cluttering our git repository with files that shouldn't be committed to version control.
env/
.idea/
__pycache__/
*.py[cod]
*$py.class
.vscode/
.DS_Store
.AppleDouble
.LSOverride
Now initialize the repository, add and commit the code.
git init
git add .
git commit -m'Initial commit'
Create a GitHub or GitLab repository, add the remote and let's deploy it with Appliku.
Deploy Wagtail on AWS EC2
In order to deploy Wagtail project on AWS EC2 you need to generate AWS keys first. Follow instructions in this guide
After you have keys setup open Appliku dashboard and go to the "Servers" tab.
Select AWS provider.
Select the region, instance type and the disk size. To fit in AWS Free Tier make sure the disk size to be <30GB.
Click "Create EC2 Instance".
You will be taken to the server page where you will see the setup process. When it is finished, go to Applications tab and create a new app from GitHub (or GitLab if you hosted your repository there).
Deploy Wagtail on Digital Ocean
If you choose to go with Digital Ocean first make sure to create a DO account.
You will need to generate a token here https://cloud.digitalocean.com/account/api/tokens/new
Make sure to select scopes both "Read" and "Write". We recommend to setup expiration to "Never" or "1 Year" to avoid service interruptions.
In Appliku dashboard go to Servers tab and click "Setup Credentials" for Digital Ocean. Paste and save the token and go back to Servers tab. You will be able to select Digital Ocean provider now.
Select the region and Droplet Type. For Droplet Size we strongly recommend to pick at least 1GB RAM instance so your server can build and run your app and the database.
Click "Create Droplet".
Appliku will call Digital Ocean API and create a server for you and begin setup.
When that is finished you will be able to create the app and the database.
Deploy Wagtail with Appliku
Click on the "Applications" menu link.
If you haven't setup connection to will need to do that by clicking Setup Credentials.
When you are done you will be able to select the service you prefer. For this tutorial we'll go with GitHub
Give your app a name, select repository, branch and the server you have just created and click "Create Application".
Click on "Manage" in the databases block and add a database "Postgres 15" on the server we have just created.
Wait for it to get to "Deployed" status.
Go back to the application overview and click "Settings"
Go to the "Environment variables" tab. You see that our database's connection string is already there.
Click "+ Add" and add variables:
- "ALLOWED_HOSTS" must consist of the app name and ".applikuapp.com" so in our example it will be "wagtaildemo.applikuapp.com"
- SECRET_KEY: any random long string. Must be kept secret
- DJANGO_SETTINGS_MODULE must be set to myblog.settings.production
Now click "Save And Deploy"
You can go back to the application overview page and see that deployment has started.
The first deployment might take a bit longer, because the dependencies take some time to install at first run. Next deployments will take less time if you haven't changed requirements.txt
file.
Click on "View Application"
Congratulations! You have deployed your Wagtail app!