首页 > 代码库 > apache2.4配置Django1.7运行环境

apache2.4配置Django1.7运行环境

系统环境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
wget http://archive.apache.org/dist/httpd/httpd-2.4.10.tar.bz2
解压httpd,把下面这两个软件解压到httpd/srclib下并重命名
.
├── apr
├── apr-util
├── Makefile
└── Makefile.in
wget http://mirror.bit.edu.cn/apache/apr/apr-1.5.1.tar.bz2
wget http://mirrors.hust.edu.cn/apache/apr/apr-util-1.5.4.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
 
安装mod_wsgi
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.3.0.tar.gz
./configure --with-apxs=/usr/local/apache/bin/apxs --with-python=/usr/local/python/bin/python
 
配置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

<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

apache2.4配置Django1.7运行环境