Installation Notes for Flask App on Ubuntu 14.04 LTS using gUnicorn
Followed these excellent directions from Real Python and modified for python3 and Ubuntu 14.04.Start with updating ubuntu and loading additional packages
sudo apt-get update
sudo apt-get install -y python3 python3-pip nginx mongodb supervisor
sudo pip3 install virtualenv
sudo mkdir /var/www
sudo chown ubuntu:ubuntu /var/www
mkdir /var/www/flask-app
mkdir /var/www/flask-app/logs
cd /var/www/flask-app
Setup virtualenvvirtualenv flask_env
source flask_env/bin/activate
pip install -r requirements.txt
pip install gunicorn
Setup nginx
sudo /etc/init.d/nginx start
sudo rm /etc/nginx/sites-enabled/default
sudo touch /etc/nginx/sites-available/flask-app
sudo ln -s /etc/nginx/sites-available/flask-app /etc/nginx/sites-enabled/flask-app
sudo vim /etc/nginx/sites-enabled/flask-app
Add the following to the nginx flask-app conf file being editedserver {
location / {
proxy_pass http://127.0.0.1:8000;
}
location /static {
alias /var/www/flask-app/flAsk-app/static;
}
}
sudo service nginx reload
Setup gunicorn start file
Setup bash script to run gunicorncd /var/www/flask-app
touch gunicorn_start
chmod a+x gunicorn_start
vim gunicorn_start
Insert the following into the gunicorn_start bash script#!/bin/bash NAME="flAsk-app" FLASKDIR=/var/www/flask-app VENVDIR=/var/www/flask-app/flask_env SOCKFILE=/var/www/flask-app/sock USER=ubuntu
GROUP=ubuntu
NUM_WORKERS=3 echo "Starting $NAME" # activate the virtualenv cd $VENVDIR source bin/activate export PYTHONPATH=$FLASKDIR:$PYTHONPATH # Create the run directory if it doesn't exist RUNDIR=$(dirname $SOCKFILE) test -d $RUNDIR || mkdir -p $RUNDIR # Start your unicorn exec gunicorn runserver:app -b 127.0.0.1:8000 \ --name $NAME \ --workers $NUM_WORKERS \ --user=$USER --group=$GROUP \ --log-level=debug \
Setup Supervisor
This will run and restart the Flask application when the application files are changed.cd /etc/supervisor/conf.d
sudo vim flask-app.conf
Insert the following into the flask-app.conf file:[program:flask-app]
command = /var/www/flask-app/gunicorn_start
user = ubuntu
stdout_logfile = /var/www/flask-app/logs/gunicorn_supervisor.log
redirect_stderr = true
Start flask-app gunicorn:
sudo supervisorctl updateYou can use the following commands as well:
sudo supervisorctl status
sudo supervisorctl start flask-app
sudo supervisorctl start all
sudo supervisorctl help|avail|stop|restart
Not to self: Don't use Markdown with Marsedit to post to Blogger! Bloody hell that was painful.
ReplyDelete