Update system

sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get clean

Install Admin Tools

sudo apt-get -y install unzip psmisc mlocate telnet lrzsz vim rcconf htop sudo p7zip dos2unix curl
sudo apt-get clean
sudo apt-get -y install gcc
sudo apt-get clean
sudo apt-get -y install build-essential libssl-dev libffi-dev libxml2-dev libxslt1-dev
sudo apt-get clean
sudo apt-get -y install libtiff5-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.6-dev tk8.6-dev python-tk
sudo apt-get clean

Git

sudo apt-get -y install git-core
sudo apt-get clean

Install Python and environment

sudo apt-get -y install python-dev
sudo apt-get clean
sudo apt-get -y install python-pip
sudo apt-get -y install python-virtualenv
sudo apt-get -y install python-pillow
sudo apt-get clean

Postgres Installation and settings

sudo apt-get -y install postgresql postgresql-contrib libpq-dev python-psycopg2
sudo apt-get -y install postgis
sudo apt-get clean

Creating the database and user

    • Switch to superuser postgres by
sudo su - postgres
    • Create user for the tool by
createuser --interactive -P
#The following questions shall be asked
#Enter name of role to add: jrcfloodtool
#Enter password for new role:
#Enter it again:
#Shall the new role be a superuser? (y/n) n
#Shall the new role be allowed to create databases? (y/n) n
#Shall the new role be allowed to create more new roles? (y/n) n
    • Now create database as
#jrcflood is the name of the database and jrcfloodtool is the owner
createdb --owner jrcfloodtool jrcflood
    • Logout from the postgres user
logout

Now create a virtual env for the jrcfloodtool

virtualenv jrcfloodtool_env

Workon the virtual env we just created

source jrcfloodtool_env/bin/activate

Make folder for the jrcfloodtool

cd /home
mkdir jrcfloodtool

Download source code from git if it is hosted or go create one

git clone git://github.com/your-repo.git jrcfloodtool
cd jrcfloodtool/

Install dependencies from the requirements.txt

pip install -r requirements.txt

Make changes in the settings

    • Make changes in the database settings
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # 'django.db.backends.sqlite3'
        'NAME': '',
        'USER': '',
        'PASSWORD': '',
        'HOST': '127.0.0.1', # Change this if your database is in remote server
        'PORT': '5432', # Change this if database is not in the default port
    }
}
	<li>Change the</li>

ALLOWED_URL
SECRET_KEY
DEBUG = False
  • Other changes as necessary

Verify the server is running by

#To end Ctrl + C
python manage.py runserver 0.0.0.0:8000

Now migrate the database

python manage.py migrate

Install application server

pip install gunicorn

Check if gunicorn is running well by

gunicorn jrcfloodtool.wsgi:application --bind 0.0.0.0:8001

Now make sh (or bash) script called outside from project to automate with gunicorn

cd ..
nano gunicorn_jrcfloodtool.bash

Edit according to your environment

#!/bin/sh

NAME=&quot;jrcfloodtool&quot; # Name of the application
DJANGODIR=/home/jrcfloodtool # Django project directory
SOCKFILE=/home/jrcfloodtool_env/run/gunicorn.sock # we will communicte using this unix socket
USER=ubuntu # the user to run as
GROUP=ubuntu # the group to run as
NUM_WORKERS=3 # how many worker processes should Gunicorn spawn; # usually is NUM_OF_CPU * 2 + 1
DJANGO_SETTINGS_MODULE=jrcfloodtool.settings # which settings file should Django use
DJANGO_WSGI_MODULE=jrcfloodtool.wsgi # WSGI module name
TIMEOUT=900

# Activate the virtual environment

cd $DJANGODIR
source /home/jrcfloodtool_env/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

# Create the run directory if it doesn't exist

RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn

exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--timeout $TIMEOUT \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-

Now make this script executable

sudo chmod u+x gunicorn_jrcfloodtool.sh

Now install supervisor

sudo apt-get -y install supervisor

Now create a supervisor conf file for the project

sudo nano /etc/supervisor/conf.d/jrcfloodtool.conf

And add the following bash script

[program:jrcfloodtool]
command = /home/gunicorn_jrcfloodtool.bash ; Command to start app
user = ubuntu ; User to run as
stdout_logfile = /home/logs/gunicorn_jrcfloodtool_supervisor.log ; Where to write log messages
redirect_stderr = true ; Save stderr in the same log
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8 ; Set UTF-8 as default encoding

Now create the required files and folder

sudo mkdir -p /home/logs/
sudo touch /home/logs/gunicorn_jrcfloodtool_supervisor.log

Make supervisor reread configuration files

For ubuntu 14.04

sudo supervisorctl reread
sudo supervisorctl update

For ubuntu 16.04

sudo systemctl restart supervisor
sudo systemctl enable supervisor

Start the supervisor service as

sudo supervisorctl start jrcfloodtool

To check the status of the service

sudo supervisorctl status jrcfloodtool

To stop the service

sudo supervisorctl stop jrcfloodtool

To restart the service

sudo supervisorctl restart jrcfloodtool

Install nginx

sudo apt-get -y install nginx

Make a conf file for nginx

sudo nano /etc/nginx/sites-available/jrcfloodtool.conf

Then add the following script to the conf file

upstream jrcfloodtool_server {
    server unix:/home/ubuntu/django_env/run/gunicorn.sock fail_timeout=0;
}

server {

    listen 80;
    name-of-dns-or-ip ;

    client_max_body_size 4G;

    keepalive_timeout 0;
    sendfile  on;

    proxy_connect_timeout 950s;
    proxy_read_timeout 1050s;

    access_log /home/logs/nginx-access.log;
    error_log /home/logs/nginx-error.log;

    location /static/ {
        alias /home/jrcfloodtool/static/;
    }

    location /media/ {
       alias /home/jrcfloodtool/media/;
    }

    location / {
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

       # enable this if and only if you use HTTPS
       # proxy_set_header X-Forwarded-Proto https;

       # pass the Host
       proxy_set_header Host $http_host;

       # No redirect
       proxy_redirect off;

       # Serve static files from nginx
       if (!-f $request_filename) {
          proxy_pass http://jrcfloodtool_server;
          break;
       }
    }

    # Error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /home/jrcfloodtool/static/;
    }
}

Make a soft link to the nginx conf

sudo ln -s /etc/nginx/sites-available/jrcfloodtool.conf /etc/nginx/sites-enabled/jrcfloodtool.conf

You can delete the default soft link in the sites-enabled as

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

start the nginx service

sudo service nginx start

Sometimes ngnix might not work, so consider restarting the service as well

sudo service nginx restart

NB: make sure the application, script and services have necessary permission to run

You can change permissions as

sudo chown -R -v your-user /your-folder

For static file settings in your nginx conf

  1. In your settings file, set the STATIC_ROOT setting to the directory from which you’d like to serve these files, for example:
STATIC_ROOT = &quot;/pathto-your-app/static&quot;
STATIC_URL = '/static/'

To generate static files, run the collectstatic management command:

python manage.py collectstatic

For media file settings in your nginx conf

  1. In your settings file, set the STATIC_ROOT setting to the directory from which you’d like to serve these files, for example:
MEDIA_URL = '/media/'
MEDIA_ROOT = &quot;/pathto-your-app/media&quot;

inspired from https://goo.gl/YyHssz