Receive our weekly advanced programming tips for FREE
How to deploy django on a VPS server
| 0 votes
Autor: bookkeeper
Chapter: Django
You build a great django website, now you need to deploy it to production. In this step-by-step tutorial you will learn how to properly deploy django on your own VPS.

Step #1

Get a reliable VPS Hosting provider A2Hosting (Affiliate link, is financing programmersbook)

Step #2

Configure your new VPS

Update the resources:

apt-get update

Remove all the unnecessary software:

apt-get -y remove apache2 bind9 postfix rpcbind

Let's install the software we need:

apt-get -y --force-yes install nginx python-virtualenv python-dev python-setuptools python-pip git git-core

Change to the opt folder

cd /opt

Clone example project

git clone

Create a virtualenv

virtualenv env

Activate the virtual enviroment

. env/bin/activate

Install all the python packages the easy way

pip install -r requirements.txt

Change to nginx sites-available folder

cd /etc/nginx/sites-available/

Create a new file

vim djangovpshosting.conf

Before you paste it press ‘a’ and to save ESC ‘:x’

server {

listen 80;

server_name; # put YOUR domain here e.x.

access_log /var/log/nginx/djangovpshosting.access.log;

error_log /var/log/nginx/djangovpshosting.error.log;

root /opt/djangovpshosting/djangovpshosting/;

location / {

uwsgi_pass unix://opt/djangovpshosting/djangovpshosting/run/uwsgi.socket;

include uwsgi_params;



Change to site-enabled folder

cd /etc/nginx/sites-enabled/

Create a soft link to your nginx conf file

ln -s ../sites-available/djangovpshosting.conf .

Reload nginx

service nginx reload

Create a script to control the django micro server

vim /etc/init.d/djangovpshosting

Before you paste press ‘a’ and to save press ESC and ‘:x’

#! /bin/bash

# /etc/init.d/djangovpshosting




args="-i /opt/djangovpshosting/djangovpshosting/conf/uwsgi.ini"

# Carry out specific functions when asked to by the system

case "$1" in


echo "Starting djangovpshosting"

start-stop-daemon -p $pid --start --chuid www-data --exec $daemon -- $args



echo "Stopping script djangovpshosting"

start-stop-daemon --signal INT -p $pid --stop $daemon -- $args



echo "Reloading conf"

kill -HUP $(cat /opt/djangovpshosting/djangovpshosting/run/



echo "Usage: /etc/init.d/djangovpshosting {start|stop|reload}"

exit 1



exit 0

Make the script executable

chmod +x /etc/init.d/djangovpshosting

For keep the files safe, change to user and group to www-data

chown -R www-data:www-data /opt/djangovpshosting/

To make sure that the django webserver starts automaticly change to folder /etc/rc2.d/

cd /etc/rc2.d/

Create a soft link to our script

ln -s /etc/init.d/djangovpshosting S18djangovpshosting

Start the django werb server!

/etc/init.d/djangovpshosting start