Thursday, June 28, 2012

Django Deployment in Production by Sidharth Shah

  1. Create virtualenv with no site packages
  1. virtualenv mphoria-env --no-site-packages
  1. Use the virtualenv with
  1. source mphoria-env/bin/activate
  1. Checkout latest version of code
  1. svn checkout mphoriacatalog
  1. Install preqs
  1. CouchDB
  2. Memcache
  3. Nginx
  1. Install required modules using pip
  1. cd mphoriacatalog; pip install -r requirements.txt
  1. Initialize couchdb’s databases
  1. cd ..;cd couchdb-init;python
  1. Install couchapp utility (this will help up push our defined views onto couchdb)
  1. pip install couchapp
  1. Push all backed up views to couchdb
  1. couchapp push emailpasswds
  2. couchapp push meta
  3. cd productsmeta_user/; couchapp push meta 
  1. Update the domain of the app that we will running this off from (this is to avoid redirect issues)
  1. In under INSTALLED_APPS add  'django.contrib.admin',

  1. Under make sure the follwing lines are uncommented or exits

from django.contrib import admin
urlpatterns = patterns('',
    url(r'^admin/', include(,
  1. Run python syncdb. Its going to  start command prompt to create admin user name, which we’re going to use later to to change site name
  2. Run python runserver, log on to server with right IP:8000
  3. Enter credentials that we just created.
  4. Click on Sites, and change it to desired values. In this case
  5. Comment out the line in and  that we used to create admin interface.

  1. Update Nginx’s config
  1. In our project directory we have nginx’s desired config. Within server blog we need to add following. Make sure you replace root with right direcotry of your deployment as highlighted below

               location = /_.gif {
                location /static/ {
                autoindex    on;          
                root /home/sidharth/Code/odesk/mphoria/mphoriacatalog;
                # your standard Nginx config for your site here...
                location / {
                 proxy_pass        http://localhost:8000;
                 proxy_set_header  X-Real-IP  $remote_addr;
  1. After this run our django application using gunicorn
  1. cd mphoriacatalog; ~/mphoria-env/bin/gunicorn_django -w4
Alternatively as phase 2 of installation we can use supervisord that will restart unicorn automatically if required
  1. Supervisor is already installed when we did pip install -r requirements.txt
  2. Copy supervisord.conf from our home directory to /etc
  3. Check our config using supervisord -c /etc/supervisord.conf
  4. If needed modify the config (Hint: Change paths that are highlighted in bold below)

[inet_http_server]                          ; inet (TCP) server setings
port=                    ; (ip_address:port specifier, *:port for all iface)
logfile=/home/sidharth/logs/supervisord.log         ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=20MB                               ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=4                                 ; (num of main logfile rotation backups;default 10)
loglevel=debug                                       ; (log level;default info; others: debug,warn,trace)
pidfile=/home/sidharth/                                  ; (supervisord pidfile;default
nodaemon=false                                      ; (start in foreground if true;default false)
minfds=1024                                         ; (min. avail startup file descriptors;default 1024)
minprocs=200                                        ; (min. avail process descriptors;default 200)
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
directory = /home/sidharth/Code/latest/mphoriacatalog/
user = sidharth
command = /home/sidharth/Code/latest/mphoriacatalog/mphoriacatalog/
  1. You may also need to modify paths in files that contains command/params for our gunicon_django process  (Hint: Change paths that are highlighted in bold below)

  set -e
  LOGDIR=$(dirname $LOGFILE)
  # user/group to run as
  source ~/Code/mphoriacatalog/bin/activate
  test -d $LOGDIR || mkdir -p $LOGDIR
  exec gunicorn_django -b -w $NUM_WORKERS \
    --user=$USER --group=$GROUP --log-level=debug \
    --log-file=$LOGFILE 2>>$LOGFILE
  1. Start our supervisor process using
  1. supervisorctl
  2. if mphoriacatalog seems not to be running, use start all
  1. Upon successful execution you can log on to http://localhost:9001 and see web interface like following

No comments:

Post a Comment