---
title: "Ubuntu下apache跟Django的整合"
author: "duooluu"
date: "2008-11-27T02:05:56Z"
tags: []
slug: "ubuntu-apache-django"
draft: false
lastmod: "2008-11-27T02:05:56Z"
id: 280517
url: "https://duooluu.iteye.com/blog/280517"
---

1. 下载mod_python
 
http://www.modpython.org/
 
Linux一般是以源码的形式提供，对下载的源码需要编译方可使用，先下载下来备用
 
2. 编译mod_python
 
准备：
1）需要apache的开发环境apxs
 
sudo apt-get install apache2-dev
 
自动安装以后的路径：/usr/bin/apxs2

2）安装python的开发环境

sudo apt-get install python-dev

3. 配置和安装mod_python

完成以上两步以后，将下载下来的mod_python解压，在doc-html中有详细的安装文档，按照其中的指导来安装
依次执行下面的命令行，如果没有出现错误基本上就没问题了
./configure --with-apxs=/usr/bin/apxs2
./configure --with-python=/usr/bin/python2.5
./configure --with-mutex-dir=/var/lock/apache2/mod_python
./configure --with-max-locks=32

接下来编译
$make
$sudo make install
如果顺利的话，就算安装成功了。在执行sudo make install的过程中有类似下面的输出提示：
Now don't forget to edit your main config and add
    LoadModule python_module /usr/lib/apache2/modules/mod_python.so
and if your configuration uses ClearModuleList, then also
    AddModule mod_python.c

记住把LoadModule python_module /usr/lib/apache2/modules/mod_python.so加到apache的配置文件中。

4. 配置apache

apache2.conf是一个总的配置文件，我们不去修改它，每个站点各自的配置文件都建立了一个软连接到sites-enabled，上一篇已经说过了。自定义的公共配置我们就放到httpd.conf中，内容如下：

LoadModule python_module /usr/lib/apache2/modules/mod_python.so
PythonOption mod_python.mutex_directory "/var/lock/apache2/"
PythonOption mod_python.mutex_locks 8

5. 配置站点

<VirtualHost 192.168.0.101:80>
        ServerAdmin zhouzcy@gmail.com
        ServerName www.newtest.com
        DocumentRoot /home/dooluu/share/newtest
        <Directory /home/dooluu/share/newtest>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
        Alias /images/ "/home/dooluu/share/newtest/images/"
        <Directory "/home/dooluu/share/newtest/images/">
                Options Indexes MultiViews FollowSymLinks
                AllowOverride None
                Order deny,allow
                Allow from all
        </Directory>
        #control images
        <Location /images/>
                Order allow,deny
                allow from all
        </Location>

        #除了图片都转发到django，PythonPath指定站点的根目录，这里站点在/home/dooluu/share/newtest下，即diango-admin.py创建的工程，那么在PythonPath指定为/home/dooluu/share
        <Location /^(images)>
                SetHandler python-program
                PythonPath " + sys.path"
                PythonHandler django.core.handlers.modpython
                SetEnv DJANGO_SETTINGS_MODULE newtest.settings
                PythonAutoReload On
                PythonDebug On
        </Location>
        ErrorLog /var/log/apache2/newtest.log
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
        CustomLog /var/log/apache2/newtest.log combined
</VirtualHost>

6. 启动django

访问：http://www.newtest.com/t1/

参考：http://trac.edgewall.org/wiki/TracModPython