Foreword
It took a lot of time and effort to write this article. I came across a lot of instructions, both in English and in Russian, but as I understood, they were all clones of the original article on Digital Ocean. You ask why I think so, but all because all errors and inaccuracies are transferred from one resource to another without any changes.
Training
We have a regular VPS with the Ubuntu OS, and we have already written our site on Django in PyCharm or in a notepad, and all that remains is to publish it, bind a domain, install a certificate and go.
The first step is to update the list of repositories and install the nginx package immediately:
apt-get update apt-get install nginx
I decided to store the site files in the / var / www / directory. In this case, we move to the cd / var / www / directory and create a new mkdir geekhero directory and get the following path: / var / www / geekhero /
Go to the new geekhero directory: cd geekhero and create a virtual environment: python3 -m venv geekhero_env
We activate the virtual environment: source geekhero_env / bin / activate and install Django in it: pip install Django and immediately install pip install gunicorn
: django-admin startproject ghproj
; : python manage.py makemigrations
, python manage.py migrate
.
: python manage.py createsuperuser
.
applications, , .
Settings.py , :
import os
– , :
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
Gunicorn
/etc/systemd/system/ : gunicorn.service gunicon.socket:
gunicorn.service:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=root
WorkingDirectory=/var/www/geekhero # manage.py
ExecStart=/var/www/geekhero/geekhero_env/bin/gunicorn --workers 5 --bind unix:/run/gunicorn.sock ghproj.wsgi:application
# gunicorn
[Install]
WantedBy=multi-user.target
gunicorn.socket:
[Unit] Description=gunicorn socket [Socket] ListenStream=/run/gunicorn.sock [Install] WantedBy=sockets.target
gunicorn.service :
systemd-analyze verify gunicorn.service
NGINX
: /etc/nginx/sites-available/ geekhero ( ) :
server {
listen 80;
server_name example.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/www/geekhero; # static
}
location /media/ {
root /var/www/geekhero; # media
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
, /etc/nginx/site-enabled/ :
sudo ln -s /etc/nginx/sites-available/geekhero /etc/nginx/sites-enabled/
, sites-enabled : sudo systemctl restart nginx
nginx :
sudo nginx -t
sudo nginx -t
gunicorn socket:
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
:
sudo systemctl disable gunicorn
sudo systemctl stop gunicorn
, , - HTML python , , , , , python manage.py makemigrations <app> migrate <app> .
/ Gunicorn:
service gunicorn start / service gunicorn stop
:
sudo systemctl status gunicorn sudo journalctl -u gunicorn.socket ( : 05 16:40:19 byfe systemd[1]: Listening on gunicorn socket. )
, :
file /run/gunicorn.sock
: /run/gunicorn.sock: socket
- gunicorn.service .socket, :
systemctl daemon-reload
, nginx:
sudo service nginx start
SSL
certbot Let's Encrypt: sudo apt-get install certbot python-certbot-nginx
certbot: sudo certbot certonly --nginx
- nginx: sudo certbot install --nginx
It remains only to restart the nginx service: sudo systemctl restart nginx
Outcome
As part of this article, we have covered how to bring our site to production by installing Django, Gunicorn, nginx, and even Let's Encrypt's certbot.