Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activating Gunicorn through virtualenv with Supervisor for Flask Application

I'm trying to deploy a flask application to an ec2 instance using (1) nginx (2) gunicorn, (3) git, and (4) supervisor. I've set up nginx, git, gunicorn, but I've having trouble writing the supervisor script.

I'm unable to get supervisor to launch gunicorn within the context of the virtualenv.

When I run gunicorn run:app outside of the virtualenv it returns

ImportError: No module named flask

When I run the same command within the virtualenv it works just fine.

When I run the same command outside of the virtualenv but specify the gunicorn in the virtual env (i.e /var/www/sitename/env/bin/gunicorn run:app) it works just fine again.

That's a problem that I couldn't figure out, but I figured if I could just have supervisor run gunicorn inside the virtualenv it wouldn't be a problem, but I'm not able to do that either.

I've tried adding two programs in the supervisor script, one to launch the virtual environment and the other for gunicorn, added the two commands together using quotes which one similar SO answer suggested, using && to combine activating the virtualenv and launching gunicorn, declaring an environment=PATH= variable, and a number of other options; I just can't get it to work.

I have no idea what I'm doing wrong or what else to try; does anyone know what I can do at this point?

I'm running python3 - I read that supervisor is limited to v2 but someone else mentioned in an answer that it's just a task manager and it shouldn't matter.

like image 247
Nikola Jankovic Avatar asked Jun 10 '17 22:06

Nikola Jankovic


1 Answers

You were on the right track, but it's simpler than you were making it. To run a Flask app named run with entry point app via gunicorn under supervisor with the path you gave:

/etc/supervisor/conf.d/run.conf

[program:run]
command = /var/www/sitename/env/bin/gunicorn run:app -b localhost:8000
directory = /var/www/sitename
user = siteuser

You can provide the environment argument to set stuff like production mode or whatever, but this is all you need to have the virtual environment version of gunicorn, running python 3 if it's a python 3 venv, run your flask app in the same virtual environment.

like image 94
jla Avatar answered Oct 21 '22 23:10

jla