Django blasts off
RuSITE | caorussell | March 10, 2020, 8:40 a.m.

From now on, things go a bit complicated.

We should set up connections within Django, uWSGI, and Nginx.

1. Django to uWSGI

Start a Django project, named as "mysite".

django-admin startproject mysite

Find the settings.py file at directory ~/mysite/mysite and edit ALLOWED_HOSTS.

cd ~/mysite/mysite
vim settings.py
ALLOWED_HOSTS = ['*',]

Go to ~/mysite, and there is a manage.py. Try to run this file to start the Django server.

cd ~/mysite
python3 manage.py runserver 0.0.0.0:9000

Open the test page.

www.caorussell.com:9000

The Django Welcome page.

 Try to launch Django via uWSGI.

uwsgi --http :9000 --module mysite.wsgi

 Then you will see the Django welcome page again.

www.caorussell.com:9000

Go to the ~/mysite and print the current direction.

cd ~/mysite
pwd
/home/ubuntu/mysite

Create a uWSGI configuration file, name as "mysite_uwsgi.ini".

sudo vim ~/mysite/mysite_uwsgi.ini

Edit the file.

#mysite_uwsgi.ini
[uwsgi]
http = :9000
module = mysite.wsgi
chdir = /home/ubuntu/mysite
master = true
processes = 10
vacuum = true
env DJANGO_SETTINGS_MODULE=mysite.settings
safe-pidfile = /tmp/project-master.pid
harakiri = 20
limit-as = 128
max-requests = 5000
daemonize = /var/log/uwsgi/mysite.log
env = LANG=en_US.UTF-8

Create a new directory for the uWSGI log file.

sudo mkdir /var/log/uwsgi

 Start the uWSGI web server via ini file.

cd ~/mysite
uwsgi --ini mysite_uwsgi.ini
[uWSGI] getting INI configuration from mysite_uwsgi.ini

# Run this command if you get "Permission Denied".

sudo chmod -R 777 /var/log/uwsgi

Run the test page at the port 9000.

www.caorussell.com:9000

If everything is OK, don't forget to stop the server after this test.

sudo killall -9 uwsgi

2. uWSGI to Nginx

Copy the uWSGI parametric file of Nginx to the Django project directory.

cp /etc/nginx/uwsgi_params ~/mysite

New an Nginx configuration file for the project.

sudo vim ~/mysite/mysite_nginx.conf

Link the config file to the Nginx directory.

sudo ln -s ~/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/

Then edit the nginx.conf file.

sudo vim /etc/nginx/nginx.conf

Modify the user parameter from "www-data" to "root".

user root;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
    worker_connections 768;
}
http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    gzip on;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Create media and static in the project directory for further use. 

mkdir /home/ubuntu/mysite/media
mkdir /home/ubuntu/mysite/static

Edit the Nginx default configuration file.

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

a. default for http.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name www.caorussell.com;
    charset utf-8;
    location / {
        proxy_set_header x-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        include     /etc/nginx/uwsgi_params;
        uwsgi_pass 127.0.0.1:9000;
    }
    location /media {
        alias /home/ubuntu/mysite/media;
    }
    location /static {
        alias /home/ubuntu/mysite/static;
    }
}

b. default for https.

You should put your ssl certificate files into a certain directory, which may end with suffix .crt .key.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}
server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    server_name www.caorussell.com;
    charset utf-8;
    ssl             on;
    ssl_certificate     /home/ubuntu/mysite/ssl/caorussell.com.crt;
    ssl_certificate_key     /home/ubuntu/mysite/ssl/caorussell.com.key;
    location / {
        proxy_set_header x-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        include     /etc/nginx/uwsgi_params;
        uwsgi_pass 127.0.0.1:9000;
    }
    location /media {
        alias /home/ubuntu/mysite/media;
    }
    location /static {
        alias /home/ubuntu/mysite/static;
    }
}

Start your Nginx server.

sudo /etc/init.d/nginx start

Edit the uWSGI configuration file "mysite_uwsgi.ini" one more time, to build the connection with Nginx via socket 9000.

sudo vim ~/mysite/mysite_uwsgi.ini
#mysite_uwsgi.ini
[uwsgi]
socket = 127.0.0.1:9000
chmod-socket = 666
module = mysite.wsgi
chdir = /home/ubuntu/mysite
master = true
processes = 10
vacuum = true
env DJANGO_SETTINGS_MODULE=mysite.settings
safe-pidfile = /tmp/project-master.pid
harakiri = 20
limit-as = 128
max-requests = 5000
daemonize = /var/log/uwsgi/mysite.log
env = LANG=en_US.UTF-8

Start uWSGI via ini file.

cd ~/mysite
uwsgi --ini mysite_uwsgi.ini

Run the test page, and your Django Welcome page should be there.

www.caorussell.com

Nice job you have done till now! Finally, we can go back and focus on our Django project.