Skip to content

Deploy Django with Nginx/Gunicorn/Virtualenv/Supervisor on Ubuntu

Notifications You must be signed in to change notification settings

nauhygon/dujiao

Repository files navigation

Deploy Django with Nginx/Gunicorn/Virtualenv/Supervisor on Ubuntu

A step-by-step guide

Author: Oak Nauhygon
Contact: nauhygon [at] gmail.com
Version: 0.0.1
Copyright: Public domain

Steps:

$ sudo apt-get install build-essential python-dev python-setuptools
$ sudo apt-get build-dep python-imaging
$ sudo easy_isntall virtualenv

Steps (sub /path/to/mywork with a valid one):

$ mkdir /path/to/mywork

Steps:

$ cd /path/to/mywork
$ virtualenv --no-site-packages --distribute myenv
$ cd myenv
$ . bin/activate

Steps:

$ pip install pinax yolk
$ pinax-admin setup_project myproj

$ cd myproj
$ python manage.py syncdb
$ python manage.py runserver 0.0.0.0:8000
# check http://<hostname>:8000

Steps:

$ pip install gunicorn
# Add gunicorn to INSTALLED_APPS in myproj/settings.py
   INSTALLED_APPS = {
        # ......
        gunicorn
        # ......
   }
$ python manage.py run_gunicorn --workers=2
# check http://localhost:8000 on localhost
# use "--bind 0.0.0.0:8000" to check http://<hostname>:8000 on a different machine

Steps:

$ sudo apt-get install nginx
$ sudo /etc/init.d/nginx start
# edit /var/www/ngnix-default/index.html and check http://<hostname>

# Insert below into /etc/nginx/sites-enabled/default

    upstream app_server_djangoapp {
       server localhost:8000 fail_timeout=0;
    }
    server {
       # ......
       location / {
           #root   /var/www/nginx-default;
           #index  index.html index.htm;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $http_host;
           proxy_redirect off;
           if (!-f $request_filename) {
               proxy_pass http://app_server_djangoapp;
               break;
          }
       }
        # ......
    }

$ sudo /etc/init.d/nginx restart
# check http://<hostname> (note now on regular port 80 instead of 8000!)

Steps:

$ sudo apt-get install supervisor

Steps:

$ cd /path/to/mywork/myenv/myproj/deploy

$ cat > run_gunicorn

#!/bin/bash

# A script to run myproj using Gunicorn

dir0=`pwd`

# get script dir
script=$0;
script_dir=`dirname $script`

# assuming dir structure: <venv>/<project>/deploy
venv=$script_dir/../..
proj=$script_dir/..

# start virtualenv
. $venv/bin/activate

# start server
cd $proj
python manage.py run_gunicorn --workers=2

# end virtualenv
deactivate

cd $dir0
^D

$ chmod +x run_gunicorn

$ cat > djangoapp.conf
[program:djangoapp]
command=/path/to/mywork/myenv/myproj/deploy/run_gunicorn
directory=/path/to/mywork/myenv/myproj
user=www-data
autostart=true
autorestart=true
redirect_stderr=True
^D

$ cd /etc/supervisor/conf.d
$ sudo ln -s /path/to/mywork/myenv/myproj/deploy/djangoapp.conf

$ sudo /etc/init.d/supervisor start
$ sudo supervisorctl
djangoapp RUNNING pid 953, uptime 0:01:01

About

Deploy Django with Nginx/Gunicorn/Virtualenv/Supervisor on Ubuntu

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages