Deployment - cyliang/covart-web GitHub Wiki

Deployment

This page describes how to deploy this website. Tasks mentioned in this page is required to be perform once. After the deployment, you shall initialize data for the website.

Create Database

Create an empty database in any popular DBMS. For example:

  • Host: localhost
  • Database: covart
  • Account: covart
  • Password: yourpassword
  • Charset: utf8mb4_unicode_ci

Local Settings

Write the following local settings in covart/local_settings.py (create this file by yourself).

Secret Key

Get a secret key from here.

SECRET_KEY = 'your secret key'

Turn Debug off

DEBUG = False

Allowed Hosts

Allowed hosts must be set.

ALLOWED_HOSTS = ['your host']

Database

Please refer to here for the detail.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'covart',
        'USER': 'covart',
        'PASSWORD': 'yourpassword',
        'HOST': '127.0.0.1',
        'OPTIONS': {
            'sql_mode': 'STRICT_TRANS_TABLES',
            'charset': 'utf8mb4',
        },
    }
}

Email Backend (SMTP Server)

Set this to have the functionality to send email. Please refer to here. Also, if you are using Gmail's SMTP server, you have to turn on access for less secure apps (Google Account settings, find Security -> Account permissions -> Access for less secure apps).

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'your gmail account'
EMAIL_HOST_PASSWORD = 'your gmail password'
EMAIL_USE_TLS = True

Static and Media Files Path

The path and URL for static and media files have to be identical to the one to be set in the web server configuration.

  • STATIC_ROOT: The directory path to store collected static files.
  • STATIC_URL: The URL to access static files.
  • MEDIA_ROOT: The directory path to store media files.
  • MEDIA_URL: The URL to access media files.
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

Google Analytics ID

Set this to understand this website with Google Analytics. You shall get a track ID first.

GOOGLE_ANALYTICS_ID = 'UA-XXXXXXX-Y'

Google Login

To enable Google login, create a Google project and get the OAuth2 credential as mentioned here. The redirect url shall look like:

http://your.url.root/oauth/complete/google-oauth2/

The Google Plus API also has to be enabled. Then, fill the API key and secret:

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'OAUTH2 KEY'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'OAUTH2 SECRET'

Google Services

To connect this site with Google services, such as calendar:

  1. Get your Google app required permissions from here. Required APIs are:
    1. Calendar API
    2. Gmail API
  2. Create a service account from here, and download the credential to covart/service_secret.json.

Slack Login

To enable Slack login, create a Slack app, get its client id/secret, and set the redirect URL to:

http://your.url.root/oauth/complete/slack/

Then, fill the API key and secret to covart/local_settings.py:

SOCIAL_AUTH_SLACK_KEY = 'client id'
SOCIAL_AUTH_SLACK_SECRET = 'client secret'

Slack Services

Get your Slack app's access token here, and fill it along with the channel ID you'd like to notify to covart/local_settings.py:

SLACK_TOKEN = 'your token'
SLACK_CHANNEL = 'G12345667'

Email Notification Target

Some email notification require target to be explicitly defined in the settings:

NOTIFICATION_EMAIL_TO = '[email protected]'
BASE_URL = 'https://example.com'

Create Virtual Environment and Install Dependent Packages

$ virtualenv venv
$ source venv/bin/activate
$ pip install -r requirements.txt

You also have to install the package required by the database backend you use. For example:

$ pip install mysqlclient

Migrate Database

$ python manage.py migrate

Create Super User

$ python manage.py createsuperuser

Collect Static Files and Create Media Directory

$ python manage.py collectstatic

Create the media directory and make that directory public. For example,

$ mkdir media
$ chmod a+rw media

Configure Web Server

We use Apache2 for example here. The following shows how to configure the WSGI module with daemon process in the Apache configuration file.

WSGIDaemonProcess covart.csie python-home=/path/to/covart-web/venv python-path=/path/to/covart-web processes=5 threads=4 display-name=%{GROUP}
WSGIProcessGroup covart.csie
WSGIScriptAlias / /path/to/covart-web/covart/wsgi.py
WSGIImportScript /path/to/covart-web/covart/wsgi.py process-group=covart.csie application-group=%{GLOBAL}

<Directory /path/to/covart-web/covart>
<Files wsgi.py>
    Require all granted
</Files>
</Directory>

Alias /media/ /path/to/covart-web/media/
Alias /static/ /path/to/covart-web/static/
Alias /robots.txt /path/to/covart-web/static/robots.txt
Alias /favicon.ico /path/to/covart-web/static/favicon.ico

<Directory /path/to/covart-web/static>
    Require all granted
</Directory>

<Directory /path/to/covart-web/media>
    Require all granted
</Directory>

The covart.csie is the name of the process group defined with WSGIProcessGroup. WSGIDaemonProcess defines how daemon processes shall be instantiated to run the WSGI application, where, in this example, there are 5 four-thread daemon processes. WSGIScriptAlias specifies a WSGI application (wsgi.py in this example) to handle an URL (/ in this example). WSGIImportScript preloads the WSGI application so it can have better performance to handle incoming requests. Check WSGI module's documentation for more detail.

Static files are served by the web server itself, so they can have shorter response time. Check Apache's documentation and Django's documentation for more information.

Make sure the web server (Apache) have correct encoding

To correctly handle file uploading for unicode file name, the web server shall have a utf-8 encoding configuration. This can be configured through the environment variable LANG of the web server. For Apache, /etc/apache2/envvars is the configuration file. Please refer to this document for more information.

Reload Apache Configuration

After configuring Apache, we have to reload Apache to apply the new configuration. After that, no more Apache reloading will be required until next re-configuration is made.

$ sudo service apache2 reload

Reload WSGI Application

To reload the application after an upgrade or modification to the application, just make the WSGI application newer than before through a touch. Apache will automatically detect it and perform the application reloading.

$ touch /path/to/covart-web/covart/wsgi.py

Start Django-Q Cluster

The command to start Django-Q cluster is python manage.py qcluster. The following is an example to use supervisord to start the cluster as a daemon process on Ubuntu. For usage of other process managers, please refer to here.

Install supervisord

Skip this step if your system already has supervisord.

$ sudo apt-get install supervisor

Configure supervisor for the Cluster

Create /etc/supervisor/conf.d/any_name_you_like.conf with privilege:

[program:any-name-you-like]
command = /path/to/covart-web/venv/bin/python manage.py qcluster
directory = /path/to/covart-web
stopasgroup = true
killasgroup = true

Update supervisor to Start the Cluster

$ sudo supervisorctl update

Initialize the Data

Please refer to this page.

⚠️ **GitHub.com Fallback** ⚠️