Login
main >   new_website_server >  


https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

Update hosts:
C:\Windows\System32\drivers\etc\hosts
178.79.185.172 cvauto-updated.co.za
178.79.185.172 www.cvauto-updated.co.za

Install Putty

Install VS Code.

Setup VS Code extensions:
Python Extenion for Visual Studio Code
PHP Extension pack
VS Code Javascript (ES6) snippets
Django extension for Visual Studio Code
Tabnine

Install SSH access

WinSCP
Setup login
Update editor: View > Preferences > Editors > Add

Be sure to have psql installed

Test index.html

vi /srv/cvauto-updated.co.za/test/index.html

Test NGinx

vi /etc/nginx/sites-available/cvauto

server {
	listen 80;
	listen [::]:80;

	server_name cvauto-updated.co.za www.cvauto-updated.co.za;

	root /srv/cvauto.co.za/test;
	index index.html;
}

cd /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-available/cvauto.
sudo systemctl restart nginx

On your local browser:
http://cvauto-updated.co.za/

Setup new environment

pip install virtualenv
cd /pathtoenvironments
virtualenv cvauto
source cvauto/bin/activate
to leave you type deactivate

Django

https://www.djangoproject.com/
python -m django --version
If there's no django:
python -m pip install Django
cd to directory with sites source: cd /opt/sites
django-admin startproject cvauto
python manage.py runserver

GUnicorn

python -m gunicorn --version
python -m pip install gunicorn

Make a start script:
/opt/sites/cvauto/configs/gunicorn.sh

#!/bin/bash

set -e

LOGFILE=/srv/cvauto.co.za/logs/gunicorn.log
LOGDIR=$(dirname $LOGFILE)
NUM_WORKERS=9
USER=gideon
GROUP=www-data
TIMEOUT=1200

export PATH="/home/gideon/environments/cvauto/bin:$PATH"

cd /srv/cvauto.co.za/cvauto/
source /home/gideon/environments/cvauto/bin/activate
test -d $LOGDIR || mkdir -p $LOGDIR
cd /srv/cvauto.co.za/cvauto/
exec gunicorn -b 127.0.0.1:8081 -w $NUM_WORKERS --user=$USER --group=$GROUP --timeout=$TIMEOUT --log-level=debug --log-file=$LOGFILE cvauto.wsgi:application 2>>$LOGFILE

Make a directory logs:
/srv/cvauto.co.za/logs/

We'll be using port 8081:
sudo ufw allow 8081

/srv/cvauto.co.za$ ln -s /opt/sites/cvauto/ .

The user needs to be part of www-data
usermod -a -G www-data gideon
newgrp www-data ( otherwise you may need to log in again )

cd /opt/sites/cvauto/
run with group www-data ( note this will keep running, open in a new screen)
sg www-data -c 'bash /srv/cvauto.co.za/cvauto/configs/gunicorn.sh'

logs: /srv/cvauto.co.za/logs/gunicorn.log
processes: ps aux | grep gunicorn

NGinx with GUnicorn site

server {
        listen 80;
        listen [::]:80;

        server_name cvauto-updated.co.za www.cvauto-updated.co.za;

        client_max_body_size 100m;

        add_header X-XSS-Protection "0; mode=block";

        location / {
            proxy_pass http://127.0.0.1:8081/;
            include /etc/nginx/proxy.conf;
        }

        access_log  /srv/cvauto.co.za/logs/static_access.log;
        error_log   /srv/cvauto.co.za/logs/static_error.log;
}

Update Django project settings:
/opt/sites/cvauto/cvauto/settings.py

ALLOWED_HOSTS = ['*']

Run the launch gunicorn again:
sg www-data -c 'bash /srv/cvauto.co.za/cvauto/configs/gunicorn.sh'

sudo systemctl restart nginx

Test the initial page:
http://cvauto-updated.co.za/

Django project settings

Get the database ready
sudo -u postgres psql
CREATE DATABASE cvauto;
If you haven't done so allready
CREATE USER myprojectuser WITH PASSWORD 'password';
GRANT ALL PRIVILEGES ON DATABASE cvauto TO myprojectuser;

hidden1

hidden2