使用FlaskApp进行uWSGI配置

时间:2016-10-08 20:30:47

标签: flask uwsgi ubuntu-16.04 systemd

为了切换到Nginx,我遇到了配置问题并遇到了502 Gateway错误。这是连接错误日志:

tail -f error.log
2016/10/08 16:09:31 [crit] 21682#21682: *29 connect() to unix:/var/www/FlaskApp/FlaskApp/runserver.sock failed (13: Permission denied) while connecting to upstream, client: 73.188.249.47, server: ceejcalvert.com, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/var/www/FlaskApp/FlaskApp/runserver.sock:", host: "website.com"

如果我在服务器的终端上,我可以通过以下方式手动指示套接字来启动网站:

如果运行,此命令将使一切正常:

uwsgi -s /var/www/FlaskApp/FlaskApp/runserver.sock -w runserver:app --chmod-socket=666

问题是我无法在守护进程模式下运行它。我的配置如下:

$ cat /etc/systemd/system/runserver.service 

[Unit]
Description=uWSGI instance to serve runserver
After=network.target

[Service]
User=username
Group=www-data
WorkingDirectory=/var/www/FlaskApp/FlaskApp
Environment="PATH=/var/www/FlaskApp/FlaskApp/venv/bin"
ExecStart=/var/www/FlaskApp/FlaskApp/venv/bin/runserver.sock --ini runserver.ini

[Install]
WantedBy=multi-user.target

...

cat /var/www/FlaskApp/FlaskApp/runserver.ini 

[uwsgi]
module = wsgi:app

master = true
processes = 5

logto = /home/jmc856/error.log

socket = runserver.sock
chmod-socket = 666
vacuum = true

die-on-term = true

假设站点可用链接到已启用站点的

cat /etc/nginx/sites-available/runserver 

server {
    listen 80;
    server_name website.com;

location / {
    include uwsgi_params;
    uwsgi_pass unix:/var/www/FlaskApp/FlaskApp/runserver.sock;
    }
}

我遗失了哪些明显的东西?

当我运行以下内容时,我会得到退出代码3.

sudo systemctl status runserver

runserver.service - uWSGI instance to serve runserver
Loaded: loaded (/etc/systemd/system/runserver.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2016-10-08 16:08:45 EDT; 20min ago
Main PID: 22365 (code=exited, status=203/EXEC)

Oct 08 16:08:45 FirstNameLastName.com systemd[1]: Stopped uWSGI instance to serve runserver.
Oct 08 16:08:45 FirstNameLastName.com systemd[1]: Started uWSGI instance to serve runserver.
Oct 08 16:08:45 FirstNameLastName.com systemd[1]: runserver.service: Main process exited, code=exited, status=203/EXEC
Oct 08 16:08:45 FirstNameLastName.com systemd[1]: runserver.service: Unit entered failed state.
Oct 08 16:08:45 FirstNameLastName.com systemd[1]: runserver.service: Failed with result 'exit-code'.

2 个答案:

答案 0 :(得分:1)

解决了我的问题。在大多数情况下,我的配置很好。如果您遇到502网关错误,这是一个确保清单的清单。

1)我首先添加了配置文件的所有绝对路径。例如,我将systemd配置更改为:

$ cat /etc/systemd/system/runserver.service 

[Unit]
Description=uWSGI instance to serve runserver
After=network.target

[Service]
User=username
Group=www-data
WorkingDirectory=/var/www/FlaskApp/FlaskApp
Environment="PATH=/var/www/FlaskApp/FlaskApp/venv/bin"
ExecStart=/var/www/FlaskApp/FlaskApp/venv/bin/runserver.sock --ini    /var/www/FlaskApp/FlaskApp/runserver.ini

[安装] WantedBy = multi-user.target

2)更改了.ini文件以直接调用uWSGI app:

 cat /var/www/FlaskApp/FlaskApp/runserver.ini 

[uwsgi]
chdir=/var/Webpage/
wsgi-file = wsgi.py
callable = app

master = true
processes = 5

logto = /home/error.log

socket = runserver.sock
chmod-socket = 666
vacuum = true

die-on-term = true

3)确保FlaskApp主机为0.0.0.0:

if __name__ == "__main__":
app.run(host='0.0.0.0')

4)使用这些命令试图找出失败的地方。

确保配置具有正确的语法

$ sudo nginx -t

确保nginx守护程序正常运行

$ systemctl status nginx.service

确保服务{app}的uWSGI实例正在运行。

$ systemctl

如果一切正常但仍然发现错误,请在

中搜索失败
$ sudo journalctl

并且

$ sudo tail -f /var/log/nginx/error.log

如果一切正常,请确保执行以下操作:

$ sudo systemctl restart {app}
$ sudo start {app}
$ sudo enable {app}

最后一个命令是我忘记的东西,阻止我实现我的配置很长一段时间。在我的情况下,{app}是'runserver'

答案 1 :(得分:0)

我一直在使用这个Docker镜像用于我的所有nginx和flask应用程序 https://github.com/tiangolo/uwsgi-nginx-flask-docker