-
Notifications
You must be signed in to change notification settings - Fork 8
Deploying to Production
Justin Mi edited this page Apr 12, 2018
·
12 revisions
As of this writing, Malasakit is deployed on a production server that uses Apache. The following tasks should be completed in order the first time Malasakit is deployed:
- Create a virtual env
venvand runpip install -r requirements.txt. Also make sure you havenpminstalled. - Set the MySQL password as an environment variable
mysql_passin Apache'senvvarsand as an environment variable in your server. Access the variable insettings.pyasos.environ.get('mysql_pass'). - Set a
SECRET_KEYincafe/settings.py. You can generate a new secret key by creating a new temporary Django app and copying over the secret key from itssettings.js. - Initialize the
pcaridatabase in MySQL. This can be done withmake createproddb. Thepcaridatabase can be deleted withmake deleteproddb. Once the database exists, use./manage.py migrate --run-syncdbto initialize empty tables. See the instructions below for importing data from SQLite. - Create any superuser accounts needed for accessing the admin panel.
This can be done either from the admin panel by another superuser, or from the command line by using the
createsuperusercommand ofmanage.py. - Fill out all message files with translations.
Message files can be prepared with
make preparetransfrom the repository top level. - Create an config file on your HTTP server to direct requests to your app. Malasakit was originally deployed using Apache; here is an example config, assuming that the app is hosted on a parent domain (e.g.
www.parent-domain.com/malasakit):
WSGIDaemonProcess malasakit python-home=path/to/your/venv python-path=/path/to/project/root:/path/to/venv/lib/python2.7/site-packages
Alias /malasakit/static /path/to/static/directory
WSGIScriptAlias /malasakit /path/to/wsgi.py
<Location /malasakit>
WSGIProcessGroup malasakit
</Location>
- Configure the site to serve HTTPS only, and redirect all HTTP requests as HTTPS.
This is not only good security practice, but is needed for browsers to trust service workers so that Malasakit can run offline.
LetsEncrypt is a good place to start for obtaining an HTTPS certificate, and
certbotmakes configuration relatively painless. - Identify a static root, the filesystem path where all static files are stored.
The static root is controlled by the
STATIC_ROOTsetting incafe/settings.py. If you are serving Malasakit alongside other web applications, you may want to pool their static files together. By default, the static root iscafe/static.
Every time Malasakit is run, the following steps should be followed:
- Update the source tree (if the project is a
gitrepository, you can usegit pull origin master). Fix any merge conflicts. - Run
make deployfrom the project top level. This accomplishes several tasks:- Disables debug mode by setting
DEBUG = Falseincafe/settings.py.- Disables Django's static resource server, which is not robust enough for use in production (serve static resources through Apache itself by mapping the
STATIC_URLto theSTATIC_ROOT, and disable directory traversal). - Disables debug pages, which are served when an error is thrown. Debug pages in production are a security vulnerability because of the wealth of system information they provide.
- Enables HTML minification.
- Switches over to MySQL from SQLite.
- Only allows
opinion.berkeley.eduas an authorized host (theALLOWED_HOSTSsetting).
- Disables Django's static resource server, which is not robust enough for use in production (serve static resources through Apache itself by mapping the
- Minifies static assets as needed (for instance, running
lesscover LESS files). - Collects static files in
STATIC_ROOT. - Compiles translations for use in production.
- Disables debug mode by setting
- Set
APP_URL_ROOTinpcari/static/js/client.jsandURL_ROOTincafe/settings.pyto the base URL path the application is served from.
To import data from SQLite, temporarily set DEBUG = True (to use SQLite instead of MySQL), place db.sqlite3 into malasakit-django, then run
$ ./manage.py migrate
$ ./manage.py dumpdata --natural-primary --natural-foreign > data.json
Next, assuming you have an empty MySQL database pcari, switch off debug with DEBUG = False (to use MySQL), then run
$ ./manage.py loaddata data.json