1、原理
2、安装mod_wsgi
http://pan.baidu.com/s/1sjsccWH
configure的时候会找对应的python脚本,默认是/usr/bin/python
生成mod_wsgi.so,拷贝到apache的modules下面
3、apache的配置:
LoadModule wsgi_module modules/mod_wsgi.so WSGIScriptAlias /myapp /usr/local/sinasrv2/var/www/wsgi-scripts/myapp.wsgi <Directory "/usr/local/sinasrv2/var/www/wsgi-scripts/"> AllowOverride all Options Indexes FollowSymLinks ExecCGI Order allow,deny SetHandler wsgi-script Allow from all </Directory>
4、wsgi的脚本:
#!/usr/bin/env python26 import tornado.web import tornado.wsgi import wsgiref.simple_server import wsgiref.handlers class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world,AAAA,WSGI") def application(environ, start_response): if 'SCRIPT_NAME' in environ: tornado_app = tornado.wsgi.WSGIApplication([ (r"/myapp", MainHandler), ]) return tornado_app(environ,start_response) if __name__ == "__main__": tornado_app = tornado.wsgi.WSGIApplication([ (r"/myapp", MainHandler), ]) server = wsgiref.simple_server.make_server('',6188, application) server.serve_forever()
必须设置application函数,否则会出现下面的错误信息:
[Wed Aug 13 13:39:50 2014] [error] [client 10.217.12.113] mod_wsgi (pid=3309): Exception occurred processing WSGI script '/us r/local/sinasrv2/var/www/wsgi-scripts/myapp.wsgi'
5、测试
6、遇到的问题
[Wed Aug 13 16:29:57 2014] [error] [client 10.217.87.146] Traceback (most recent call last): [Wed Aug 13 16:29:57 2014] [error] [client 10.217.87.146] File "/usr/local/sinasrv2/var/www/wsgi-scripts/myapp.wsgi", line 11, in <module> [Wed Aug 13 16:29:57 2014] [error] [client 10.217.87.146] os.chdir(sys.path[0]) [Wed Aug 13 16:29:57 2014] [error] [client 10.217.87.146] OSError: [Errno 20] Not a directory: '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg'
原因在于调用的时候使用如下代码:
os.chdir(sys.path[0]) sys.path.append("./lib")
apache调用的时候path[0]为
usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg
路径引用的时候使用:
sys.path.append(os.path.dirname(__file__) + os.sep + 'lib')
[参考文献]
1、http://pith.org/notes/2011/06/13/tornado-in-wsgi-mode-on-dotcloud/