Skip to content

Instantly share code, notes, and snippets.

@ayushkr19
Last active December 9, 2016 20:43
Show Gist options
  • Save ayushkr19/b2ffaba77bd3183b5545 to your computer and use it in GitHub Desktop.
Save ayushkr19/b2ffaba77bd3183b5545 to your computer and use it in GitHub Desktop.
Server configuration for Django + Gunicorn + Nginx + Supervisor.

Server configuration for Django + Gunicorn + Nginx + Supervisor.

  • gunicorn_start - Bash script to start gunicorn which is serving our Django webapp. This script will be managed by 'supervisor' to make sure server starts again in case of failure or reboot.

  • spellplay - Configuration for nginx in sites-available directory. Necessary for static file handling, user media upload etc.

  • spellplay.conf - Configuration file for supervisor.

  • settings.py - Settings for the Django Project

  • dir_tree.txt - Directory structure for reference

Since the aim of this file is just to show the directory structure, some files have been removed
For example, the __pychache()__ directory, the virtualenv files, the static files etc
ayush@host:/$ tree webapps/
webapps/
`-- djangouser
|-- DjangoProjects
| `-- SpellPlay-Server
| `-- spellplay
| |-- manage.py
| |-- spellplay
| | |-- __init__.py
| | |-- settings.py
| | |-- urls.py
| | `-- wsgi.py
| |-- spellplayapp
| | |-- admin.py
| | |-- __init__.py
| | |-- migrations
| | | |-- 0001_initial.py
| | | |-- 0002_auto_20150829_2135.py
| | | |-- 0003_auto_20150829_2140.py
| | | |-- 0004_auto_20150829_2203.py
| | | `-- __init__.py
| | |-- models.py
| | |-- serializers.py
| | |-- tests.py
| | |-- urls.py
| | `-- views.py
| `-- static
| `-- admin
| |-- css
| |-- img
| `-- js
|-- gunicorn_start
|-- logs
| |-- gunicorn_supervisor.log
| |-- nginx-access.log
| `-- nginx-error.log
|-- run
| `-- gunicorn.sock
`-- virtualenv
|-- bin
| |-- activate
| |-- activate.csh
| |-- activate.fish
| |-- activate_this.py
| |-- django-admin
| |-- django-admin.py
| |-- easy_install
| |-- easy_install-3.4
| |-- gunicorn
| |-- gunicorn_django
| |-- gunicorn_paster
| |-- markdown_py
| |-- pip
| |-- pip3
| |-- pip3.4
| |-- __pycache__
| | `-- django-admin.cpython-34.pyc
| |-- python -> python3
| |-- python3
| `-- python3.4 -> python3
|-- include
|-- lib
`-- pip-selfcheck.json
19 directories, 42 files
#!/bin/bash
NAME="spellplay" # Name of the application
DJANGODIR=/webapps/djangouser/DjangoProjects/SpellPlay-Server/spellplay # Django project directory
SOCKFILE=/webapps/djangouser/run/gunicorn.sock # We will communicte using this unix socket
# We could also bind by IP, but the issue with that is that once bound, other services like nginx server can't listen on the same port.
# Thus for this case, sockfile has to be used
VIRTUALENVDIR=/webapps/djangouser/virtualenv # Directory for virtual environment
USER=djangouser # the user to run as
GROUP=webapps # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=spellplay.settings # which settings file should Django use
DJANGO_WSGI_MODULE=spellplay.wsgi # WSGI module name
SERVERIP=22.22.22.22:8010 # Server IP to be used to bind to gunicorn (Not used in this case)
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $VIRTUALENVDIR
source bin/activate
cd $DJANGODIR
export DJANGO_SETTINGS_MODULE=${DJANGO_SETTINGS_MODULE}
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
cd ..
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-
"""
Django settings for spellplay project.
Generated by 'django-admin startproject' using Django 1.8.4.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'li$#$p1338uruu5l!9^wyxpcs-elcf)b=_m0*9+106sw8mo^1e'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['22.22.22.22']
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'spellplayapp',
'rest_framework'
)
# Django Rest Framework specific. Not necessary otherwise
REST_FRAMEWORK = {
'PAGE_SIZE': 30
}
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'spellplay.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'spellplay.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
# Set STATIC_URL & STATIC_ROOT both.
# NOTE: Will have to run 'manage.py collectstatic' to populate static files directory
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
# File for nginx configuration in /etc/nginx/sites-available/ directory.
# Symlink has to be created of this file in /etc/nginx/sites-enabled directory to enable this.
# NOTE : Use 'sudo nginx -t' to verify for syntax errors.
# NOTE : In case of errors, 'sudo service nginx restart' would fail. For other cases, error logs can also be checked.
upstream spellplay_app_server {
# NOTE : Change the name for different app servers
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/webapps/djangouser/run/gunicorn.sock fail_timeout=0;
# server 22.22.22.22 fail_timeout=0; # Not used
}
server {
listen 8010; # The port to listen on
server_name 22.22.22.22;
client_max_body_size 4G;
# Log files
access_log /webapps/djangouser/logs/nginx-access.log;
error_log /webapps/djangouser/logs/nginx-error.log;
# This specifies the location of the static files
location /static/ {
alias /webapps/djangouser/DjangoProjects/SpellPlay-Server/spellplay/static/;
}
location /media/ {
alias /webapps/djangouser/DjangoProjects/SpellPlay-Server/spellplay/media/;
}
location / {
# an HTTP header important enough to have its own Wikipedia entry:
# http://en.wikipedia.org/wiki/X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if and only if you use HTTPS, this helps Rack
# set the proper protocol for doing redirects:
# proxy_set_header X-Forwarded-Proto https;
# pass the Host: header from the client right along so redirects
# can be set properly within the Rack application
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
# set "proxy_buffering off" *only* for Rainbows! when doing
# Comet/long-poll stuff. It's also safe to set if you're
# using only serving fast clients with Unicorn + nginx.
# Otherwise you _want_ nginx to buffer responses to slow
# clients, really.
# proxy_buffering off;
# Try to serve static files from nginx, no point in making an
# *application* server like Unicorn/Rainbows! serve static files.
if (!-f $request_filename) {
proxy_pass http://spellplay_app_server;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapps/djangouser/DjangoProjects/SpellPlay-Server/spellplay/static/;
}
}
; Configuration file for 'supervisor' in /etc/supervisor/conf.d/ directory.
; Commands to view existing supervisor apps 'sudo supervisorctl'
; Then use 'start/stop/restart appname' in the supervisor prompt.
[program:spellplay]
command = /webapps/djangouser/gunicorn_start ; Command to start app
user = djangouser ; User to run as
; NOTE: Create log file with 'touch' initially if required.
stdout_logfile = /webapps/djangouser/logs/gunicorn_supervisor.log ; Where to write log messages.
redirect_stderr = true ; Save stderr in the same log
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8 ; Set UTF-8 as default encoding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment