Ejercicio 2: Desplegando aplicaciones flask con Apache + gunicorn
En esta tarea se usa la aplicación Guestbook.
Crear venv
mkdir venv
cd venv
sudo apt update
sudo apt install python3-venv
python3 -m venv .
source bin/activate
Descargar Guestbook
sudo apt install git
git clone https://github.com/josedom24/guestbook.git
pip install -r requirements.txt
sudo apt install redis
Fichero wsgi
/home/vagrant/guestbook/app/wsgi.py
Instalar Gunicorn
Probar funcionamiento
Crear unidad systemd
/etc/systemd/system/gunicorn-guestbook.service
:
[Unit]
Description=gunicorn-guestbook
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
User=www-data
Group=www-data
Restart=always
ExecStart=/home/vagrant/venv/bin/gunicorn -w 2 -b :8080 wsgi
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
WorkingDirectory=/home/vagrant/guestbook/app
Environment=PYTHONPATH='/home/vagrant/guestbook/app:/home/vagrant/venv/lib/python3.9/site-packages'
PrivateTmp=true
La activo e inicio:
Pruebo que funciona:
Apache + Gunicorn
Creo /etc/apache2/sites-available/guestbook.conf
:
<VirtualHost *:80>
ServerName www.guestbook-gunicorn.com
DocumentRoot /home/vagrant/guestbook/app
<Directory /home/vagrant/guestbook/app>
Require all granted
</Directory>
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>
Lo habilito:
Reinicio Apache:
Modifico mi /etc/hosts
:
Pruebo que funciona:
Nginx + Gunicorn
Creo /etc/nginx/sites-available/guestbook.conf
:
server {
listen 80;
server_name www.guestbook-gunicorn.com;
root /home/vagrant/guestbook/app;
location / {
proxy_pass http://localhost:8080;
include proxy_params;
}
}
Lo habilito:
Reinicio Nginx:
Pruebo que funciona: