Skip to content

Instantly share code, notes, and snippets.

@hameedullah
Forked from panuta/gist:3075882
Created April 26, 2016 10:43
Show Gist options
  • Save hameedullah/1678cf57df75092236649aefbce4f908 to your computer and use it in GitHub Desktop.
Save hameedullah/1678cf57df75092236649aefbce4f908 to your computer and use it in GitHub Desktop.
How to setup Django server with virtualenv on Ubuntu Server 12.04

Fix locale problem

Run the following command

$ update-locale LC_ALL="en_US.UTF-8"

If it failed, you will need to add the following to /var/lib/locales/supported.d/local file

en_US.UTF-8 UTF-8

Then run

$ locale-gen

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 "[email protected]"

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

Test the key by

$ ssh -T [email protected]

Install Virtualenv

Install virtualenv

$ pip install virtualenv
  • I will not use virtualenvwrapper because I want to isolate everything about the website under one directory (in this case, 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.1/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

Setup aptitude

$ apt-get install python-software-properties software-properties-common
$ add-apt-repository ppa:nginx/stable

Install nginx

$ apt-get install nginx-full

Install PCRE library (necessary for rewrite module)

$ 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 [email protected]:[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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment