jqjyy 发表于 2011-12-21 08:44

CentOS5.5中快速部署Python应用:Nginx+uWSGI配置详解

<P>环境:CentOS5.5+Nginx1.0+Python2.6.2+uwsgi0.9.7.7 </P>
<P>&nbsp;</P>
<P>1、升级Python<BR>系统自带的Python版本是2.4,我们需要升级到2.6.1,官网号称是被屏蔽了,所以我们需要到网上搜索一个。</P>
<P>下载文件:<BR>&nbsp;<A href="http://download.huihoo.com/python/Python-2.6.1.tar.bz2">http://download.huihoo.com/python/Python-2.6.1.tar.bz2</A><BR>解压:<BR>&nbsp;tar -jxvf Python-2.6.1.tar.bz2</P>
<P>编译:<BR>&nbsp;cd cd Python-2.6.1<BR>&nbsp;./configure<BR>&nbsp;make<BR>&nbsp;make install</P>
<P>自此,python2.6安装后路径默认是在/usr/local/lib/python2.6,查看Python版本:</P>
<P>&nbsp;/usr/local/bin/python2.6 -V</P>
<P>建立软连接,使系统默认的python指向python2.6<BR>正常情况下即使python2.6安装成功后,系统默认指向的python仍然是2.4.3版本,所以我们需要做一个软连接</P>
<P>&nbsp;mv /usr/bin/python&nbsp; /usr/bin/python.bak</P>
<P>&nbsp;ln -s /usr/local/bin/python2.6 /usr/bin/python</P>
<P>检验python指向是否成功<BR>&nbsp;python -V</P>
<P><BR>解决系统python软链接指向python2.6版本后,yum不能正常工作<BR>&nbsp;vi /usr/bin/yum<BR>将文本编辑显示的#/usr/bin/python修改为#/usr/bin/python2.4,保存修改即可</P>
<P><BR>2、安装uwsgi<BR>目前最新的版本是0.9.7.2</P>
<P>下载文件:<BR>&nbsp;wget <A href="http://projects.unbit.it/downloads/uwsgi-0.9.7.2.tar.gz">http://projects.unbit.it/downloads/uwsgi-0.9.7.2.tar.gz</A></P>
<P>uwsgi需要用到libxml2,系统自带的版本无法使用,我们需要升级到libxml2-2.6.26<BR>&nbsp;yum -y install libxml2*</P>
<P>编译:<BR>&nbsp;tar -zxvf uwsgi-0.9.7.2.tar.gz<BR>&nbsp;cd uwsgi-0.9.7.2<BR>&nbsp;make -f Makefile.Py26&nbsp;#使用针对Python2.6的配置文件</P>
<P>编译完成后,目录下会生成可扫行文件 uwsgi,这样编译就算成功了。<BR>我们可以将文件拷到/usr/bin目录,方便使用<BR>&nbsp;mv uwsgi /usr/bin</P>
<P>参考地址:<A href="http://projects.unbit.it/uwsgi/wiki/Install">http://projects.unbit.it/uwsgi/wiki/Install</A></P>
<P>3、安装Nginx</P>
<P>下载文件:<BR>&nbsp;wget <A href="http://nginx.org/download/nginx-1.0.0.tar.gz">http://nginx.org/download/nginx-1.0.0.tar.gz</A></P>
<P>Nginx编译需要pcre和openssl的支持,需要先安装下:<BR>&nbsp;yum -y install pcre-devel openssl openssl-devel</P>
<P>编译:<BR>&nbsp;./configure --prefix=/usr/local/nginx <BR>&nbsp;make<BR>&nbsp;make instal</P>
<P>4、启动uswgi服务</P>
<P>编写一个简单的wsgi程序,myapp.my<BR>def application(environ, start_response):<BR>&nbsp;&nbsp;&nbsp; start_response('200 OK', [('Content-Type', 'text/plain')])<BR>&nbsp;&nbsp;&nbsp; yield 'Hello World\n'</P>
<P>运行uwsgi<BR>&nbsp;uwsgi -s 127.0.0.1:3031 -w myapp</P>
<P>正常情况下,会出现提示信息:<BR>*** Starting uWSGI 0.9.7.2 (32bit) on ***<BR>compiled with version: 4.1.2 20080704 (Red Hat 4.1.2-50) on 27 April 2011 06:40:07<BR>uWSGI running as root, you can use --uid/--gid/--chroot options<BR>&nbsp;*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***<BR>&nbsp;*** WARNING: you are running uWSGI without its master process manager ***<BR>your memory page size is 4096 bytes<BR>uwsgi socket 0 bound to TCP address 127.0.0.1:3031 fd 3<BR>Python version: 2.6.1 (r261:67515, Apr 27 2011, 06:29:38)&nbsp; <BR>Python main interpreter initialized at 0x883ed30<BR>your server socket listen backlog is limited to 100 connections<BR>*** Operational MODE: single process ***<BR>WSGI application 0 (SCRIPT_NAME=) ready on interpreter 0x883ed30 (default app)<BR>*** uWSGI is running in multiple interpreter mode ***<BR>spawned uWSGI worker 1 (and the only) (pid: 1208, cores: 1)</P>
<P><BR>参考地址:<A href="http://projects.unbit.it/uwsgi/wiki/Doc095">http://projects.unbit.it/uwsgi/wiki/Doc095</A></P>
<P>5、配置并启动Nginx<BR>编辑Nginx配置文件<BR>&nbsp;vim /usr/local/nginx/conf/nginx.conf</P>
<P>加入:<BR>&nbsp;location / {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uwsgi_pass&nbsp; 127.0.0.1:3031;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; include uwsgi_params;<BR>在本机3031端口监听。</P>
<P><BR>在浏览器里输入<A href="http://localhost">http://localhost</A> <BR>将提示如下信息:<BR>&nbsp;Hello World<BR>说明平台已经OK了</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P><BR>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
页: [1]
查看完整版本: CentOS5.5中快速部署Python应用:Nginx+uWSGI配置详解