Flask app on uwsgi/nginx - unix socket file is not created on booting

匿名 (未验证) 提交于 2019-12-03 02:26:02

问题:

I'm trying to use Flask app on uwsgi/nginx.

Following http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html and http://www.markjberger.com/flask-with-virtualenv-uwsgi-nginx/, I could make wiki.ini file,

[uwsgi] vhost = true socket = /tmp/flask_app.sock venv = /home/ubuntu/webapp/flask/hello/.env chdir = /home/ubuntu/webapp/flask/hello module = flaskapp callable = app chmod-socket = 666 

I checked the wiki.ini file works fine with uwsgi --ini wiki.ini.

Then, I tried to start the Flask app when booting.

From sudo update-rc.d uwsgi enable, I could start the uwsgi service in booting time, and copied the wiki.ini file in /etc/uwsgi/apps-enabled directory.

This is the conf file for nginx.

server {     listen       80;     server_name wiki.example.com;     access_log  /var/log/nginx/uwsgi_access.log;     error_log /var/log/nginx/uwsgi_error.log;      location / { try_files $uri @riki; }     location @riki {         include uwsgi_params;         uwsgi_pass unix:/tmp/flask_app.sock;     }      error_page  404              /404.html; } 

However, when I rebooted my ubuntu server, the Flask app isn't working. I checked the error log to find this error message.

2015/11/07 17:48:17 [crit] 1055#0: *1 connect() to  unix:/tmp/flask_app.sock failed (2: No such file or directory)  while connecting to upstream, client: 68.203.30.28, server: wiki.example.com, 

I created the /tmp/flask_app.sock file and run chown -R www-data:www-data /tmp/flask_app.sock to make the application working.

> touch /tmp/flask_app.sock > sudo chown www-data:www-data /tmp/flask_app.sock  > sudo service uwsgi restart > sudo service nginx restart 

However, I had another connection refuse error.

2015/11/07 17:50:38 [error] 1055#0: *4 connect() to  unix:/tmp/flask_app.sock failed (111: Connection refused) while  connecting to upstream, client: 68.203.30.28,  server: wiki.example.com, request: "GET / HTTP/1.1",  upstream: "uwsgi://unix:/tmp/flask_app.sock:", host: "wiki.example.com" 

What might be wrong? How to teach uwsgi to create the unix domain socket? Also, how to make the connection work? I use ubuntu 14.04.

EDIT

Removing the /tmp/flask_app.sock and run uwsgi --ini /etc/uwsgi/apps-enabled/wiki.ini makes the app working fine.

回答1:

The main issue seems to be from the uwsgi service; it just doesn't work.

I found another way to start uwsgi at startup: upstart and uwsgi --emperor from http://uwsgi-docs.readthedocs.org/en/latest/Upstart.html and http://upstart.ubuntu.com

The process is just make a flask.conf file in /etc/init directory. uwsgi --emperor controls all the ini files in the uwsgi directory.

# simple uWSGI script # http://uwsgi-docs.readthedocs.org/en/latest/Upstart.html  description "uwsgi tiny instance" start on runlevel [2345] stop on runlevel [06]  respawn  exec uwsgi --emperor /etc/uwsgi/apps-enabled 

I also had to sudo update-rc.d uwsgi disable so that uwsgi service should be disabled.

I also found this site http://flaviusim.com/blog/Deploying-Flask-with-nginx-uWSGI-and-Supervisor/ for invoking uswgi at startup, but I didn't test it.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!