一、使用nginx+mod_wsgi
参考:http://moodylee.me/2011/10/13/python_web_programming_1/1、下载软件
mkdir software# cd software/http://nginx.org/en/download.html# wget http://nginx.org/download/nginx-1.2.5.tar.gz错误:这个是用在apache上面的,不是用在nginx上面的# wget http://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz或者#hg clone https://code.google.com/p/modwsgi/也就是mod_wsgi-3.2-1.el6.i686包# rpm -qa|grep mod_wsgimod_wsgi-3.2-1.el6.i686正确为
hg工具需要安装mercurial-hgk# yum -y install mercurial-hgk# hg clone http://bitbucket.org/lifeeth/mod_wsgi/2、安装
1)基本环境./configure: error: the WSGI addon requires Python library.# yum -y install python-devel./configure: error: the HTTP rewrite module requires the PCRE library.# yum -y install pcre-devel2)安装nginx+mod_wsgi#tar -zxvf nginx-1.2.5.tar.gz #cd nginx-1.2.5# ./configure --add-module=/root/software/mod_wsgiconfig通过 nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx configuration prefix: "/usr/local/nginx/conf" nginx configuration file: "/usr/local/nginx/conf/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"# make && make install3、配置mod_wsgi1)基本bin和配置文件# cd /root/software/mod_wsgi安装好后,先执行mod_wsgi中的配置程序setup.py。它会帮你把一些文件拷贝到nginx配置mod_wsgi本身带了一些示例,全部放在代码的examples目录中。配置文件也有一个示例在这个目录中就叫做nginx.conf。如果编译安装nginx不是默认配置,指定了安装目录,则运行本脚本需要指定目录#python setup.py --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf --sbin-path=/usr/local/nginx/sbin如果是默认,则无需# python setup.pyusingprefix: /usr/local/nginxsbin-path: /usr/local/nginx/sbinconf-path: /usr/local/nginx/confdone增加sbin# ll /usr/local/nginx/sbin/total 2908-rwxr-xr-x 1 root root 2956970 Nov 30 12:09 nginx #原来-rwxr-xr-x 1 root root 4584 Nov 30 12:18 ngx_wsgi.py-rwxr-xr-x 1 root root 5322 Nov 30 12:18 ngx_wsgiref.py-rwxr-xr-x 1 root root 3905 Nov 30 12:18 ngx_wsgitest.py配置/usr/local/nginx/conf/wsgi_vars2)ngxin配置文件# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf_bak
# cp /root/software/mod_wsgi/examples/nginx.conf /usr/local/nginx/conf/nginx.confvim /usr/local/nginx/conf/nginx.confworker_processes 2;daemon off;master_process on;error_log logs/error.log info;
pid logs/nginx.pid;events {
worker_connections 1024;}env HOME;
#env PYTHONPATH=/some/path; #修改 项目路径,可不填写env PYTHON_EGG_CACHE=/tmp; #增加 #解决python执行egg包的权限问题http {
#include conf/mime.types; #修改 路径问题 include mime.types; default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] $request '
# '"$status" $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;#keepalive_timeout 0;
keepalive_timeout 65;gzip off;
#ssl on; #ssl_certificate conf/server.crt; #ssl_certificate_key conf/server.key;wsgi_python_optimize 0;
#wsgi_python_executable /usr/bin/python; #wsgi_python_home /usr/; wsgi_enable_subinterpreters on;server {
listen 1234; server_name localhost; #include conf/wsgi_vars; # 修改 路径问题 include wsgi_vars;#wsgi_middleware wsgiref.validate validator;
#wsgi_middleware paste.lint;location / {
#client_body_buffer_size 50; wsgi_pass /usr/local/nginx/nginx.py; #默认没有这个文件,需要拷贝,用于测试wsgi_pass_authorization off;
wsgi_script_reloading on; wsgi_use_main_interpreter on; } } } } 125 #gzip_types text/html text/plain; 需要注释或者下面全部删除3)测试脚本# cp mod_wsgi/examples/nginx.py /usr/local/nginx/nginx.py启动后还报错没有nginx_module这模版注释 #import nginx_module4、启动# /usr/local/nginx/sbin/nginx http://172.16.9.252:1234/会显示一些信息信息5、部署自己的项目1) 404 500页面vim /usr/local/nginx/html/404# cat /usr/local/nginx/html/404.html<h1>Oh Noes!</h1># cat /usr/local/nginx/html/500.html<h1>Oh Bad!</h1>注意确保#vim /usr/lib/python2.6/site-packages/rsync-api/myproject/settings.pyTEMPLATE_DIRS = ( '/usr/local/nginx/html/', )2)自己程序rpm -vih rsync-api-0.0.1-1.noarch.rpm2) 入口程序(1)入口脚本#vim /usr/lib/python2.6/site-packages/rsync-api/main.wsgi # -*- coding: utf-8 -*-import os, syssys.path.append('/usr/lib/python2.6/site-packages/rsync-api')os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'import django.core.handlers.wsgi
_application = django.core.handlers.wsgi.WSGIHandler()
DEBUG = True
def application(environ, start_response): environ['PATH_INFO'] = environ['SCRIPT_NAME'] + environ['PATH_INFO'] return _application(environ, start_response)(2)nginx配置修改vim /usr/local/nginx/conf/nginx.conf location / { #client_body_buffer_size 50; #wsgi_pass /usr/local/nginx/nginx.py;修改为程序脚本 wsgi_pass /usr/lib/python2.6/site-packages/rsync-api/main.wsgi;wsgi_pass_authorization off;
wsgi_script_reloading on; wsgi_use_main_interpreter on; }4)测试测试访问不存在的页面测试访问引起500http://172.16.9.252:1234/rsync/list500打开DEBUG可以看到是权限问题,默认是nobody运行nobody 18982 0.0 0.2 8024 2188 pts/2 S+ 18:01 0:00 nginx: worker process nobody 18983 0.0 0.2 8024 2256 pts/2 S+ 18:01 0:00 nginx: worker process 无法对/etc/rsyncd.conf操作# ll /etc/rsyncd.conf-r-------- 1 root root 5901 Nov 30 14:14 /etc/rsyncd.conf配置修改为# vim /usr/local/nginx/conf/nginx.confuser root;二、使用nginx+uWSGI方式
http://projects.unbit.it/uwsgi/http://projects.unbit.it/uwsgi/wiki/Installhttp://projects.unbit.it/uwsgi/wiki/Quickstarthttp://www.oschina.net/question/54100_30386http://wiki.nginx.org/HttpUwsgiModuleMultipleDynamicApplicationshttp://projects.unbit.it/uwsgi/wiki/Example1、下载# cd software/http://nginx.org/en/download.html# wget http://nginx.org/download/nginx-1.2.5.tar.gzhttp://projects.unbit.it/uwsgi/# wget http://projects.unbit.it/downloads/uwsgi-1.4.2.tar.gz2、安装1) uwsgi# yum -y install libxml2-dev# tar -zxvf uwsgi-1.4.2.tar.gz # cd uwsgi-1.4.2###### make -f Makefile.Py26 #指定你python的版本,如果你的python是2.7 就应该是 make -f Makefile.Py27cp uwsgi /usr/sbin/uwsgi####这种方式老版本方式了# python setup.py --help# python setup.py buildrunning buildrunning build_pycreating build/lib# python setup.py install############## end of uWSGI configuration #############*** uWSGI is ready, launch it with /usr/bin/uwsgi ***running buildrunning build_pyrunning install_libcopying build/lib/uwsgidecorators.py -> /usr/lib/python2.6/site-packagesbyte-compiling /usr/lib/python2.6/site-packages/uwsgidecorators.py to uwsgidecorators.pycrunning install_egg_inforunning egg_infocreating uWSGI.egg-infowriting uWSGI.egg-info/PKG-INFOwriting top-level names to uWSGI.egg-info/top_level.txtwriting dependency_links to uWSGI.egg-info/dependency_links.txtwriting manifest file 'uWSGI.egg-info/SOURCES.txt'reading manifest file 'uWSGI.egg-info/SOURCES.txt'writing manifest file 'uWSGI.egg-info/SOURCES.txt'Copying uWSGI.egg-info to /usr/lib/python2.6/site-packages/uWSGI-1.4.2-py2.6.egg-inforunning install_scripts另外一种安装方式# yum -y install python-pip.noarch # pip-python install pip未成功python uwsgiconfig.py --buildcp uwsgi /usr/sbin/uwsgi
至此 uWSGI 就算是安装完成了,下一步安装 Nginx > 0.8 的版本,因为只有Nginx > 0.8 的版本才支持wsgi
2)、nginx
yum -y install nginx3、配置nginx
如果没有# cp ./nginx/uwsgi_params /etc/nginx/#vi /etc/nginx/conf.d/test-uwsgi.confserver { listen 12345; server_name test.skylog.com; access_log /data/logs/sitelogs/test.skylog.com-access.log ; error_log /data/logs/sitelogs/test.skylog.com-error.log ;location / {
uwsgi_pass 127.0.0.1:8630; include uwsgi_params; }}全部/etc/nginx/nginx.confuser root;worker_processes 2;daemon off; master_process on;#error_log logs/error.log info;
pid /data/logs/nginx.pid;events {
use epoll; worker_connections 1024;}#env HOME;
#env PYTHONPATH=/var/www/rsync;#env PYTHON_EGG_CACHE=/tmp;http {
include mime.types; default_type application/octet-stream;log_format main '$remote_addr - $remote_port - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;#keepalive_timeout 0;
keepalive_timeout 65;gzip off;
#ssl on; #ssl_certificate conf/server.crt; #ssl_certificate_key conf/server.key;##wsgi_python_optimize 2;
##wsgi_python_executable /usr/bin/python; #wsgi_python_home /usr; #wsgi_enable_subinterpreters on; server { listen 12345; server_name test.skylog.com; access_log /data/logs/sitelogs/test.skylog.com-access.log ; error_log /data/logs/sitelogs/test.skylog.com-error.log ;location / {
uwsgi_pass 127.0.0.1:8630; include uwsgi_params; } }}
4、配置uwsgi
socket方式,比http方式好1)配置文件# vim /usr/lib/python2.6/site-packages/rsync-api/myproject.xml <uwsgi> <socket>127.0.0.1:8630</socket> <listen>20</listen> <master>true</master> <pidfile>/tmp/uwsgi.pid</pidfile> <processes>2</processes> <module>wsgi</module> #就是wsgi.py <pythonpath>/usr/lib/python2.6/site-packages/rsync-api/</pythonpath> #项目目录 <profiler>true</profiler> <memory-report>true</memory-report> <enable-threads>true</enable-threads> <logdate>true</logdate> <limit-as>6048</limit-as> </uwsgi>2)加载脚本作用通过加载项目加载到项目和应用也就是上面的<module>wsgi</module># vi /usr/lib/python2.6/site-packages/rsync-api/wsgi.pyimport os,sysif not os.path.dirname(__file__) in sys.path[:1]:
sys.path.insert(0, os.path.dirname(__file__))os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()说明:
os.path.dirname(__file__):本文件所在目录sys.path系统路径 3)启动#uwsgi -x /usr/lib/python2.6/site-packages/rsync-api/myproject.xml 不行成功的提示Sat Dec 1 13:00:01 2012 - WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x9596c78 pid: 21343 (default app)未成功的提示Sat Dec 1 13:00:44 2012 - unable to load app 0 (mountpoint='') (callable not found or import error)Sat Dec 1 13:00:44 2012 - *** no app loaded. going in full dynamic mode **问题原因<module>jin</module>不能项目名相同,不能设置为<module>myproject</module>,也就是加载的脚本也不能设置为myproject.py
也不能叫uwsgimyproject.xml 可以和项目相同规范:
加载脚本:uwsgi 不能是uwsgi或者项目名,不然无法加载完整xml:myproject.xml测试nginx是root这种方式无法读取/etc/rsyncd.conf文件
快速启动方式
uwsgi --socket 127.0.0.1:8630 --pythonpath /usr/lib/python2.6/site-packages/rsync-api --module wsgi --processes 2启动之后杀进程关不掉# kill -9 `cat /tmp/uwsgi.pid`5、启动# /etc/init.d/nginx start 6、测试http://192.168.78.250:12345/7、也可以使用unix socket模式socket = /tmp/i_am_a_unix_socket.sock#vim /usr/lib/python2.6/site-packages/rsync-api/myproject1.xml# cp /usr/lib/python2.6/site-packages/rsync-api/wsgi.py /usr/lib/python2.6/site-packages/rsync-api/rsync.pyuwsgi -x /usr/lib/python2.6/site-packages/rsync-api/myproject1.xmluwsgi --socket /tmp/i_am_a_unix_socket.sock --pythonpath /usr/lib/python2.6/site-packages/rsync-api --module rsync --processes 2module是其中一个app名称也报错# uwsgi --socket /tmp/uwsgi.sock --pythonpath /usr/lib/python2.6/site-packages/rsync-api --module wsgi --processes 2# vim /etc/nginx/nginx.confuwsgi_pass unix:///tmp/uwsgi.sock;