Windows下Trac搭建配置---modwsgi配置项

  • 概述

最近需要使用一下Trac,因此查找了许多相关的资料,在经历了漫长的配置之后总算是成功了。现在将配置过程记录一下,方便以后查阅。本文主要记录怎么将安装好的Trac配置成可供多人访问的网络服务器的模式。Trac配置中使用的是modwsgi和Apache来进行配置的。

  • 基本安装

本文中使用的是Apache2.2,可以在Apache官网上下载:http://www.apache.org/

使用的modwsgi是3.5的版本,可以在下面的网站中下载:

源码:https://code.google.com/p/modwsgi/wiki/DownloadTheSoftware?tm=2

编译好的版本(Windows):http://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi

  1. 在Windows下安装完Apache(直接安装exe文件,一直下一步完成)
  2. 将下载好的mod_wsgi.so文件拷贝到Apache的安装目录下modules文件夹之中
  3. 在Apache安装目录中找到它的配置文件(conf文件夹下面的httpd.conf文件)并用编辑器打开
  4. 在一大段的LoadModule后面添加一行:LoadModule wsgi_module modules/mod_wsgi.so
  • 基本配置

WSGI是一个Python的标准PEP 0333用来将网络服务器抽象称为Python的Web Application。一个简单的WSGI程序如下所示:

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]
当执行这段代码的时候,相当于用户在使用网页服务器(如Apache)一样,为此Apache服务器必须可以访问这段Python的脚本以及脚本中涉及到所有的文件夹和文件。

为了让Apache可以访问到这一段脚本,我们需要给之一段脚本一个URL,在Apache的配置文件中设置:

WSGIScriptAlias /myapp /usr/local/www/wsgi-scripts/myapp.wsgi
这一行可以放在Apache配置文件的顶层中(不在任何标签之下),也可以放在VirtualHost标签下面作为某一个特殊用途的配置和其他配置内容放在一起。

这一行中第一个参数是WSGI脚本的URL,这个URL中不能包含结尾的斜杠(不能写成/myapp/的方式),如果将WSGI映射成为网页服务器的Root位置,则需要直接用一个斜杠

第二个参数是WSGI脚本文件的绝对路径

如果WSGI脚本放在Apache可以访问的文件夹之外的位置,那么需要告诉Apache这个存放WSGI脚本的文件夹是可以被访问的,需要添加:

<Directory /usr/local/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>

第一行中 /usr/local/www/wsgi-scripts是存放WSGI脚本文件的文件夹的绝对路径(在Windows下写成如:C:/usr/local/www/wsgi-scripts这样的方式)

综上所述,一个完整的mod_wsgi虚拟主机配置如下:

<VirtualHost *:80>

    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin [email protected]

    DocumentRoot /usr/local/www/documents

    <Directory /usr/local/www/documents>
    Order allow,deny
    Allow from all
    </Directory>

    WSGIScriptAlias /myapp /usr/local/www/wsgi-scripts/myapp.wsgi

    <Directory /usr/local/www/wsgi-scripts>
    Order allow,deny
    Allow from all
    </Directory>

</VirtualHost>
当所有这些配置写到Apache配置文件中之后,需要重新启动Apache服务器,这时候通过类似于'http://www.example.com/myapp'这样的网址就能访问到我们的WSGI脚本了。

  • Trac中的配置

首先我们需要在Trac中也写一个与上面类似的WSGI脚本,并且让这个脚本文件映射称为一个网络地址,在Trac中可以通过Trac-admin工具来为我们自动产生这样一个文件:

mkdir -p /usr/share/trac/projects/my-project
trac-admin /usr/share/trac/projects/my-project initenv
trac-admin /usr/share/trac/projects/my-project deploy /tmp/deploy
mv /tmp/deploy/* /usr/share/trac (拷贝/tmp/deploy/文件夹中的所有内容到/user/share/trac中)

这样在/user/share/trac中就存在一个trac.wsgi文件了。为什么要这么做,可以参考Trac中的解释:

There is, however, a bit of a chicken-and-egg problem. The trac-admin command requires an existing environment to function, 
but complains if the deploy directory already exists. This is a problem, because environments are often stored in a subdirectory of the deploy. 
The solution is to do something like this: //上面的处理方式

我们接下来需要做的就是和上面一样,配置Apache的配置文件:

WSGIScriptAlias /trac /usr/share/trac/cgi-bin/trac.wsgi

<Directory /usr/share/trac/cgi-bin>
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>
配置完成之后重启Apache服务器,就可以访问Trac站点了,在本例中应该使用如http://xxxx:xx/trac的网址来访问。

你可能感兴趣的:(Trac,mod_wsgi)