kramer65
kramer65

Reputation: 54003

How to correctly load Flask app module in uWSGI?

[EDIT]

I managed to load the flask app module by starting uwsgi from within the project folder. I now have a problem with nginx not having permission to the socket file though (scroll down to the end of the question). If anybody can help with that..?

[/EDIT]

Following this tutorial I'm trying to run my Flask website with uWSGI and nginx. When doing exactly as the tutorial says it works fine. I now want to run my own website though. And the structure of my own website project looks as follows:

myownproject
   |-app
       | -__init__.py
   |-run.py
   |-myownproject_nginx.conf
   |-myownproject_uwsgi.ini

in which app is loaded in __init__.py like this:

app = Flask(__name__)

and myownproject_uwsgi.ini looks like this:

[uwsgi]
#application's base folder
base = /home/kramer65/myownproject

#python module to import
app = app
module = %(app)

# I disabled these lines below because I don't use a venv (please don't ask)
# home = %(base)/venv
# pythonpath = %(base)

#socket file's location
socket = /home/kramer65/myownproject/%n.sock

#permissions for the socket file
chmod-socket    = 666

#the variable that holds the flask application inside the imported module
callable = app

#location of log files
logto = /var/log/uwsgi/%n.log

But when I run this:

$ uwsgi --ini /home/kramer65/myownproject/myownproject_uwsgi.ini
[uWSGI] getting INI configuration from /home/kramer65/myownproject/myownproject_uwsgi.ini

I get the following logs in /var/log/uwsgi/myownproject_uwsgi.log:

*** Operational MODE: single process ***
ImportError: No module named app
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***

Why doesn't uwsgi find my callable? And why is the mountpoint empty (='')? What am I doing wrong here?

Does anybody know how I can get this to work properly?

[EDIT]

Okay, I tried running uwsgi --ini /home/kramer65/myownproject/myownproject_uwsgi.ini from within the myownproject project folder, which solves this problem; it now finds the callable and that seems to work fine.

I still get a 502 though, the next problem seems to be a permission problem with nginx not having permission to the socket file. /var/log/nginx/error.log says:

2015/10/27 11:40:36 [crit] 14276#0: *1 connect() to unix:/home/kramer65/myownproject/myownproject_uwsgi.sock failed (13: Permission denied) while connecting to upstream, client: 80.xxx.xxx.xxx, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/home/kramer65/myownproject/myownproject_uwsgi.sock:", host: "52.xx.xx.xxx"

So I changed the chmod-socket = 666 to chmod-socket = 777. When doing an ls -l I actually see the socket file having full permissions, but I still get the error I pasted above.

Any ideas to get this working?

Upvotes: 4

Views: 5869

Answers (1)

iurisilvio
iurisilvio

Reputation: 4987

The base config is just a internal variable. The part you commented out caused your problem.

If you don't want to use virtualenv and set your pythonpath, change the base config to chdir.

chdir = /home/kramer65/myownproject

Internally, uWSGI will run from chdir instead of from the current directory.

About the socket permission problem, the nginx user (probably www-data) does not have access to your personal folder (/home/kramer65/). You must set the socket to another folder, where nginx and uwsgi have access.

Upvotes: 3

Related Questions