您的位置:首页 > 运维架构 > Linux

linux下使用wsgi安装django应用

2012-07-20 16:08 591 查看
前言:由于对linux系统的不了解,装软件什么的总是喜欢用yum命令,结果这次在搭建环境下吃了大亏。网上文章大多是使用mod_python来搭建的,这个只支持到python2.5,现在需要使用python2.7,只好选择mod_wsgi,貌似mod_wsgi的处理效率还要更高一些。

1.      安装环境:

CentOs版本:   CentOs5.7

Python版本:   Python2.7

Apache版本:   Httpd2.2

Django版本: Django1.1

Mod_wsgi版本:    Mod_wsgi-3.2-1.el5.x86_64.rpm

2.      软件安装:

2.1      安装Apache:

一般服务器系统会自带Apache,如果没有可以使用yum安装一下:

yum –y install httpd(httpd-2.2.3-53.el5.centos.1.x86_64.rpm)

2.2      安装Python2.7:

一般linux系统会自带Python,一般是2.4版本的,我们需要重新安装,这里注意,不要使用yum安装,而要用源码编译的方法安装,不然下面在配置之后会出错。

tar -xvf Python-2.7.tar.bz2

  cd Python-2.7

  ./configure  --enable-shared       这里一定要注意,解压完之后要设置enable-shared
make
make install
一般安装到的路径是: /usr/local/lib/python2.7

安装好之后可以执行:# python   查看python是否安装成功,这里可能会遇到错误:

错误:
/usr/local/lib/python2.7/config/libpython2.7.a:  could not read symbols: Bad value

  collect2: ld returned 1 exit status

  apxs:Error: Command failed with rc=65536

这是因为安装python的时候,没有   ./configure  --enable-shared

加上后重新编译,然后运行python,

遇到错误:

python: error while loading shared  libraries: libpython2.7.so.1.0:

  cannot open shared object file: No such file or

  
解决方案: 

新建下面文件

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

  加入内容:

  /usr/local/lib

  保存退出后运行:

  ldconfig

 
再次执行 python,问题成功解决。

2.3      安装Django:

Django的安装比较简单,和windows下一样

tar xzvf  Django-1.1.tar.gz
cd  Django-1.1
sudo python  setup.py install
  
安装完之后,执行命令:

# python

>>>import django

>>>django.VERSION

可以看到Django的版本号,则安装成功。

2.4      安装wsgi

下载wsgi模块,注意要下载linux版本的
http://code.google.com/p/modwsgi/downloads/detail?name=mod_wsgi-3.3.tar.gz&can=2&q=

安装 mod_wsgi 之前先安装apache的apxs扩展:

yum install httpd_devel

 
这个时候有可能发现yum命令用不了,提示:No module named yum

原因是yum命令依赖python2.4,现在用了2.7之后,yum命令就用不了了,解决方法如下:

vim /usr/bin/yum

将  #!/usr/bin/python 修改为 #!/usr/bin/python2.4

安装完httpd_devel之后,开始安装mod_wsgi:

./configure --with-python=/usr/local/bin/python2.7

make

make install 

安装mod_wsgi的时候,建议不要使用yum安装,因为用yum安装很可能无法找到正确的python版本和apxs模块,或者本来没安装apxs模块,用yum安装wsgi也可以成功,但实际运行却会发生很多奇怪的错误,所以建议还是源码编译安装。

3          配置

3.1    配置apache:

Apache的配置文件在: /etc/httpd/conf/httpd.conf

添加:

LoadModule  wsgi_module modules/mod_wsgi.so
注意:LoadModule wsgi_module modules/mod_wsgi.so要跟所有LoadModule 配置放到一起,如果加到最后无法找到这个模块。
这个非常重要,哥哥就是吃这个亏吃大了,调试了一个下午,没结果,各种蛋疼啊。

然后在配置文件中,添加:
</VirtualHost>
NameVirtualHost  *:8080
Listen  192.168.145.139:8080
<VirtualHost  *:8080>
ServerName  www.abc.com
WSGIScriptAlias  / /var/www/html/LTFS_VLA/setting.wsgi
DocumentRoot  /var/www/html/LTFS_VLA
<Directory />
Options FollowSymLinks
AllowOverride
Order allow,deny
Allow from all
</Directory>
 
<Directory /var/www/html/LTFS_VLA/setting.wsgi>
AllowOverride None
Order Allow,Deny
Allow from all
<Files setting.wsgi>
Allow from all
</Files>
</Directory>
Alias /ltfs "/var/www/html/LTFS_VLA" 
<Directory "/var/www/html/LTFS_VLA">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
 

Alias /admin_media  "/usr/local/lib/python2.7/site-pacages/django/conftrib/admin/media"

<Directory "/usr/local/lib/python2.7/site-pacages/django/conftrib/admin/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>
</VirtualHost>
配置中的路径和IP端口等,根据实际需要自行更改。

3.2   配置wsgi:
根据上面的配置,在/var/www/html/LTFS_VLA/  目录下,创建setting.wsgi文件,内容如下:
import os
import sys
sys.stdout=sys.stderr
from os.path  import abspath,dirname,join
from  django.core.handlers.wsgi import WSGIHandler
sys.path.insert(0,abspath(join(dirname(__file__),"./")))
os.environ["DJANGO_SETTINGS_MODULE"]  =  "gui.settings"
application=WSGIHandler()
这个文件的作用是一些环境变量的设置。

4          调试
开启apache服务
Service httpd restart
然后打开浏览器,输入IP地址就可以了,我这里输入的是192.168.145.139:8080。

以上就是基本的配置过程,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息