Nginx+uWSGI+DJango+P
本文出自:Http://www.mpyun.com/
一、安装所需工具
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
三、Nginx 安装
参考另一篇文章:http://galean.blog.51cto.com/7702012/1268352
注意编译的时候要加上参数:--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;
}
}
七、配置Django app
配置很简单,几乎不用改动你app的任何文件。
首先在你的app目录创建个wsgi.py 文件,内容如下:
import os,sys
if not os.path.dirname(__file__) in sys.path[:1]:
sys.path.insert(0, os.path.dirname(__file__))
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
然后在app目录创建个django.xml文件,作为uWSGI运行的配置文件,内容如下:
<uwsgi>
<Socket>127.0.0.1:8630</socket>
<chdir>/data/WEB/vpssite</chdir>
<pythonpath>..</pythonpath>
<module>wsgi</module>
</uwsgi>
最后一步,运行 uWSGI 就行了,如下:
uwsgi -x /data/web/vpssite/django.xml
这样你的Django app 就用 uWSGI hold住了。django.xml的配置远不止这些比如log文件,内存限制等等,具体的可以参考 http://projects.unbit.it/uwsgi/wiki/Example
相关文章