之前使用Nginx+Fastcgi来搭建python web服务器,本文介绍Nginx+UWSGI组合来实现。uWSGI 是一个快速的、纯C语言开发的、自维护的、对开发者友好的WSGI服务器,旨在提供专业的 Python web应用发布和开发。它更符合python web的标准协议,速度要比Fastcgi要快、性能更加稳定。

这里有一组性能测试对比 http://www.peterbe.com/plog/fcgi-vs-gunicorn-vs-uwsgi

一、安装软件
1、安装pcre
cd /opt
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.31.tar.gz
tar zxvf pcre-8.31.tar.gz
cd pcre-8.31
./configure
make && make install

cd ..
2、安装nginx
tar zxvf nginx-1.2.4.tar.gz
cd nginx-1.2.4

./configure --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-pcre=../pcre-8.31/

make && make install

3、yum安装mysql
yum -y install mysql*
/etc/init.d/mysqld start

4、安装python
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
tar jxvf Python-2.7.3.tar.bz2
cd Python-2.7.3
./configure 
make && make install
改下yum的py版本,不然yum报错
sed -i '1s/\/usr\/bin\/python/\/usr\/bin\/python2.4/1' /usr/bin/yum 

5、安装MySql-python
wget http://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3.tar.gz
python setup.py install

6、安装Django
wget http://www.djangoproject.com/m/releases/1.4/Django-1.4.2.tar.gz
tar zxvf Django-1.4.2.tar.gz
cd Django-1.4.2
python setup.py install

7、安装uWSGI
wget http://projects.unbit.it/downloads/uwsgi-1.3.tar.gz
tar zxvf uwsgi-1.3.tar.gz
cd uwsgi-1.3
make
cp uwsgi /usr/bin
 
二、平台配置:项目位置:/opt/www/Purgesys
1、mkdir -p /opt/www/
创建项目方法:
django-admin.py startproject Purgesys
 
查看项目(出现以下文件结构说明项目已创建成功)
tree Purgesys
[root@localhost www]# tree Purgesys
Purgesys
|-- Purgesys
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- settings.py
|   |-- settings.pyc
|   |-- urls.py
|   |-- urls.pyc
|   `-- wsgi.py
|-- django_wsgi.py
|-- django_wsgi.pyc
`-- manage.py

1 directory, 10 files

2、nginx配置
cd /usr/local/nginx/conf
vim django_uwsgi.conf
server {   
        listen  80;   
        server_name 10.1.20.151;   
  
        location / {   
            uwsgi_pass   127.0.0.1:9000;   
            include     uwsgi_params;   
            access_log  off;   
        }   
  
        location ^~ /static {   
                root   /opt/www/Purgesys;   
        }   
  
        location ^~ /admin/ {   
            uwsgi_pass   127.0.0.1:9000;   
            include  uwsgi_params;   
            access_log   off;   
        }   
  
        location ~* ^.+\.(mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg)$ {   
            root   /opt/www/Purgesys/static;   
            access_log   off;   
        }   
  
}   

vim nginx.conf
http{   
........   
include django_uwsgi.conf;
}
 
3、uWSGI配置
mkdir -p /home/uwsgi;cd /home/uwsgi
mkdir -p /opt/www/logs 
echo "" > /opt/www/logs/django.log

vim uwsgi.xml
 
  127.0.0.1:9000  
  200  
  true  
  /usr/local/nginx/uwsgi.pid  
  8  
  /opt/www/Purgesys  
  /opt/www/  
  django_wsgi  
  true  
  true  
  true  
  true  
  2048  
  /opt/www/logs/django.log  
 

4、创建应用模块
cd /opt/www/Purgesys
vi django_wsgi.py
import os  
os.environ['DJANGO_SETTINGS_MODULE'] = 'Purgesys.settings'   
import django.core.handlers.wsgi   
application = django.core.handlers.wsgi.WSGIHandler()   

5、启动服务
/usr/bin/uwsgi -x /home/uwsgi/uwsgi.xml
uwsgi: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决方法:
64位系统 # ln -s /usr/local/lib/libpcre.so.1 /lib64
32位系统 # ln -s /usr/local/lib/libpcre.so.1 /lib

/usr/local/nginx/sbin/nginx

6、查看进程
[root@localhost ~]#  ps -ef|grep uwsgi|grep -v grep
root     13035     1  0 10:25 ?        00:00:00 uwsgi -x /home/uwsgi/uwsgi.xml
root     13036 13035  0 10:25 ?        00:00:00 uwsgi -x /home/uwsgi/uwsgi.xml
root     13037 13035  0 10:25 ?        00:00:00 uwsgi -x /home/uwsgi/uwsgi.xml
root     13038 13035  0 10:25 ?        00:00:00 uwsgi -x /home/uwsgi/uwsgi.xml
root     13039 13035  0 10:25 ?        00:00:00 uwsgi -x /home/uwsgi/uwsgi.xml
root     13040 13035  0 10:25 ?        00:00:00 uwsgi -x /home/uwsgi/uwsgi.xml
root     13041 13035  0 10:25 ?        00:00:00 uwsgi -x /home/uwsgi/uwsgi.xml
root     13042 13035  0 10:25 ?        00:00:00 uwsgi -x /home/uwsgi/uwsgi.xml
root     13043 13035  0 10:25 ?        00:00:00 uwsgi -x /home/uwsgi/uwsgi.xml

7、访问测试
输入http://ip/