2022年Linux+Django+Python+Wsgi配置过程 .pdf
Linux 下配置 Apache+Mod_Wsgi+Django环境(ybw 2011-09-23)一安装环境操作系统:CentOS release 5.5 内核版本:Linux Svn-168-1-11 2.6.18-194.el5#1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux Python 版本:Python 2.4.3 Apche 版本:httpd-2.2.3 Mod_Wsgi 版本:mod_wsgi-3.2-1.el5.x86_64.rpm Django 版本:Django-1.2.4 二软件安装Python 采用默认rpm安装路径安装 Apache:yum y install httpd(httpd-2.2.3-53.el5.centos.1.x86_64.rpm)安装 Mod_Wsgi:rpm ivh mod_wsgi-3.2-1.el5.x86_64.rpm 安装 Django:tar zxvf Django-1.2.4.tar.gz cd Django-1.2.4 python setup.py install 安装 MySQLdb wget-q http:/ wget http:/pypi.python.org/packages/2.4/s/setuptools/setuptools-0.6c11-py2.4.eggpython ez_setup.pytar zxvf MySQL-python-1.2.3.tar.gz cd MySQL-python-1.2.3 python setup.py build python setup.py install 安装成功验证:Shell pythonPython 2.4.3(#1,May 5 2011,16:39:10)GCC 4.1.2 20080704(Red Hat 4.1.2-50)on linux2 Type help,copyright,credits or license for more information.import django django.VERSION(1,2,4,final,0)import MySQLdb 名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 4 页 -shellls/etc/httpd/conf.d proxy_ajp.conf README welcome.conf wsgi.conf shell ls/etc/httpd/modules/|grep mod_wsgi mod_wsgi.so三项目部署1.打包压缩开发代码:dajie.zip 2.上传至服务器并解压缩至目录/var/www/html Shell ls/var/www/html/dajie 3.修改 Apche 配置文件httpd.conf Shell vi/etc/httpd/conf/httpd.conf#在最后添加如下内容 ServerName mice.operation.dajie- DocumentRoot/var/www/html/dajie WSGIScriptAlias/var/www/html/dajie/apache/django.wsgi Order deny,allow Allow from all Allow from all 4.创建并配置wsgi 的配置文件:Shellcd/var/www/html/dajie Shellmkdir apache Shelltouch django.wsgi Shellcat django.wsgi import os,sys#Calculate the path based on the location of the WSGI script.apache_configuration=os.path.dirname(_file_)project=os.path.dirname(apache_configuration)workspace=os.path.dirname(project)sys.path.append(workspace)os.environDJANGO_SETTINGS_MODULE=dajie.settings os.environPYTHON_EGG_CACHE=/tmp import django.core.handlers.wsgi application=django.core.handlers.wsgi.WSGIHandler()print sys.stderr,sys.path 名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 4 页 -shellchmod a+x django.wsgi 5.修改 django 项目的配置文件/var/www/html/dajie/settings.py,主要是下面两处:DATABASES=default:ENGINE:django.db.backends.mysql,NAME:dajieod,USER:dajieod,PASSWORD:dajieod123,HOST:127.0.0.1,PORT:3306,TEMPLATE_DIRS=(#Put strings here,like/home/html/django_templates or C:/www/django/templates.#Always use forward slashes,even on Windows.#Dont forget to use absolute paths,not relative paths./var/www/html/dajie/templates,)四启动项目shellservice httpd restart 访问测试:http:/192.168.1.11项目部署ok 五问题集1.安装 MySQL-python 执行:python setup.py build 报错:EnvironmentError:mysql_config not found 方法解决:ln-s/usr/local/mysql/bin/mysql_config/usr/local/bin/mysql_config 将 mysql_confi 从你的安装目录链接到/usr/local/bin目录下,这样就可以在任意目录下访问了(也可以放到/usr/bin)2.import MySQLdb 报错:ImportError:libmysqlclient_r.so.16:cannot open shared object file:No such file or directory 解决方法:将 mysql/lib 下所有关于libmysqlclient 的 so 文件软链接到/usr/lib 下。ln-s/usr/local/mysql/lib/mysql/libmysqlclient*/usr/lib ldconfig 这样import MySQLdb 的时候就不会出错了3.在系统环境中安装完django、MySQLdb后,执行 import 都没有问题,但是启动Apache 后,浏览器访问时却报错:Error loading MySQLdb module:ImportError:libmysqlclient_r.so.16:cannot open shared object file:Permission denied 名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 4 页 -解决方法:这里 apache 的 Permission denied 问题是由SELINUX引起的,关闭 SELINUX即可。关闭 Selinux:vi/etc/selinux/config=SELINUX=disabled,然后重启系统(不重启设置Selinux:setenforce 0 查看 Selinux:getenforce)名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 4 页 -