今天小编跟大家讲解下有关科技常识:Centos5.4+Nginx-0.8.50+UWSGI-0.9.6.2+Django-1.2.3搭建高性能WEB服务器,相信小伙伴们对这个话题应该也很关注吧,小编也收集到了有关科技常识:Centos5.4+Nginx-0.8.50+UWSGI-0.9.6.2+Django-1.2.3搭建高性能WEB服务器的相关资料,希望小伙伴会喜欢也能够帮助大家。
之前一直使用Nginx+Fastcgi来搭建python web服务器,本文介绍Nginx+UWSGI组合来实现。uWSGI 是一个快速的、纯C语言开发的、自维护的、对开发者友好的WSGI服务器,旨在提供专业的 Python web应用发布和开发。它更符合python web的标准协议,速度要比Fastcgi要快、性能更加稳定。
一、安装平台1、安装pcre
复制代码 代码如下:cd /homemkdir -p /home/install/nginx && cd /home/install/nginxwget http://ftp.exim.llorien.org/pcre/pcre-8.00.tar.gztar -zxvf pcre-8.00.tar.gzcd pcre-8.00./configuremake && make installcd ..
2、安装Nginx
复制代码 代码如下:wget http://nginx.org/download/nginx-0.8.50.tar.gztar -zxvf nginx-0.8.50.tar.gzcd nginx-0.8.50/./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-cc-opt='-O3' --with-cpu-opt=opteronmake && make installcd ..
3、安装python&Mysql-python
复制代码 代码如下:wget http://www.python.org/ftp/python/2.5.1/Python-2.5.1.tgztar -zxvf Python-2.5.1.tgzcd Python-2.5.1./configure && make && make installecho"export PATH=\$PATH:/usr/local/bin">> /etc/profilesource /etc/profile
4、安装MySql-python
复制代码 代码如下:cd ../wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.2/MySQL-python-1.2.2.tar.gz?r=&ts=1285248455&use_mirror=nchccd MySQL-python-1.2.2python setup.py install
5、安装Django
复制代码 代码如下:wget http://www.djangoproject.com/download/1.2.3/tarball/tar -zxvf Django-1.2.3.tar.gzcd Django-1.2.3python setup.py installcd ..
6、安装uWSGI
复制代码 代码如下:wget http://projects.unbit.it/downloads/uwsgi-0.9.6.2.tar.gztar -zxvf uwsgi-0.9.6.2.tar.gzcd uwsgi-0.9.6.2makecp uwsgi /usr/bin
二、平台配置
1、平台环境说明 1.1、项目位置:/opt/www/Purgesys 创建项目方法:
复制代码 代码如下:cd /opt/wwwdjango-admin.py startproject Purgesys
1.2、查看项目(出现以下文件结构说明项目已创建成功) #tree Purgesys
复制代码 代码如下:Purgesys|-- __init__.py|-- manage.py|-- settings.py`-- urls.py
2、Nginx相关配置
#cd /usr/local/nginx/conf#vi django_uwsgi.conf
# Django projectserver {listen80;server_name purgesys.domain.com;location / {uwsgi_pass 127.0.0.1:9000;include uwsgi_params;access_logoff;}location ^~ /static {root /opt/www/Purgesys;}location ^~ /admin/ {uwsgi_pass 127.0.0.1:9000;includeuwsgi_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;}}*Nginx-0.8.50默认已添加uwsgi输出头支持include uwsgi_params;
#vi nginx.conf追加子配置文件django_uwsgi.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#vi uwsgi.xml
<uwsgi> <socket>127.0.0.1:9000</socket> <listen>200</listen> <master>true</master> <pidfile>/usr/local/nginx/uwsgi.pid</pidfile> <processes>8</processes> <pythonpath>/opt/www/Purgesys</pythonpath> <pythonpath>/opt/www/</pythonpath> <module>django_wsgi</module> <profiler>true</profiler> <memory-report>true</memory-report> <enable-threads>true</enable-threads> <logdate>true</logdate> <limit-as>6048</limit-as> <daemonize>/opt/www/logs/django.log</daemonize></uwsgi>4、创建应用模块cd /opt/www/Purgesysvi django_wsgi.py
import osos.environ['DJANGO_SETTINGS_MODULE'] = 'Purgesys.settings'import django.core.handlers.wsgiapplication = django.core.handlers.wsgi.WSGIHandler()5、启动服务#/usr/bin/uwsgi -x /home/uwsgi/uwsgi.xml#/usr/local/nginx/sbin/nginx
6、查看进程#[liuts@webserver nginx]# ps -ef|grep uwsgi|grep -v grep
复制代码 代码如下:root 21652 1 0 17:12 ? 00:00:00 /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xmlroot 21653 21652 0 17:12 ? 00:00:00 /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xmlroot 21654 21652 0 17:12 ? 00:00:00 /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xmlroot 21655 21652 0 17:12 ? 00:00:00 /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xmlroot 21656 21652 0 17:12 ? 00:00:00 /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xmlroot 21657 21652 0 17:12 ? 00:00:00 /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xmlroot 21658 21652 0 17:12 ? 00:00:00 /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xmlroot 21659 21652 0 17:12 ? 00:00:00 /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xmlroot 21660 21652 0 17:12 ? 00:00:00 /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xml
7、监听端口#[liuts@webserver nginx]# netstat -an|grep 9000引用
tcp 0 127.0.0.1:9000 0.0.0.0:* LISTEN
8、访问测试输入http://serverip/
大功告成!
*附uwsgi启动脚本#cd /home/uwsgi#vi uwsgiserver.sh
#!/bin/bashif [ $1 = start ];then psid=`ps aux|grep"uwsgi"|grep -v"grep"|wc -l` if [ $psid -gt 2 ];then echo"uwsgi is running!"exit 0 else /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xml fi echo"Start uwsgi service [OK]"elif [ $1 = stop ];then killall -9 uwsgi echo"Stop uwsgi service [OK]"elif [ $1 = restart ];then killall -9 uwsgi /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xml echo"Restart uwsgi service [OK]"else echo"Usages: sh uwsgiserver.sh [start|stop|restart]"fi#sh uwsgiserver.sh start[uWSGI] parsing config file /home/uwsgi/uwsgi.xmlStart uwsgi service [OK]
如大家有什么疑问或感兴趣的话题可以通过weibo与我交流:http://t.qq.com/yorkoliu
以下是补充:
1、MySQL-python 安装错误如下from setuptools import setup ImportError:
先安装python ez_setup.py
在安装python setup.py install
贴一下,方便其他兄弟了
2、我的nginx+uwsgi+django遇到大header的request,经常出现502,uwsgi报的是 invalid request header size,何解我的uwsgi配置为[uwsgi]socket = :8010master = truemodule = django_wsgiprocesses = 8home = /path/to/virtualenvharakiri = 20daemonize = /path/to/uwsgi.logpidfile = /path/to/uwsgi.pidpythonpath = /path/to/projectpythonpath = /path/to/uwsgibuffer-size = 32768reload-mercy = 8max-requests = 5000socket-timeout = 4
解决方法:大header可以修改nginx http head部分参数。
3、Nginx报502,但是uwsgi正常启动端口9000也正常监听。有可能是应用程序有错误导致。
来源:爱蒂网