Python - Déploiement de votre application Web

Auteur : Vince NADUS
Date de publication : July 29, 2021

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-20-04-fr

https://stackoverflow.com/questions/54375834/run-multiple-django-project-with-nginx-and-gunicorn

https://michal.karzynski.pl/blog/2013/10/29/serving-multiple-django-applications-with-nginx-gunicorn-supervisor/

Tester la capacité de Gunicorn à servir le projet

pip install gunicorn
gunicorn --bind 0.0.0.0:8000 myproject.wsgi

Création de fichiers de socket et de service systemd pour Gunicorn

/etc/systemd/system/gunicorn.socket

[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target

/etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myprojectdir
ExecStart=/home/sammy/myprojectdir/myprojectenv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
myproject.wsgi:application

[Install]
WantedBy=multi-user.target

systemctl start gunicorn.socket
systemctl enable gunicorn.socket

Configurer Nginx pour passer par proxy à Gunicorn

/etc/nginx/sites-available/myproject

server {
listen 80;
server_name server_domain_or_IP;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/sammy/myprojectdir;
}

location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}