Skip to content

Instantly share code, notes, and snippets.

Last active April 12, 2021 15:07
  • Star 39 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save panuta/3075882 to your computer and use it in GitHub Desktop.
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


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 ""

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

Test the key by

$ ssh -T

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


Example website is

Setup folder structure

Create folders

$ mkdir -p /web/
$ cd /web/
$ mkdir logs public_html source

Get source code

$ cd /web/
$ git clone[username]/[repository].git

Set owner to www-data

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

Setup virtualenv

$ cd /web
$ virtualenv

Config nginx

$ cd /etc/nginx/sites-available
$ vim

Add the following configuration to the file

server {
    listen          80;
    server_name     $hostname;
    access_log /web/;
    error_log /web/;

    location / {
        uwsgi_pass      unix:///run/uwsgi/app/;
        include         uwsgi_params;
        uwsgi_param     UWSGI_SCHEME $scheme;
        uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;

    location /static {
        root   /web/;

Then create a symlink

$ ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Then you may need to delete default site

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

Config uwsgi

$ vim /etc/uwsgi/apps-available/

Add the following configuration to the file


Then create a symlink

$ ln -s /etc/uwsgi/apps-available/ /etc/uwsgi/apps-enabled/

Restart server

$ service uwsgi restart
$ service nginx restart


Install PIL/Pillow

Install dependencies

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

Install Pillow

$ pip install Pillow
Copy link

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