Django 1.8 + GIS (postgis) + PostgreSQL 9.4 + Digital Ocean + Gunicorn + Nginx + Ubuntu 14.04 & Virtualenv. Complete Deployment Guide step by step.


Step 0 : Create droplet with django application image.

Step 1 :

Connect to Digital Ocean server :

ssh root@<ip_address>

Step 2 :

As Ubuntu 14.04 comes with postgresql 9.3, remove it completely and install 9.4

Steps are as follows :

sudo apt-get --purge remove postgresql\*
sudo aptitude update
sudo aptitude dist-upgrade
echo "deb trusty-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - | sudo apt-key add -
sudo aptitude update
## install 9.4
sudo aptitude install --with-recommends postgresql-9.4 postgresql-contrib-9.4 postgresql-server-dev-9.4


Step 3 :

Install pip, git , supervisor, postgresql 9.4 etc .

Steps are as follows :

sudo apt-get install python-pip python-dev libpq-dev postgresql-contrib git supervisor
sudo apt-get install python-imaging libjpeg8 libjpeg62-dev libfreetype6 libfreetype6-dev
sudo apt-get install postgresql-9.4-postgis python-psycopg2 postgresql-9.4 postgresql-server-dev-9.4


Step 4 :

set up virual env and git repository .

Steps are as follows :

cd /home/django/
sudo pip install virtualenv
virtualenv <prj_name>env
source <prj_name>env/bin/activate
git clone <git repo url> <prj_name>
cd <prj_name>
pip install -r requirements.txt  ## make sure pip install gevent
ln -s /usr/include/freetype2 /usr/include/freetype # if you are using pillow or pip

Step 5 :

GEOS, GIS, PROJ, GDAL installation. If you are not intend to use any of this libs, skip this step.

Steps are as follows :

sudo apt-get install binutils libproj-dev gdal-bin
sudo apt-get install python-gdal
# make temp directory ourside project directory and download below contents and install. 
# It may take more time . ## GEOS wget tar xjf geos-3.3.8.tar.bz2 cd geos-3.3.8 ./configure make sudo make install sudo ldconfig cd .. ## PROJ.4 wget wget tar xzf proj-4.8.0.tar.gz cd proj-4.8.0/nad tar xzf ../../proj-datumgrid-1.5.tar.gz cd .. ./configure make sudo make install sudo ldconfig cd .. ## GDAL wget tar xzf gdal-1.9.2.tar.gz cd gdal-1.9.2 ./configure make sudo make install sudo ldconfig cd .. ## PostGIS sudo apt-get install libxml2-dev wget tar xzf postgis-2.1.5.tar.gz cd postgis-2.1.5 ./configure make sudo make install sudo ldconfig cd .. ###

Step 6 :

Allow remote postgres connection if you are going to use pgadmin like tools.

Steps are as follows :

vim /etc/postgresql/9.4/main/postgresql.conf




vim /etc/postgresql/9.4/main/pg_hba.conf

host  all  all md5

service postgresql restart

Step 7 :

Set up db and its postgresql password

Steps are as follows :

sudo su - postgres
ALTER USER postgres with password <something_strong_password>;
\connect <db_name>;
CREATE EXTENSION postgis_topology;
CREATE EXTENSION postgis_tiger_geocoder;

Step 8 :

Migrate Django Models and final changes to file

Steps are as follows :

# change to prod db user password
# push all changes to live and pull @ live
python migrate auth
python migrate
python createsuperuser 
python collectstatic

Step 9 :

Nginx , gunicorn, & supervisor changes

Steps are as follows :

## if you have supervisor script for example cronjob then put its short link.
ln -s /home/django/<prj_dir_name>/jobs_supervisor.conf /etc/supervisor/conf.d/jobs_supervisor.conf

# do path changes in /etc/nginx/sites-enabled/django
# do path changes in /etc/init/gunicorn.conf

############## content of gunicorn.conf###############
description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectadly trigger a respawn

setuid django
setgid django
chdir /home/django

exec /home/django/<prj_name>env/bin/gunicorn \
    --name=<prj_name> \
    --pythonpath=<prj_name> \
    --bind= \
    --config /etc/gunicorn.d/ \

Step 10 :

Above steps has deployed your django project. Note down below quick steps for quick deployment when you make code change to your project.

Steps are as follows :

ssh root@<ip_address>
source /home/django/<prj_name>env/bin/activate
cd /home/django/<prj_name>
git pull origin master
python collectstatic
python migrate
cd ..
chown -R django:django <prj_name>
sudo service gunicorn restart
sudo service nginx restart
sudo supervisorctl update
/etc/init.d/supervisor force-reload
Current rating: 5


There are currently no comments

New Comment


required (not published)