Lab Currently Closed Closed All Day on Tuesday (Summer Break) more »
Note: This document only applies to student groups with virtual hosts who have applied for apphosting. For normal user accounts or for groups without apphosting, you'll want to host with FastCGI instead. See our instructions for Django or Flask.
You will want to deploy your application using a virtualenv so that you can easily install and manage dependencies and versions.
Create a directory for your app to live in:
mkdir -p ~/myapp cd ~/myapp
Set up a virtualenv:
Activate the virtualenv:
You should do this step every time before running your app or managing installed packages.
Copy your code to
~/myapp/src or similar, and install any dependencies
We recommend using gunicorn to serve your application. After activating your
virtualenv, install it with
pip install gunicorn.
Note that you may see a warning about a syntax error. As long as the output ends in "Successfully installed gunicorn", it's safe to ignore this.
Create a file at
~/myapp/run with content like:
#!/bin/bash -e . ~/myapp/venv/bin/activate PYTHONPATH=~/myapp/src:$PYTHONPATH \ exec gunicorn -w 2 -b unix:/srv/apps/$(whoami)/$(whoami).sock \ --log-file - main:app
main:app with the module containing the app, and name of your app,
chmod +x ~/myapp/run
Test executing the run script. You should be able to access your website while running it (or see any errors in your terminal).
Cool, your app works. Set up systemd to supervise your app (so that it starts and restarts automatically).
Gunicorn will reload your app if you send it SIGHUP. You can teach systemd that
fact by adding the following line under
[Service] in your systemd unit file:
ExecReload=/bin/kill -HUP $MAINPID
and then running
systemctl --user daemon-reload. After that, you can use
systemctl to reload your app:
systemctl --user reload myapp
If you have a better way to host Python-based apps on the app server (or a suggestion for how we could improve this documentation), send us an email!