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
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
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-inc.com
DocumentRoot /var/www/html/dajie
WSGIScriptAlias / /var/www/html/dajie/apache/django.wsgi
Order deny,allow
Allow from all
Allow from all
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)