Nginx+uWSGI+Django+Python+ MySQL 搭建可靠的Python Web服务器

一、安装所需工具

yum -y install gcc gcc-c++ rpm-build mysql* libtool-ltdl* libtool automake autoconf libtool make setuptool

二、编译安装python2.7.5

1. 下载python2.7.5,保存到 /data/qtongmon/software

http://www.python.org/ftp/python/

2. 解压文件

tar xvf Python-2.7.5.tar.bz2

3. 创建安装目录

mkdir /usr/local/python27

4. 安装python

./configure --prefix=/usr/local/python27 --enable-shared

make

make install

5. 修改老版本的ln指向(注意:这里修改后,可能会影响yum的使用)

mv /usr/bin/python /usr/bin/python2.4.3

ln -s /usr/local/python27/bin/python /usr/bin/python

6.yum是使用python写的,升级新版本的python后会导致yum不可用,如下为运行yum时的错误提示:

There was a problem importing one of the Python modules

required to run yum. The error leading to this problem was:

No module named yum

解决方法:

查找yum文件,并编辑此py文件

[root@TC-Boss ~]# which yum

/usr/bin/yum

[root@TC-Boss ~]# vi /usr/bin/yum

#!/usr/bin/python

改为:

whereis python出来的结果(老版本的python)

#!/usr/bin/python2.6

然后保存OK.

二、安装uWSGI

下载uWSGI的最新版

wget http://projects.unbit.it/downloads/

因为我最后采用xml配置django app 的部署,所以编译 uWSGI 时候需要把libxml编译进去

yum -y install libxml2-devel

剩下的就简单了

tar zxvf uwsgi-1.9.17.tar.gz

cd uwsgi-1.9.17

make

cp uwsgi /usr/sbin/uwsgi

如果遇到错误:python: error while loading shared libraries: libpython2.7.so.1.0:

cannot open shared object file: No such file

按如下方式解决:

vim /etc/ld.so.conf.d/python2.7.conf

加入以下内容:/usr/local/python27/lib (实际的python安装路径下的lib目录)

保存退出后运行:

ldconfig

即可

至此 uWSGI 就算是安装完成了,下一步安装 Nginx

更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2013-10/91063p2.htm

推荐阅读

你应该使用 Nginx + uWSGI  http://www.linuxidc.com/Linux/2013-07/87286.htm

uWSGI + Nginx 部署 Flask Web 应用 http://www.linuxidc.com/Linux/2013-06/85828.htm

Django+Nginx+uWSGI 部署 http://www.linuxidc.com/Linux/2013-02/79862.htm

Linux下Nginx+uWSGI部署Python应用 http://www.linuxidc.com/Linux/2012-10/72443.htm

Ubuntu Server 12.04 安装Nginx+uWSGI+Django环境 http://www.linuxidc.com/Linux/2012-05/60639.htm

CentOS 5.5 + Nginx 0.8.50 + uWSGI + Django 1.2.3 部署Django项目 http://www.linuxidc.com/Linux/2011-05/36399.htm

Nginx+uWSGI+Django+Python+ MySQL 搭建可靠的Python Web服务器

第 2 页(共 2 页)

三、Nginx 安装

Nginx 的详细介绍请点这里
Nginx 的下载地址请点这里

参考CentOS 6.4安装配置Nginx+Pcre+php-fpm  http://www.linuxidc.com/Linux/2013-08/88984.htm

注意编译的时候要加上参数:--with-cc-opt='-O3' --with-cpu-opt=opteron

[root@htuidc nginx-1.4.2]# ./configure --with-http_stub_status_module --prefix=/usr/local/nginx --with-cc-opt='-O3' --with-cpu-opt=opteron

四、安装MySql-python

wget http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz/download

tar zxvf MySQL-python-1.2.3.tar.gz

cd MySQL-python-1.2.3

python setup.py install

ImportError: No module named setuptools 解决方案 :

wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz

tar zxvf setuptools-0.6c11.tar.gz

cd setuptools-0.6c11

python setup.py build

python setup.py install

/usr/bin/ld: cannot find -lpython2.7错误,解决方案 :

cd /usr/lib

ln -s /usr/local/python27/lib/libpython2.7.so libpython2.7.so

ll -a|grep python

lrwxrwxrwx. 1 root root 39 Oct 1 16:39 libpython2.7.so -> /usr/local/python27/lib/libpython2.7.so

drwxr-xr-x. 3 root root 4096 May 3 15:15 python2.6

出错:error: command 'gcc' failed with exit status 1

处理方法:

yum -y install mysql-devel

安装完成,到你的python安装目录下的site-packages目录里检查以下文件是否存在,如果存在即代表安装成功了

Linux:MySQL_python-1.2.3c1-py2.6-linux-i686.egg

测试方法

1)运行命令python进入python运行环境

2)输入以下python代码进行测试

import MySQLdb

test=MySQLdb.connect(db='mydb',host='localhost',user='root',passwd='p')

cur = test.cursor()

cur.execute('show databases;')

for data in cur.fetchall():

print data

五、安装Django

wget https://www.djangoproject.com/m/releases/1.5/Django-1.5.4.tar.gz

tar xzvf Django-1.5.4.tar.gz

cd Django-1.5.4

python setup.py install

测试Django安装

终端中输入 python,进入python交互模式,输入如下:

>>>import django

django.VERSION

正常情况会显示版本号码。

六、nginx配置

server {

listen 80; ## listen for ipv4; this line is default and implied

#listen [::]:80 default ipv6only=on; ## listen for ipv6

server_name blog.hysia.com;

access_log /var/log/nginx/blog.hysia.com-access.log ;

error_log /var/log/nginx/blog.hysia.com-error.log ;

location / {

uwsgi_pass 127.0.0.1:8630;

include uwsgi_params;

}

}

Nginx+uWSGI+Django+Python+ MySQL 搭建可靠的Python Web服务器

上一篇:JS中事件绑定的三种方式


下一篇:常见的Message Queue应用场景