Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
How to setup Django server with virtualenv on Ubuntu Server 12.04

Fix locale problem

Open file /etc/default/locale to add or change LC_ALL to the following

LC_ALL="en_US.UTF-8"

Then logout and login again.

Install necessary packages

Update aptitude

$ apt-get update
$ apt-get upgrade

Install necessary packages

$ apt-get install build-essential

Install Python

$ apt-get install python2.7-dev python-setuptools
$ easy_install pip

Setup Git

Install git

$ apt-get install git-core

Generate a new SSH key

$ ssh-keygen -t rsa -C "your_email@youremail.com"

Then add the key from ~/.ssh/id_rsa.pub to GitHub as a deployment key

Test the key by

$ ssh -T git@github.com

Install Virtualenv

Install virtualenv

$ pip install virtualenv
  • I don't like using virtualenvwrapper in production server because I want to isolate everything for the web under one directory (in this case, all Python libraries).

Install Postgresql Server

Install Postgresql Server and Python library

$ apt-get install postgresql python-psycopg2 libpq-dev

Create a new database and database's user

$ sudo -u postgres createdb [database_name]
$ sudo -u postgres createuser -SDRP [database_user_name]

Config pg_hba.conf file (you may need to change version number)

$ sudo vim /etc/postgresql/9.3/main/pg_hba.conf

Then add the following to pg_hba.conf file let database user authenticated using password

local [database_name] [database_user_name]  md5

Reload Postgresql server

$ /etc/init.d/postgresql reload

Install nginx

Install nginx

$ apt-get install nginx

Install PCRE library (necessary for rewrite module, if you need it)

$ apt-get install libpcre3-dev

Install uWSGI

Install uWSGI

$ apt-get install uwsgi uwsgi-plugin-python

Configuration

Example website is example.com

Setup folder structure

Create folders

$ mkdir -p /web/example.com/
$ cd /web/example.com/
$ mkdir logs public_html source

Get source code

$ cd /web/example.com/source/
$ git clone git@github.com:[username]/[repository].git

Set owner to www-data

$ chown -R www-data:www-data /web

Setup virtualenv

$ cd /web
$ virtualenv example.com

Config nginx

$ cd /etc/nginx/sites-available
$ vim example.com

Add the following configuration to the file

server {
    listen          80;
    server_name     $hostname;
    access_log /web/example.com/logs/access.log;
    error_log /web/example.com/logs/error.log;

    location / {
        uwsgi_pass      unix:///run/uwsgi/app/example.com/example.com.socket;
        include         uwsgi_params;
        uwsgi_param     UWSGI_SCHEME $scheme;
        uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
    }

    location /static {
        root   /web/example.com/public_html/static/;
    }
}

Then create a symlink

$ ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

Then you may need to delete default site

$ rm /etc/nginx/sites-enabled/default

Config uwsgi

$ vim /etc/uwsgi/apps-available/example.com.xml

Add the following configuration to the file

<uwsgi>
    <plugin>python</plugin>
    <vhost/>
    <socket>/run/uwsgi/app/example.com/example.com.socket</socket>
    <pythonpath>/web/example.com/source/example/</pythonpath>
    <wsgi-file>/web/example.com/source/example/example/wsgi.py</wsgi-file>
    <virtualenv>/web/example.com</virtualenv>
    <logto>/web/example.com/logs/example.com.log</logto>
    <master/>
    <processes>4</processes>
    <harakiri>60</harakiri>
    <reload-mercy>8</reload-mercy>
    <cpu-affinity>1</cpu-affinity>
    <stats>/tmp/stats.socket</stats>
    <max-requests>2000</max-requests>
    <limit-as>512</limit-as>
    <reload-on-as>256</reload-on-as>
    <reload-on-rss>192</reload-on-rss>
    <no-orphans/>
    <vacuum/>
</uwsgi>

Then create a symlink

$ ln -s /etc/uwsgi/apps-available/example.com.xml /etc/uwsgi/apps-enabled/example.com.xml

Restart server

$ service uwsgi restart
$ service nginx restart

Optional

Install PIL/Pillow

Install dependencies

$ apt-get install libjpeg62-dev zlib1g-dev libfreetype6-dev liblcms1-dev

Install Pillow

$ pip install Pillow

Thanks a lot .i was stuck in installing the pillow in virtual environment but now i am sorted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment