Linux下配置Apache+Mod_Wsgi+Django环境
一.安装环境
操作系统: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://peak.telecommunity.com/dist/ez_setup.py
wgethttp://pypi.python.org/packages/2.4/s/setuptools/setuptools-0.6c11-py2.4.egg
python ez_setup.py
tar –zxvf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
python setup.py build
python setup.py install
安装成功验证:
Shell>python
Python 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
>>>
shell>ls /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
###在最后添加如下内容
<VirtualHost *:80>
ServerName mice.operation.dajie-inc.com
DocumentRoot /var/www/html/dajie
WSGIScriptAlias / /var/www/html/dajie/apache/django.wsgi
<Directory />
Order deny,allow
Allow from all
</Directory>
<Directory /apache>
Allow from all
</Directory>
</VirtualHost>
4.创建并配置wsgi的配置文件:
Shell>cd /var/www/html/dajie
Shell>mkdir apache
Shell>touch django.wsgi
Shell>cat 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.environ['DJANGO_SETTINGS_MODULE'] = 'dajie.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
print >> sys.stderr, sys.path
shell>chmod 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.
# Don't forget to use absolute paths, not relative paths.
'/var/www/html/dajie/templates',
)
四.启动项目
shell>service httpd restart
访问测试:
项目部署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
解决方法:这里apache的Permission denied问题是由SELINUX引起的,关闭SELINUX即可。关闭Selinux:vi /etc/selinux/config===》SELINUX=disabled,然后重启系统
(不重启设置Selinux:setenforce 0 查看Selinux:getenforce)