linux下django部署到apache

用django写了个demo,目前要部署到apache上


需求:

django部署到apache


【其中变量$USER为你的账号】

1.安装apache 和 django

A.下载apache:

http://httpd.apache.org/download.cgi#apache22

版本2.2.22

B.解压

tar -xzvf httpd-2.2.22.tar.gz

C.进入解压后文件夹make & install

./configure --prefix=/home/$USER/apache

make

make install

D.改变目录所有者和权限

cd /home/$USER/apache

chown -R nobody:root htdocs 

chmod -R 755 *

测试是否安装成功:
cd /home/$USER/apache
./bin/apachectl start
http://127.0.0.1/
看到It works!即安装成功

E.安装django

下载:http://www.djangoproject.com/download/

tar xzvf Django-*.tar.gz

cd Django-*

sudo python setup.py install

>>> import django
>>> django.VERSION
(1, 1, 0, final', 1)

参考:http://djangobook.py3k.cn/2.0/chapter02/


2.安装mod_python

yum install mod_python

rpm -ql mod_python | grep so

然后把安装后的mod_python.so拷贝到 apache安装目录下的modules


或者按照啄木鸟社区的文档,使用编译的方式安装:

http://wiki.woodpecker.org.cn/moin/modpythoncn


3.httpd.conf配置:

A.基本配置:

Listen 80 //apache默认端口,可以修改   

DocumentRoot "/home/$USER/apache/htdocs" //用户默认访问的apache目录   
  
//日志输出的2种格式    
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined   
LogFormat "%h %l %u %t \"%r\" %>s %b" common   
  
CustomLog logs/access_log common //访问日志:access_log的输出采用common格式


B.配置django:

我的项目文件夹在:/home/$USER/lingbo

配置:

LoadModule python_module modules/mod_python.so
<Location "/lingbo/">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE lingbo.settings
    PythonOption django.root /lingbo
    PythonDebug On
    PythonPath "['/home/lingyue.wkl'] + sys.path"
</Location>


说明:

LoadModule python_module modules/mod_python.so  #加载module部分
<Location "/lingbo/">   #项目名  http://localhost:80/lingbo/
    SetHandler python-program
    PythonHandler django.core.handlers.modpython  
    SetEnv DJANGO_SETTINGS_MODULE lingbo.settings 
    PythonOption django.root /lingbo
    PythonDebug On
    PythonPath "['/home/username'] + sys.path"    #实际项目放的位置的上一级目录
</Location>




D.配置css,img等资源文件的访问权限

若是没有对这类静态文件的路径进行配置,apache是没有权限获取的,导致页面展现异常

配置方式:

在httpd.conf中配置

Alias /css /home/username/lingbo/templates/css
<Location "/css">
  SetHandler None
</Location>



4.其他:

可能还需要修改urls.py中的路径匹配设置

将mysite加在各个匹配项前。 如  (r'^address/', include('mysite.address.urls')),
改为
    (r'^lingbo/address/', include('mysite.address.urls')),


5.最后

./bin/apachectl start

http://localhost:80/lingbo/



问题1:

Syntax error on line 44 of /home/lingyue.wkl/apache/conf/httpd.conf:
Invalid command 'PythonHandler', perhaps misspelled or defined by a module not included in the server configuration

原因:未配置mod_python

解决:

配置文件中加入  LoadModule python_module modules/mod_python.so


问题2:

client denied by server configuration

原因:无权限访问某些资源

解决:

# 对需要访问的区域,可以增加正确的Directory块

<Directory “/home/usrname/lingbo/dd”>
Order Deny,Allow
Allow from all
</Directory>

Meet so Meet. C plusplus I-PLUS....
原文地址:https://www.cnblogs.com/iplus/p/4464654.html