Receive our weekly advanced programming tips for FREE
16.May
How to deploy django on a VPS server
0
| 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 https://github.com/geomin/djangovpshosting.git


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 djangovpshosting.com www.djangovpshosting.com; # put YOUR domain here e.x. example.com

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

#

daemon=/opt/djangovpshosting/env/bin/uwsgi

pid=/opt/djangovpshosting/djangovpshosting/run/uwsgi.pid

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

# Carry out specific functions when asked to by the system

case "$1" in

start)

echo "Starting djangovpshosting"

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

;;

stop)

echo "Stopping script djangovpshosting"

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

;;

reload)

echo "Reloading conf"

kill -HUP $(cat /opt/djangovpshosting/djangovpshosting/run/uwsgi.pid)

;;

*)

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

exit 1

;;

esac

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