Hou*_*man 15 flask uwsgi ubuntu-12.04
对于我目前的烧瓶部署,我不得不设置一个uwsgi服务器.这就是我创建uwsgi守护进程的方法:
sudo vim /etc/init/uwsgi.conf
# file: /etc/init/uwsgi.conf
description "uWSGI server"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
exec /myproject/myproject-env/bin/uwsgi --uid www-data --gid www-data --home /myproject/myproject-env/site/F11/Engineering/ --socket /tmp/uwsgi.sock --chmod-socket --module F11 --callable app --pythonpath /myproject/myproject-env/site/F11/Engineering/ -H /myproject/myproject-env
Run Code Online (Sandbox Code Playgroud)
但是在成功运行之后: sudo start uwsgi
uwsgi start/running, process 1286
Run Code Online (Sandbox Code Playgroud)
并尝试通过浏览器访问应用程序:
我得到了502 Bad Gateway
和nginx error.log中的错误条目:
2013/06/13 23:47:28 [错误] 743#0:*296上游过早关闭连接,同时从上游读取响应头,客户端:xx.161.xx.228,服务器:myproject.com,请求:"GET/show_records/2013/5 HTTP/1.1",上游:"uwsgi:// unix:///tmp/uwsgi.sock:",主持人:"myproject.com"
但是sock文件具有它需要的权限:
srw-rw-rw- 1 www-data www-data 0 Jun 13 23:46 /tmp/uwsgi.sock
Run Code Online (Sandbox Code Playgroud)
如果我exec在命令行中从上面运行命令作为一个进程,它可以正常工作.为什么守护程序不能正常工作呢?
顺便说一下Nginx正在运行
vim /etc/nginx/nginx.conf
user www-data;
Run Code Online (Sandbox Code Playgroud)
和 vim /etc/nginx/sites-available/default
location / {
uwsgi_pass unix:///tmp/uwsgi.sock;
include uwsgi_params;
}
Run Code Online (Sandbox Code Playgroud)
它以...开头 sudo service nginx start
我正在运行它Ubuntu 12.04 LTS.
我希望我已经提供了所有必要的数据,希望有人能引导我走向正确的方向.谢谢.
Hou*_*man 19
最后我在工作了近2天后解决了这个问题.我希望这个解决方案能够帮助遇到类似问题的其他flask/uwsgi用户.
我有两个主要问题导致了这个问题.
1)查找守护进程问题的最佳方法显然是日志文件和更清晰的结构.
sudo vim /etc/init/uwsgi.conf
将守护程序脚本更改为以下内容:
# file: /etc/init/uwsgi.conf
description "uWSGI server"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
exec /home/ubuntu/uwsgi-1.9.12/uwsgi -c /myproject/uwsgi.ini
Run Code Online (Sandbox Code Playgroud)
vim /myproject/uwsgi.ini
[uwsgi]
socket = /tmp/uwsgi.sock
master = true
enable-threads = true
processes = 5
chdir= /myproject/F11/Engineering
module=F11:app
virtualenv = /myproject/myproject-env/
uid = www-data
gid = www-data
logto = /myproject/error.log
Run Code Online (Sandbox Code Playgroud)
这是设置守护进程的更简洁的方法.另请注意最后一行如何设置日志文件.最初我已将日志文件设置为/var/log/uwsgi/error.log.经过大量的汗水和泪水,我意识到守护进程正在运行www-data,因此无法访问/var/log/uwsgi/error.log自error.log所拥有的root:root.这使得uwsgi无声地失败.
我发现将日志文件指向我自己的日志文件更有效率/myproject,守护程序保证访问日志文件www-data.而且不要忘记让整个项目可访问,www-data否则守护进程将失败Internal Server error message.- >
sudo chown www-data:www-data -R /myproject/
Run Code Online (Sandbox Code Playgroud)
重启uwsgi守护进程:
sudo service uwsgi restart
Run Code Online (Sandbox Code Playgroud)
2)现在您有三个要查找的日志文件:
tail -f /var/log/upstart/uwsgi.log - >启动时显示守护程序的问题
tail -f /var/log/nginx/error.log - >在拒绝wsgi访问时显示权限问题,通常是因为/tmp/uwsgi.sock文件由root而不是www-data.在这种情况下,只需删除sock文件sudo rm /tmp/uwsgi.sock
tail -f /myproject/error.log - >显示uwsgi在您的应用程序中引发的错误
This combination of log files helped me to figure out that I also had a bad import with Flask-Babel in my Flask application. Bad in that sense, that the way I utilized the library was falling back to the system's locale to determine the datetime format.
File "/myproject/F11/Engineering/f11_app/templates/show_records.html", line 25, in block "body"
<td>{{ record.record_date|format_date }}</td>
File "./f11_app/filters.py", line 7, in format_date
day = babel_dates.format_date(value, "EE")
File "/myproject/myproject-env/local/lib/python2.7/site-packages/babel/dates.py", line 459, in format_date
return pattern.apply(date, locale)
File "/myproject/myproject-env/local/lib/python2.7/site-packages/babel/dates.py", line 702, in apply
return self % DateTimeFormat(datetime, locale)
File "/myproject/myproject-env/local/lib/python2.7/site-packages/babel/dates.py", line 699, in __mod__
return self.format % other
File "/myproject/myproject-env/local/lib/python2.7/site-packages/babel/dates.py", line 734, in __getitem__
return self.format_weekday(char, num)
File "/myproject/myproject-env/local/lib/python2.7/site-packages/babel/dates.py", line 821, in format_weekday
return get_day_names(width, context, self.locale)[weekday]
File "/myproject/myproject-env/local/lib/python2.7/site-packages/babel/dates.py", line 69, in get_day_names
return Locale.parse(locale).days[context][width]
AttributeError: 'NoneType' object has no attribute 'days'
Run Code Online (Sandbox Code Playgroud)
This is the way I was using the Flask filter:
import babel.dates as babel_dates
@app.template_filter('format_date')
def format_date(value):
day = babel_dates.format_date(value, "EE")
return '{0} {1}'.format(day.upper(), affix(value.day))
Run Code Online (Sandbox Code Playgroud)
The strangest part is that this code is working perfectly fine within the dev environment (!). It works even fine when running the uwsgi as a root process from the command line. But it fails when ran by the www-data daemon. This must have something to do with how the locale is set, which Flask-Babel is trying to fall back to.
When I changed the import like this, it all worked finally with the daemon:
from flask.ext.babel import format_date
@app.template_filter('format_date1')
def format_date1(value):
day = format_date(value, "EE")
return '{0} {1}'.format(day.upper(), affix(value.day))
Run Code Online (Sandbox Code Playgroud)
Hence be careful when using Eclipse/Aptana Studio that is trying to pick the right namespace for your classes in code. It can really turn ugly.
从2天开始,它现在在Amazon Ec2(Ubuntu 12.04)上作为uwsgi守护程序工作得非常好.我希望这种经验可以帮助其他python开发人员.
| 归档时间: |
|
| 查看次数: |
20033 次 |
| 最近记录: |