Prefacio
Me llevó mucho tiempo y esfuerzo escribir este artículo. Encontré muchas instrucciones, tanto en inglés como en ruso, pero según entendí, todas eran clones del artículo original sobre Digital Ocean. Usted pregunta por qué creo que sí, pero todo porque todos los errores e inexactitudes se transfieren de un recurso a otro sin ningún cambio.
Capacitación
Tenemos un VPS regular con el sistema operativo Ubuntu, y ya hemos escrito nuestro sitio en Django en PyCharm o en un bloc de notas, y todo lo que queda es publicarlo, vincular un dominio, instalar un certificado y listo.
El primer paso es actualizar la lista de repositorios e instalar el paquete nginx inmediatamente:
apt-get update apt-get install nginx
Decidí almacenar los archivos del sitio en el directorio / var / www /. En este caso, nos movemos al directorio cd / var / www / y creamos un nuevo directorio mkdir geekhero y obtenemos la siguiente ruta: / var / www / geekhero /
Vaya al nuevo directorio de geekhero: cd geekhero y cree un entorno virtual: python3 -m venv geekhero_env
Activamos el entorno virtual: source geekhero_env / bin / active e instalamos Django en él: pip instala Django e inmediatamente instalamos 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
Solo queda reiniciar el servicio nginx: sudo systemctl restart nginx
Salir
Como parte de este artículo, hemos cubierto cómo llevar nuestro sitio a producción instalando Django, Gunicorn, nginx e incluso el certbot de Let's Encrypt.