Executando um site Django em nginx + Gunicorn + SSL

Prefácio

Demorou muito tempo e esforço para escrever este artigo. Encontrei muitas instruções, tanto em inglês quanto em russo, mas, pelo que entendi, eram todas clones do artigo original do Digital Ocean. Você pergunta por que penso assim, mas tudo porque todos os erros e imprecisões são transferidos de um recurso para outro sem nenhuma alteração.





Treinamento

Temos um VPS regular com o sistema operacional Ubuntu, e já escrevemos nosso site no Django no PyCharm ou em um bloco de notas, e tudo o que resta é publicá-lo, vincular um domínio, instalar um certificado e pronto.





A primeira etapa é atualizar a lista de repositórios e instalar o pacote nginx imediatamente:





apt-get update
apt-get install nginx
      
      



Decidi armazenar os arquivos do site no diretório / var / www /. Nesse caso, vamos para o   diretório cd / var / www / e criamos um novo  diretório mkdir geekhero  e obtemos o seguinte caminho: / var / www / geekhero /





Vá para o novo diretório geekhero:  cd geekhero  e crie um ambiente virtual:  python3 -m venv geekhero_env





Nós ativamos o ambiente virtual:  source geekhero_env / bin / activate  e instalamos Django nele: pip instale Django e imediatamente instale pip instale 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-enabledsudo 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







Resta apenas reiniciar o serviço nginx: sudo systemctl restart nginx







Resultado

Como parte deste artigo, cobrimos como colocar nosso site em produção instalando Django, Gunicorn, nginx e até mesmo o certbot do Let's Encrypt.








All Articles