Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
python:flask:deploiement_production [2021/04/27 04:50] marclebrun [Configuration d'Apache] |
python:flask:deploiement_production [2023/11/09 05:33] (Version actuelle) marclebrun |
||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
Déploiement via **ssh** avec la commande **rsync**. | Déploiement via **ssh** avec la commande **rsync**. | ||
- | À lire : [[https://www.bogotobogo.com/python/Flask/Python_Flask_HelloWorld_App_with_Apache_WSGI_Ubuntu14.php]] | + | À lire : |
+ | * [[https://www.bogotobogo.com/python/Flask/Python_Flask_HelloWorld_App_with_Apache_WSGI_Ubuntu14.php]] | ||
+ | * [[https://www.bortzmeyer.org/wsgi.html]] | ||
===== Préparation du serveur ===== | ===== Préparation du serveur ===== | ||
Ligne 13: | Ligne 15: | ||
<code bash> | <code bash> | ||
sudo apt install libapache2-mod-wsgi-py3 | sudo apt install libapache2-mod-wsgi-py3 | ||
+ | </code> | ||
+ | |||
+ | Installer **python3-pip** si nécessaire | ||
+ | |||
+ | <code bash> | ||
+ | sudo apt install python3-pip | ||
</code> | </code> | ||
Ligne 27: | Ligne 35: | ||
<code bash> | <code bash> | ||
sudo mkdir /var/www/monsite | sudo mkdir /var/www/monsite | ||
- | sudo chmod marc:marc /var/www/monsite | + | sudo chown marc:marc /var/www/monsite |
</code> | </code> | ||
Ligne 36: | Ligne 44: | ||
<code bash deploy.sh> | <code bash deploy.sh> | ||
#!/bin/sh | #!/bin/sh | ||
- | rsync -av ./ USER@SERVEUR:/var/www/monsite/ \ | + | rsync -rlpv \ |
+ | ./ \ | ||
+ | USER@SERVEUR:/var/www/monsite/ \ | ||
--exclude-from=.gitignore \ | --exclude-from=.gitignore \ | ||
- | --exclude=".*" | + | --exclude=".*" \ |
--exclude="run.py" | --exclude="run.py" | ||
</code> | </code> | ||
+ | ^ Voir la [[https://ss64.com/bash/rsync.html|Man Page de rsync]] ^ | ||
^ Ne pas oublier de terminer les chemins par des "/" ^ | ^ Ne pas oublier de terminer les chemins par des "/" ^ | ||
Ligne 81: | Ligne 92: | ||
<code python /var/www/monsite/myapp.wsgi> | <code python /var/www/monsite/myapp.wsgi> | ||
#!/usr/bin/python | #!/usr/bin/python | ||
- | import sys | + | import os, sys |
- | sys.path.insert(0, "/var/www/monsite/") | + | sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) |
from app import app as application | from app import app as application | ||
</code> | </code> | ||
+ | |||
+ | ===== Créer un fichier .env ===== | ||
+ | |||
+ | Si nécessaire, créer la configuration du site dans un fichier **.env** : | ||
+ | |||
+ | <code bash .env> | ||
+ | FLASK_ENV=production | ||
+ | |||
+ | SECRET_KEY='...(chaîne aléatoire)...' | ||
+ | |||
+ | DBPATH=... | ||
+ | |||
+ | ... | ||
+ | </code> | ||
+ | |||
+ | Pour générer la clé secrète, voir la page [[python:generer_secret_key]] | ||
===== Configuration d'Apache ===== | ===== Configuration d'Apache ===== | ||
Ligne 90: | Ligne 117: | ||
Créer un nouveau **VirtualHost** : | Créer un nouveau **VirtualHost** : | ||
- | <code apache monsite.conf> | + | <code apache /etc/apache2/sites-available/monsite.conf> |
<VirtualHost *:80> | <VirtualHost *:80> | ||
ServerName monsite.com | ServerName monsite.com | ||
ServerAdmin admin@monsite.com | ServerAdmin admin@monsite.com | ||
- | WSGIDaemonProcess myapp user=www-data group=www-data threads=5 | + | WSGIDaemonProcess myapp user=www-data group=www-data python-home=/var/www/monsite/venv threads=5 |
WSGIProcessGroup myapp | WSGIProcessGroup myapp | ||
WSGIScriptAlias / /var/www/monsite/myapp.wsgi | WSGIScriptAlias / /var/www/monsite/myapp.wsgi | ||
Ligne 109: | Ligne 136: | ||
</VirtualHost> | </VirtualHost> | ||
</code> | </code> | ||
+ | |||
+ | ^ Le nom "myapp" indiqué dans WSGIDaemonProcess et WSGIProcessGroup ne doit pas être utilisé par un autre VirtualHost ! ^ | ||
Le dossier **/var/log/apache2/monsite.com** pour les logs doit exister. | Le dossier **/var/log/apache2/monsite.com** pour les logs doit exister. | ||
Ligne 116: | Ligne 145: | ||
<code bash> | <code bash> | ||
sudo a2ensite monsite.conf | sudo a2ensite monsite.conf | ||
+ | sudo systemctl reload apache2 | ||
+ | </code> | ||
+ | |||
+ | ===== Déploiements ultérieurs ===== | ||
+ | |||
+ | **En local**, exécuter le script de déploiement | ||
+ | |||
+ | <code bash> | ||
+ | ./deploy.sh | ||
+ | </code> | ||
+ | |||
+ | ** Sur le serveur**, installer les nouvelles dépendances éventuelles | ||
+ | |||
+ | <code bash> | ||
+ | . venv/bin/activate | ||
+ | pip install -r requirements.txt | ||
+ | deactivate | ||
+ | </code> | ||
+ | |||
+ | ** Sur le serveur**, redémarrer Apache | ||
+ | |||
+ | <code bash> | ||
sudo systemctl reload apache2 | sudo systemctl reload apache2 | ||
</code> | </code> | ||