《Linux+Django+Python+Wsgi配置过程》由会员分享,可在线阅读,更多相关《Linux+Django+Python+Wsgi配置过程(4页珍藏版)》请在金锄头文库上搜索。
1、一安装环境Linux 下配置 Apache+Mod_Wsgi+Django 环境(ybw 2021-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 2021 x86_64 x86_64x86_64 GNU/LinuxPython 版本:Python 2.4.3 Apche 版本: d-2.2.3Mod_Wsgi 版本:mod_wsgi-3.2-1.el5.x86_64.rpmDjango 版本:Django-1.2.4二软件安装Python 承受默
2、认 rpm 安装路径安装 Apache:yum y install d( d-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.4python setup.py installwget -q :/peak.telecommunity /dist/ez_setup.py wget :/pypi.python.org/packages/2.4/s/setuptools/setupt
3、ools-0.6c11-py2.4.eggpython ez_setup.pytar zxvf MySQL-python-1.2.3.tar.gz cdMySQL-python-1.2.3python setup.py buildpython setup.py install安装 MySQLdb安装成功验证:Shell pythonPython 2.4.3 (#1, May5 2021, 16:39:10)GCC 4.1.2 20210704 (Red Hat 4.1.2-50) on linux2Type “help“, “copyright“, “credits“ or “license“
4、 for more information. import django django.VERSION (1, 2, 4, ”final”, 0) import MySQLdbshellls /etc/ d/conf.dproxy_ajp.confREADMEwelcome.confwsgi.confshell ls /etc/ d/modules/ | grep mod_wsgi mod_wsgi.so三工程部署1. 打包压缩开发代码:dajie.zip2. 上传至效劳器并解压缩至名目/var/www/html Shell ls /var/www/html/dajieServerName m
5、ice.operation.dajie-inc DocumentRoot /var/www/html/dajieWSGIScriptAlias / /var/www/html/dajie/apache/django.wsgiOrder deny,allow Allow from allAllow from all3. 修改 Apche 配置文件 d.conf Shell vi /etc/ d/conf/ d.conf #在最终添加如下内容4. 创立并配置 wsgi 的配置文件:Shellcd /var/www/html/dajieShellmkdir apache Shelltouch dja
6、ngo.wsgi Shellcat django.wsgiimport 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.environ”DJANGO_SETTINGS_MODULE” = ”daj
7、ie.settings”os.environ”PYTHON_EGG_CACHE” = ”/tmp”import django.core.handlers.wsgiapplication = django.core.handlers.wsgi.WSGIHandler() print sys.stderr, sys.pathshellchmod a+x django.wsgi5. 修改 django 工程的配置文件/var/www/html/dajie/settings.py,主要是下面两处:DATABASES = ”default”: ”ENGINE”: ”django.db.backends.
8、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.# Don”t forget to use absolute paths, not rela
9、tive paths. ”/var/www/html/dajie/templates”,)四启开工程shellservice d restart 访问测试: :/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/bi
10、n 名目下,这样就可以在任意名目下访问了也可以放到/usr/bin2. 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 的时候就不会出错
11、了3. 在系统环境中安装完 django、MySQLdb 后,执行 import 都没有问题,但是启动 Apache 后, 扫瞄器访问时却报错:Error loading MySQLdb module: ImportError: libmysqlclient_r.so.16: cannot openshared object file: Permission denied解决方法:这里 apache 的 Permission denied 问题是由 SELINUX 引起的,关闭 SELINUX 即可。关闭 Selinux:vi /etc/selinux/config=SELINUX=disabled,然后重启系统不重启设置 Selinux:setenforce 0查看 Selinux:getenforce