Skip to content

Instantly share code, notes, and snippets.

@greglinch
Forked from panuta/gist:3075882
Last active February 4, 2016 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greglinch/2b03e87ecc8d84790cc7 to your computer and use it in GitHub Desktop.
Save greglinch/2b03e87ecc8d84790cc7 to your computer and use it in GitHub Desktop.
How to setup Django server with virtualenv, virtualenvwrapper (instrux TK) and git on Ubuntu Server 14.04.3 LTS

Fix locale problem [I didn't do this]

Run the following command

$ sudo 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

$ sudo locale-gen

Install necessary packages

Update aptitude

$ sudo apt-get update
$ sudo apt-get upgrade

Install necessary packages

$ sudo apt-get install build-essential

Install Python

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

Setup Git

Install git

$ sudo apt-get install git-core

Generate a new SSH key

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

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

Test the key by

$ sudo ssh -T git@github.com

Install Virtualenv

Install virtualenv and virtualenvwrapper

$ sudo pip install virtualenv
$ sudo pip install virtualenvwrapper
  • Original author said: 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

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

Create a new database and database's user [NOT NEEDED bc db server is on RDS]

$ 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 [NOT NEEDED]

host electionresults mccelections mccelections-test-db.ckbwd6vwgqhm.us-west-2.rds.amazonaws.com md5

Reload Postgresql server

$ sudo /etc/init.d/postgresql reload

Install nginx

Setup aptitude

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

Install nginx

$ sudo apt-get install nginx-full

Install PCRE library (necessary for rewrite module)

$ sudo apt-get install libpcre3-dev

Install uWSGI

Install uWSGI

$ sudo apt-get install uwsgi uwsgi-plugin-python

Configuration

Example website is mcclatchy/electionsproject

Setup folder structure

Create folders

$ sudo mkdir -p /webapps/mcclatchy/electionsproject/
$ sudo cd /webapps/mcclatchy/electionsproject/
$ sudo mkdir logs public_html

Get source code

$ sudo cd /webapps/mcclatchy/electionsproject/
$ sudo git clone git@github.com:[username]/[repository].git

Set owner to www-data

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

Setup virtualenv [I did this differently bc of virtualenvwrapper]

$ sudo cd /webapps
$ sudo virtualenv electionsproject

Config nginx

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

Add the following configuration to the file

server {
    listen          80;
    server_name     mccelections-test.mcclatchydc.com;
    access_log /home/ubuntu/webapps/mcclatchy/electionsproject/logs/access.log;
    error_log /home/ubuntu/webapps/mcclatchy/electionsproject/logs/error.log;

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

    location /static {
        root   /home/ubuntu/webapps/mcclatchy/electionsproject/public_html/static/;
    }
}

Then create a symlink

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

Then you may need to delete default site

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

Config uwsgi

$ sudo vim /etc/uwsgi/apps-available/electionsproject.xml

Add the following configuration to the file

<uwsgi>
    <plugin>python</plugin>
    <vhost/>
    <socket>/run/uwsgi/app/electionsproject/electionsproject.socket</socket>
    <pythonpath>/home/ubuntu/webapps/mcclatchy/electionsproject/source/example/</pythonpath>
    <wsgi-file>/home/ubuntu/webapps/mcclatchy/electionsproject/source/example/example/wsgi.py</wsgi-file>
    <virtualenv>/home/ubuntu/webapps/mcclatchy/electionsproject</virtualenv>
    <logto>/home/ubuntu/webapps/mcclatchy/electionsproject/logs/electionsproject.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

$ sudo ln -s /etc/uwsgi/apps-available/electionsproject.xml /etc/uwsgi/apps-enabled/electionsproject.xml

Restart server

$ sudo service uwsgi restart
$ sudo service nginx restart

Optional

Install PIL/Pillow [I didn't do this]

Install dependencies

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

Install Pillow

$ sudo pip install Pillow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment