linux下nginx+python+uwsgi部署总结(django+web.py)
Published on 七月 13, 2011
之前的文章已经提到了 django+fastcgi的运行并不如意(web.py+spawn-fcgi却正常很多……),所以特意抽时间研究了一下uwsgi,试了一下,运行的很好,也很快,哈哈。
所以笔者的所有之前用apache+django搭建的项目(如fuload等)都已经替换成了nginx+django+uwsgi。
一.安装uwsgi
- 到http://projects.unbit.it/uwsgi/wiki/WikiStart#Getit 下载最新版本的uwsg。
-
解压后,如果没有安装libxml2,又不想安装的话,那么编辑文件 buildconf/default.ini, 把
1
xml_implementation = libxml2
改成
1
xml_implementation = false -
执行编译
1
python uwsgiconfig.py --build -
执行安装
1
python setup.py install
这样,uwsgi就安装上了~
二.nginx配置
配置要比fastcgi还简单一些:
1 2 3 4 | location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9090; } |
三.django配置
在mysite的统计目录(即setting.py的上级目录),创建文件uws_app.py:
1 2 3 4 5 6 7 8 9 | import sys import os sys.path.append(os.path.abspath(os.path.dirname(__file__))) os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() |
启动uwsgi:
1 | uwsgi -s :9090 -w uws_app -p10 -d uws.error & |
其实也可以不用写uws_app.py这个文件,直接启动uwsgi:
1 | uwsgi --module='django.core.handlers.wsgi:WSGIHandler()' --env DJANGO_SETTINGS_MODULE=mysite.settings -s :9090 -p 10 -d uws.log --touch-reload=uws.tc & |
四.web.py配置
创建文件 index.py :
1 2 3 4 5 6 7 8 9 10 11 12 13 | #!/usr/bin/python # -*- coding: utf-8 -*- import web urls = ("/.*", "hello") class hello: def GET(self): return 'Hello, world!' app = web.application(urls, globals()) application = app.wsgifunc() |
启动uwsgi:
1 | uwsgi -s :9091 -w index -p 2 -d uws.error & |
OK,到此为止就整个配置完啦,简单吧!
使用了uwsgi之后的fuload运行也非常稳定:
最后是uwsgi的一些文档,大家可以看一下.
uwsgi 启动参数:
http://projects.unbit.it/uwsgi/wiki/Doc
uwsgi 配合django,web.py,flask等:
http://projects.unbit.it/uwsgi/wiki/Example
原创文章,版权所有。转载请注明:转载自Vimer的程序世界 [ http://www.vimer.cn ]
本文链接地址: http://www.vimer.cn/?p=2274

Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1/admin/blog/blogpost/
Using the URLconf defined in myapp.urls, Django tried these URL patterns, in this order:
1. ^admin/ ^$ [name='index']
2. ^admin/ ^logout/$ [name='logout']
3. ^admin/ ^password_change/$ [name='password_change']
4. ^admin/ ^password_change/done/$ [name='password_change_done']
5. ^admin/ ^jsi18n/$ [name='jsi18n']
6. ^admin/ ^r/(?P\d+)/(?P.+)/$
7. ^admin/ ^(?P\w+)/$ [name='app_list']
The current URL, admin/blog/blogpost/, didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
[回复]
Dante 回复:
八月 29th, 2011 at 11:55 下午
正则写的有问题吧。。
[回复]
你好,我按照你的安装步骤,都没有出现什么问题,也在把/etc/nginx/sites-available/default进行了你上面的配置,但是我更改了root地址。
问题就是现在我打开127.0.0.1却是502错误,因为先前安装了spawn-fcgi,但是我后来删除了,难道是因为没有删除干净的缘故或者其他
= =!希望帮忙解决下,搜了2个多小时,没有解决
[回复]
找到原因了,是配置出来问题
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
}
貌似加上uwsgi_pass 127.0.0.1:9090;就会出现502,难道是因为我的uwsgi没有安装成功?还是说其他的原因?
[回复]
Dante 回复:
九月 15th, 2011 at 9:23 上午
uwsgi 有启动吗?
netstat -lpnt
可以看到监听端口里面有没有9090
[回复]
包子 回复:
九月 15th, 2011 at 7:17 下午
确实配置有问题
uWSGI Error
Python application not found
出现这类问题的时候,是不是需要配置uwsgi,我按照你的安装方式却在/etc里面找不到uwsgi的配置。不知道问题出在哪儿,我用的是ubuntu11,是不是跟发行版本也有关系?
[回复]
Dante 回复:
九月 22nd, 2011 at 12:26 上午
应该是uwsgi需要的app对象不存在。
[回复]
请问,为什么使用数据库后,就会出现uwsgi application not found,而其他情况下不会,我使用的是 webpy 0.36,uwsgi 0.9.8.6。
[回复]
Dante 回复:
九月 22nd, 2011 at 12:25 上午
这个确认有关系么。。。是不是看一下uwsgi进程启动了没有。。
[回复]
按您文章进行了配置,出现错误,请教问题原因
nginx.conf 配置:
server { # python/uwsgi
listen 80;
root /home/chenkai/webpytest;
error_log /home/chenkai/webpytest/nginx.log;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
index index.html index.htm;
}
}
index.py内容:
import web
urls=(‘/(.*)’,'hello’)
class hello:
def GET(self):
return ‘Hello,world!’
app=web.application(urls,globals())
application=app.wsgifunc()
用uwsgi -s 127.0.0.1:9090 -w index -d uws.error启动
出现错误uws.error:
*** Starting uWSGI 1.0-dev (32bit) on [Mon Oct 24 17:13:38 2011] ***
compiled with version: 4.6.1 on 24 October 2011 16:31:58
current working directory: /home/chenkai/webpytest
detected binary path: /usr/local/bin/uwsgi
*** WARNING: you are running uWSGI without its master process manager ***
your memory page size is 4096 bytes
uwsgi socket 0 bound to TCP address 127.0.0.1:9090 fd 4
Python version: 2.7.2+ (default, Oct 4 2011, 20:29:37) [GCC 4.6.1]
Python main interpreter initialized at 0x91c9b78
your server socket listen backlog is limited to 100 connections
*** Operational MODE: single process ***
WSGI application 0 (mountpoint=”) ready on interpreter 0x91c9b78 pid: 3717 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 3717, cores: 1)
Traceback (most recent call last):
File “/usr/local/lib/python2.7/dist-packages/web.py-0.36-py2.7.egg/web/application.py”, line 237, in process
return self.handle()
File “/usr/local/lib/python2.7/dist-packages/web.py-0.36-py2.7.egg/web/application.py”, line 228, in handle
return self._delegate(fn, self.fvars, args)
File “/usr/local/lib/python2.7/dist-packages/web.py-0.36-py2.7.egg/web/application.py”, line 409, in _delegate
return handle_class(cls)
File “/usr/local/lib/python2.7/dist-packages/web.py-0.36-py2.7.egg/web/application.py”, line 385, in handle_class
return tocall(*args)
TypeError: GET() takes exactly 1 argument (2 given)
[pid: 3717|app: 0|req: 1/1] 60.55.159.142 () {40 vars in 638 bytes} [Mon Oct 24 17:13:46 2011] GET / => generated 30432 bytes in 81 msecs (HTTP/1.1 500) 1 headers in 63 bytes (2 switches on core 0)
[回复]
Dante 回复:
十月 25th, 2011 at 10:11 上午
web.py 我只是试了一下,现在基本不用这个框架了
看报错是GET函数的参数不对。
[回复]
ara 回复:
十月 25th, 2011 at 10:23 上午
晕倒了,居然要这样才可以
def GET(self,name):
基本上网上的例子都是没有name的,非常感谢
最后请教一下现在您在用哪个python的框架?哪个python框架适合新手入门?谢谢
[回复]
Dante 回复:
十月 25th, 2011 at 10:25 上午
我用两个框架
django –适合中型项目
bottle — 适合小型项目和大型项目
[回复]
[...] http://www.vimer.cn/2011/07/linux%E4%B8%8Bnginxpythonuwsgi%E9%83%A8%E7%BD%B2%E6%80%BB%E7%BB%93django... [...]
django和uwsgi及nginx,是每个都要单独启动?
[回复]
Dante 回复:
一月 10th, 2012 at 2:40 下午
uwsgi和nginx都需要单独启动。。。
[回复]
shandy_li 回复:
一月 12th, 2012 at 9:11 下午
django是靠uwsgi来启动还是要单独启动?
[回复]
你知道如果后端uwsgi抛出exception的时候前端nginx会显示502,我如果想限制uwsgi具体的报错信息怎么办(方便调试)
[回复]
Dante 回复:
四月 25th, 2012 at 11:10 下午
没明白,你是说不要显示nginx默认的错误页面还是要显示?
[回复]