apache2.4配置Django1.7執行環境

曲珂發表於2014-09-23
系統環境Centos 6.5
這篇文章不適用6以下的系統,因為會碰到這個錯誤
[Mon Sep 22 18:13:02 2014] [error] [client 10.209.75.90] Truncated or oversized response headers received from daemon process 'cms': /var/www/openapi_cms/openapi_cms/wsgi.py, referer: http://10.210.214.237/api-auth/login/?next=/
[Mon Sep 22 18:13:03 2014] [notice] child pid 6107 exit signal Segmentation fault (11)
[Mon Sep 22 18:27:49 2014] [notice] caught SIGTERM, shutting down

  

基礎模組yum安裝好
yum -y install gcc pcre-devel zlib-devel openssl-devel bzip2-devel curl-devel openldap-devel
我們們一共需要這幾個檔案
.
├── get-pip.py
├── httpd-2.4.10.tar.bz2
├── Python-2.7.8.tgz
└── wsgi_4.3.0.tar.gz
安裝apache
解壓httpd,把下面這兩個軟體解壓到httpd/srclib下並重新命名
.
├── apr
├── apr-util
├── Makefile
└── Makefile.in
wget http://mirror.bit.edu.cn/apache/apr/apr-1.5.1.tar.bz2
編譯並安裝httpd
./configure --prefix=/usr/local/apache --with-included-apr && make && sudo make install
 
重新編譯python,不然編譯mod_wsgi會報錯
wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
./configure --prefix=/usr/local/python --enable-shared
make
make install
 
 新增python lib庫配置/etc/ld.so.conf
/usr/local/python/lib
執行ldconf
 
安裝mod_wsgi
./configure --with-apxs=/usr/local/apache/bin/apxs --with-python=/usr/local/python/bin/python
make
make install
 
配置apache
在配置檔案/usr/local/apache/conf/httpd.conf 中增加一行
Include conf/extra/python.conf
編輯python.conf
LoadModule wsgi_module modules/mod_wsgi.so
WSGISocketPrefix /var/run/wsgi
WSGIPassAuthorization On #如果不加這句話 當你用django rest framework寫rest api時,使用者驗證會彈出使用者名稱和密碼並顯示401錯誤

<VirtualHost *:80>

ServerName cms.openapi.com

Alias /static/ /usr/local/apache/htdocs/static/

<Directory /usr/local/apache/htdocs/static>
Require all granted
</Directory>

WSGIDaemonProcess daemon python-path=/usr/local/apache/htdocs/openapi_cms:/usr/local/python/lib/python2.7/site-packages
WSGIProcessGroup daemon
WSGIScriptAlias / /usr/local/apache/htdocs/openapi_cms/openapi_cms/wsgi.py

<Directory /usr/local/apache/htdocs/openapi_cms/openapi_cms>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

</VirtualHost>
 
django配置檔案settings.py增加靜態檔案配置
STATIC_URL = '/static/'
STATIC_ROOT = '/usr/local/apache/htdocs/static/'
 
備註:
django預設的模板載入器不包含egg檔案的載入器
所以python的所有模組最好都用pip安裝 免得給自己找麻煩
pip的安裝地址:wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py
/usr/local/python/bin/python get-pip.py
 
get-pip.py需要python的zlib, openssl

相關文章