“未找到Python应用” uWSGI + nginx + Ubuntu 13

6 投票
1 回答
21126 浏览
提问于 2025-04-18 07:34

我知道这个问题很常见,但我看到很多例子里的 ini 文件都让人摸不着头脑。不同的系统有不同的文件结构:

比如说 /etc/uwsgi/vassals 和 /etc/uwsgi/apps-{enabled|available} 之间的区别,还有单独启动 uwsgi 的情况。

所以,求求你们,帮帮我吧,我已经熬了14个小时,脑袋快炸了:

我有一个基本的 Flask 项目,文件结构是这样的:

/srv/py/mylovelyapp/mylovelyapp.py
                   /models.py
                   /database.py
                   /static/
                   /templates/

我那份怪异的 ini 文件(位于 /etc/uwsgi/apps-enabled/mylovelyapp.ini)是:

[uwsgi]
plugins = python
base = /srv/py/mylovelyapp
app = mylovelyapp
callable = app
gid = www-data
uid = www-data
vhost = true
socket = 127.0.0.1:3031
master = true
processes = 1
harakiri = 20
limit-as = 128

nginx 的配置文件在 /etc/nginx/sites-enabled/mysite.conf:

server {
listen 80;
server_name www.mylovelyapp.co.uk mylovelyapp.co.uk;

charset     utf-8;
client_max_body_size 75M;

location / { try_files $uri @yourapplication; }
location @yourapplication {
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:3031;
}
location /static/ {
    alias /srv/py/mylovelyapp/static/;
    expires 30d;
    access_log off;
}

access_log /var/log/nginx/mylovelyapp-a.conf;
error_log /var/log/nginx/mylovelyapp-e.conf;
}

当我查看 /var/log/uwsgi/mylovelyapp.log 的错误输出时,看到的是:

Mon May 26 06:41:40 2014 - *** Python threads support is disabled. You can enable it with --enable-threads ***
Mon May 26 06:41:40 2014 - Python main interpreter initialized at 0x1445e50
Mon May 26 06:41:40 2014 - your server socket listen backlog is limited to 100 connections
Mon May 26 06:41:40 2014 - your mercy for graceful operations on workers is 60 seconds
Mon May 26 06:41:40 2014 - mapped 145536 bytes (142 KB) for 1 cores
Mon May 26 06:41:40 2014 - *** Operational MODE: single process ***
Mon May 26 06:41:40 2014 - *** no app loaded. going in full dynamic mode ***
Mon May 26 06:41:40 2014 - *** uWSGI is running in multiple interpreter mode ***
Mon May 26 06:41:40 2014 - spawned uWSGI master process (pid: 2380)
Mon May 26 06:41:40 2014 - spawned uWSGI worker 1 (pid: 2388, cores: 1)

而访问网址时却显示这个信息:

Internal Server Error

我知道我缺少一些简单的参考资料,但我尝试了各种方法和搜索,结果找到的例子都不太适合我的情况。更让人沮丧的是,这其实是个很简单的应用和设置!!

拜托,拜托,帮帮我。 :(

附言:如果你能告诉我怎么让这个 Flask 应用使用虚拟机,我会非常感激你。

再附言:我听说 Gunicorn 更简单——我是不是应该换成这个?

1 个回答

1

你应该花点时间去了解所有相关的组件。比如,你把 .ini 的 uWSGI 文件和 nginx.conf 合并在一起,这样做是完全错误的。我建议你从这里开始学习:http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html

尽量理解每一步,特别是关于使用官方源而不是发行版包的部分。先尝试只用 uWSGI 部署,而不使用 nginx,等你确认一切都明白了,再考虑把它放在 nginx 后面。

关于 gunicorn,它确实更简单,因为它是用 Python 写的(所以你不需要 C 编译器来构建它),而且功能比较少,这样你在网上找到的配置也会少一些(不过,真的要避免盲目复制粘贴,你应该花时间去理解发生了什么,否则你的网站在遇到小问题时可能会一直宕机)。从我看到的、理解的和想象的来看,按你现在的情况,使用哪个 WSGI 服务器对你来说并没有太大区别。

撰写回答