nginx + gunicorn + Django = Production-ready

> How to deploy a django project using gunicorn and nginx. Also to add ssl certificate from certbot

How to create your own django project?

mkdir dummyproj && cd dummyproj
python3 -m venv newenv
source newenv/bin/activate
pip3 install Django
django-admin.py startproject hello

cd hello/

python manage.py runserver 0.0.0.0:8000
ALLOWED_HOSTS = ['*']

Making it production ready…

pip3 install gunicorn
gunicorn hello.wsgi:application --bind 0.0.0.0:8000
sudo vim /etc/systemd/system/gunicorn-hello.service
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=ayedaemon ## User who has permissions to the WorkingDirectory
Group=www-data
## Directory where the project resides
WorkingDirectory=/home/ayedaemon/dummyproj/

## What command will start by this service
ExecStart=/home/ayedaemon/dummyproj/newenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/home/ayedaemon/dummyproj/hello.sock\
hello.wsgi:application

[Install]
WantedBy=multi-user.target
curl --unix-socket /home/ayedaemon/dummyproj/hello.sock localhost
sudo vim /etc/nginx/sites-available/helloproj.conf
server
listen 1234;
server_name <your domain name>; ## Add your domain name or ip on which it'll be hosted

location /static/ {
alias /home/ayedaemon/dummyproj/hello/static/;
}

location / {
include proxy_params;
proxy_pass http://unix:/home/ayedaemon/dummyproj/hello.sock;
}
}
sudo ln -s /etc/nginx/sites-available/helloproj.conf /etc/nginx/sites-enabled
sudo systemctl daemon-reload
sudo systemctl restart gunicorn-hello.service
sudo systemctl restart nginx

How to install certbot ssl certificate?

Connecting the dots and rest is magic. https://ayedaemon.github.io/