发布时间:2014-09-05 17:16:56作者:知识屋
之前一直使用Nginx+Fastcgi来搭建python web服务器,本文介绍Nginx+UWSGI组合来实现。uWSGI 是一个快速的、纯C语言开发的、自维护的、对开发者友好的WSGI服务器,旨在提供专业的 Python web应用发布和开发。它更符合python web的标准协议,速度要比Fastcgi要快、性能更加稳定。
一、安装平台
1、安装pcre
引用
cd /home
mkdir -p /home/install/nginx && cd /home/install/nginx
wget http://ftp.exim.llorien.org/pcre/pcre-8.00.tar.gz
tar -zxvf pcre-8.00.tar.gz
cd pcre-8.00
./configure
make && make install
cd ..
2、安装Nginx
引用
wget http://nginx.org/download/nginx-0.8.50.tar.gz
tar -zxvf nginx-0.8.50.tar.gz
cd 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=opteron
make && make install
cd ..
3、安装python&Mysql-python
引用
wget http://www.python.org/ftp/python/2.5.1/Python-2.5.1.tgz
tar -zxvf Python-2.5.1.tgz
cd Python-2.5.1
./configure && make && make install
echo “export PATH=$PATH:/usr/local/bin” >> /etc/profile
source /etc/profile
4、安装MySql-python
引用
cd ../
wget mysql-python/mysql-python/1.2.2/MySQL-python-1.2.2.tar.gz?r=&ts=1285248455&use_mirror=nchc">http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.2/MySQL-python-1.2.2.tar.gz?r=&ts=1285248455&use_mirror=nchc
cd MySQL-python-1.2.2
python setup.py install
5、安装Django
引用
wget http://www.djangoproject.com/download/1.2.3/tarball/
tar -zxvf Django-1.2.3.tar.gz
cd Django-1.2.3
python setup.py install
cd ..
6、安装uWSGI
引用
wget http://projects.unbit.it/downloads/uwsgi-0.9.6.2.tar.gz
tar -zxvf uwsgi-0.9.6.2.tar.gz
cd uwsgi-0.9.6.2
make
cp uwsgi /usr/bin
二、平台配置
1、平台环境说明
1.1、项目位置:/opt/www/Purgesys
创建项目方法:
引用
cd /opt/www
django-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
view plaincopy to clipboardprint?
# Django project
server {
listen 80;
server_name purgesys.domain.com;
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;
}
}
# Django project server { listen 80; server_name purgesys.domain.com; 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; } }
*Nginx-0.8.50默认已添加uwsgi输出头支持
include uwsgi_params;
#vi nginx.conf
追加子配置文件django_uwsgi.conf
http{
……..
include 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
view plaincopy to clipboardprint?
<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>
<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/Purgesys
vi django_wsgi.py
view plaincopy to clipboardprint?
import os
os.environ[DJANGO_SETTINGS_MODULE] = ‘Purgesys.settings’
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
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
#/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.xml
root 21653 21652 0 17:12 ? 00:00:00 /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xml
root 21654 21652 0 17:12 ? 00:00:00 /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xml
root 21655 21652 0 17:12 ? 00:00:00 /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xml
root 21656 21652 0 17:12 ? 00:00:00 /usr/bin/uwsgi -x /home/uwsgi/uwsgi.xml
root 21657 216
linux一键安装web环境全攻略 在linux系统中怎么一键安装web环境方法
Linux网络基本网络配置方法介绍 如何配置Linux系统的网络方法
Linux下DNS服务器搭建详解 Linux下搭建DNS服务器和配置文件
对Linux进行详细的性能监控的方法 Linux 系统性能监控命令详解
linux系统root密码忘了怎么办 linux忘记root密码后找回密码的方法
Linux基本命令有哪些 Linux系统常用操作命令有哪些
Linux必学的网络操作命令 linux网络操作相关命令汇总
linux系统从入侵到提权的详细过程 linux入侵提权服务器方法技巧
linux系统怎么用命令切换用户登录 Linux切换用户的命令是什么
在linux中添加普通新用户登录 如何在Linux中添加一个新的用户
2012-07-10
CentOS 6.3安装(详细图解教程)
Linux怎么查看网卡驱动?Linux下查看网卡的驱动程序
centos修改主机名命令
Ubuntu或UbuntuKyKin14.04Unity桌面风格与Gnome桌面风格的切换
FEDORA 17中设置TIGERVNC远程访问
StartOS 5.0相关介绍,新型的Linux系统!
解决vSphere Client登录linux版vCenter失败
LINUX最新提权 Exploits Linux Kernel <= 2.6.37
nginx在网站中的7层转发功能