Deployment - cyliang/covart-web GitHub Wiki
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 an empty database in any popular DBMS. For example:
- Host: localhost
- Database: covart
- Account: covart
- Password: yourpassword
- Charset: utf8mb4_unicode_ci
Write the following local settings in covart/local_settings.py
(create this file by yourself).
Get a secret key from here.
SECRET_KEY = 'your secret key'
DEBUG = False
Allowed hosts must be set.
ALLOWED_HOSTS = ['your host']
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',
},
}
}
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
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/')
Set this to understand this website with Google Analytics. You shall get a track ID first.
GOOGLE_ANALYTICS_ID = 'UA-XXXXXXX-Y'
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'
To connect this site with Google services, such as calendar:
- Get your Google app required permissions from here.
Required APIs are:
- Calendar API
- Gmail API
- Create a service account from here,
and download the credential to
covart/service_secret.json
.
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'
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'
Some email notification require target to be explicitly defined in the settings:
NOTIFICATION_EMAIL_TO = '[email protected]'
BASE_URL = 'https://example.com'
$ 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
$ python manage.py migrate
$ python manage.py createsuperuser
$ python manage.py collectstatic
Create the media directory and make that directory public. For example,
$ mkdir media
$ chmod a+rw media
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.
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.
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
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
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.
Skip this step if your system already has supervisord.
$ sudo apt-get install supervisor
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
$ sudo supervisorctl update
Please refer to this page.