Deployment to PythonAnywhere

Overview

Full instructions follow, but here’s a high-level view.

First time config:

  1. Pull your code down to PythonAnywhere using a Bash console and setup a virtualenv
  2. Set your config variables in the postactivate script
  3. Run the manage.py migrate and collectstatic commands
  4. Add an entry to the PythonAnywhere Web tab
  5. Set your config variables in the PythonAnywhere WSGI config file

Once you’ve been through this one-off config, future deployments are much simpler: just git pull and then hit the “Reload” button :)

Getting your code and dependencies installed on PythonAnywhere

Make sure your project is fully commited and pushed up to Bitbucket or Github or wherever it may be. Then, log into your PythonAnywhere account, open up a Bash console, clone your repo, and create a virtualenv:

git clone <my-repo-url>  # you can also use hg
cd my-project-name
mkvirtualenv --python=/usr/bin/python3.5 my-project-name # or python2.7, etc
pip install -r requirements/production.txt  # may take a few minutes

Setting environment variables in the console

Generate a secret key for yourself, eg like this:

python -c 'import random; print("".join(random.SystemRandom().choice("abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)") for _ in range(50)))'

Make a note of it, since we’ll need it here in the console and later on in the web app config tab.

Set environment variables via the virtualenv “postactivate” script (this will set them every time you use the virtualenv in a console):

vi $VIRTUAL_ENV/bin/postactivate

TIP: If you don’t like vi, you can also edit this file via the PythonAnywhere “Files” menu; look in the “.virtualenvs” folder.

Add these exports

export DJANGO_SETTINGS_MODULE='<project_slug>.settings.production'
export PROD_DATABASE_URL='<see below>'
export DJANGO_SECRET_KEY='<secret key goes here>'
export DJANGO_ALLOWED_HOST_NAME='<www.your-domain.com>'
export EMAIL_HOST='email_host'
export EMAIL_FROM='support@host.com'
export EMAIL_USER='email_user'
export EMAIL_PASSWD='email_passwd'

Database setup:

Go to the PythonAnywhere Databases tab and configure your database.

  • For Postgres, setup your superuser password, then open a Postgres console and run a CREATE DATABASE my-db-name. You should probably also set up a specific role and permissions for your app, rather than using the superuser credentials. Make a note of the address and port of your postgres server.
  • For MySQL, set the password and create a database. More info here: https://help.pythonanywhere.com/pages/UsingMySQL
  • You can also use sqlite if you like! Not recommended for anything beyond toy projects though.

Now go back to the postactivate script and set the DATABASE_URL environment variable:

export DATABASE_URL='postgres://<postgres-username>:<postgres-password>@<postgres-address>:<postgres-port>/<database-name>'
# or
export DATABASE_URL='mysql://<pythonanywhere-username>:<mysql-password>@<mysql-address>/<database-name>'
# or
export DATABASE_URL='sqlite:////home/yourusername/path/to/db.sqlite'

If you’re using MySQL, you may need to run pip install mysqlclient, and maybe add mysqlclient to requirements/production.txt too.

Now run the migration, and collectstatic:

source $VIRTUAL_ENV/bin/postactivate
python manage.py migrate
python manage.py collectstatic
# and, optionally
python manage.py createsuperuser

Configure the PythonAnywhere Web Tab

Go to the PythonAnywhere Web tab, hit Add new web app, and choose Manual Config, and then the version of Python you used for your virtualenv.

NOTE: If you’re using a custom domain (not on *.pythonanywhere.com), then you’ll need to set up a CNAME with your domain registrar.

When you’re redirected back to the web app config screen, set the path to your virtualenv. If you used virtualenvwrapper as above, you can just enter its name.

Click through to the WSGI configuration file link (near the top) and edit the wsgi file. Make it look something like this, repeating the environment variables you used earlier:

import os
import sys
path = '/home/<your-username>/<your-project-directory>'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = '<project_slug>.settings.production'
os.environ['PROD_DATABASE_URL'] = '<as above>'
os.environ['DJANGO_SECRET_KEY'] = '<as above>'
os.environ['DJANGO_ALLOWED_HOST_NAME'] = '<as above>'
os.environ['EMAIL_HOST'] = '<as above>'
os.environ['EMAIL_FROM'] = '<as above>'
os.environ['EMAIL_USER'] = '<as above>'
os.environ['EMAIL_PASSWD'] = '<as above>'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Back on the Web tab, hit Reload, and your app should be live!

NOTE: you may see security warnings until you set up your SSL certificates. If you want to supress them temporarily, set DJANGO_SECURE_SSL_REDIRECT to blank. Follow the instructions here to get SSL set up: https://help.pythonanywhere.com/pages/SSLOwnDomains/

Static files

Essentially you’ll need an entry to match your STATIC_URL and STATIC_ROOT settings. There’s more info here: https://help.pythonanywhere.com/pages/DjangoStaticFiles

Future deployments

For subsequent deployments, the procedure is much simpler. In a Bash console:

workon my-virtualenv-name
cd project-directory
git pull
python manage.py migrate
python manage.py collectstatic

And then go to the Web tab and hit Reload

TIP: if you’re really keen, you can set up git-push based deployments: https://blog.pythonanywhere.com/87/

Inspired from Pythonanywhere deployment docs at https://cookiecutter-django.readthedocs.io/en/latest/deployment-on-pythonanywhere.html