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 https://mphoria.svn.beanstalkapp.com/src/mphoriacatalog/trunk 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 create_couchdbs.py
  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 http://127.0.0.1:5984/catalog_users
  2. couchapp push meta http://127.0.0.1:5984/searchmeta_users
  3. cd productsmeta_user/; couchapp push meta http://127.0.0.1:5984/productmeta_users 
  1. Update the domain of the app that we will running this off from (this is to avoid redirect issues)
  1. In settings.py under INSTALLED_APPS add  'django.contrib.admin',

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'frontend',
)
  1. Under urls.py make sure the follwing lines are uncommented or exits

from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
  1. Run python manage.py 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 manage.py runserver 0.0.0.0:8000, log on to server with right IP:8000
  3. Enter credentials that we just created.
  4. Click on Sites, example.com and change it to desired values. In this case agor.it
  5. Comment out the line in settings.py and urls.py  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 {
                empty_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=127.0.0.1:9001                    ; (ip_address:port specifier, *:port for all iface)
[supervisord]
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.pid                                  ; (supervisord pidfile;default supervisord.pid)
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)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
[program:mphoriacatalog]
directory = /home/sidharth/Code/latest/mphoriacatalog/
user = sidharth
command = /home/sidharth/Code/latest/mphoriacatalog/mphoriacatalog/start-django.sh
  1. You may also need to modify paths in start-django.sh files that contains command/params for our gunicon_django process  (Hint: Change paths that are highlighted in bold below)

#!/bin/bash
  set -e
  LOGFILE=/home/sidharth/logs/access.log
  LOGDIR=$(dirname $LOGFILE)
  NUM_WORKERS=3
  # user/group to run as
  USER=sidharth
  GROUP=sidharth
  source ~/Code/mphoriacatalog/bin/activate
  test -d $LOGDIR || mkdir -p $LOGDIR
  exec gunicorn_django -b 0.0.0.0:8000 -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