---
title: "apache+python+tilecache配置(一)"
author: "cjx186"
date: "2012-11-13T19:15:03Z"
tags: []
categories: ["开源软件"]
slug: "apache-python-tilecache"
draft: false
lastmod: "2012-11-13T19:59:34Z"
id: 1724875
url: "https://xml.iteye.com/blog/1724875"
---

1.安装[httpd-2.2.22-win32-x86-openssl-0.9.8t.msi](http://httpd.apache.org/)
2.安装[python-2.5.msi](http://www.python.org)
3.安装[mod_python-3.3.1.win32-py2.5-Apache2.2.exe](http://www.modpython.org/)
4.配置httpd.conf
<span style="color:green">第一步 加载mod_python</span>
```httpd.conf
LoadModule python_module modules/mod_python.so
```

<span style="color:green">第二步 添加处理程序.在mime_module中加入</span>
```httpd.conf
    AddHandler cgi-script .cgi .pl
    AddHandler mod_python .py
```

```httpd.conf
Include conf/extra/httpd-vhosts.conf
```
<span style="color:green">第三步 配置#TileCache虚拟目录,AddHandler部分如果配了在第二部可以省掉</span>
```httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
    ServerName dummy-host.test.cn
    ServerAlias www.dummy-host.test.cn
    ErrorLog "logs/dummy-host.test.cn-error.log"
    CustomLog "logs/dummy-host.test.cn-access.log" common
    DocumentRoot "D:/server/tilecache-2.11"
    <Directory "D:/server/tilecache-2.11/">
        AddHandler mod_python .py
        AddHandler python-program .py
        PythonHandler TileCache.Service
        PythonOption TileCacheConfig D:/server/tilecache-2.11/tilecache.cfg
        PythonDebug On
        PythonPath "['d:/server/tilecache-2.11/']+sys.path"
        AllowOverride None
        Options Indexes FollowSymLinks Multiviews 
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
```

<span style="color:red">第14行很重要pythonPath如果没配好运行时碰到错误 
> ImportError: No module named TileCache.Service
解决办法在"运行"，输入"Regedit"，打开注册表编辑器，在注册表的

位置下的键值中添加TileCache的位置"D:\server\tilecache-2.11\"</span>
<span style="color:green">第四步 配置TileCache的tilecache.cgi将第一行更改如下，保存后，将扩展名改成.py，配置好缓存</span>
```tilecache.cgi第一行更改
#!D:/dev/Python25/python.exe -u
```


```tilecache.cgi配缓存
[cache]
type=Disk
base=E:/TileCache
```

[查看单片](http://127.0.0.1/tilecache/tilecache.py?LAYERS=basic&FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=-180,0,-90,90&WIDTH=256&HEIGHT=256)
[查看全图](http://127.0.0.1/index.html)
由于在TileCache的源码中还是默认调用了cgi的接口，这里为了彻底摆脱cgi，可以将TileCache目录下的tilecache.py修改为
import urllib2
from TileCache import Service, cgiHandler, cfgfiles, handler

if __name__ == '__main__':
# svc = Service.load(*cfgfiles)
# cgiHandler(svc)
  handler(urllib2.Request)

同样service.py也修改为

if __name__ == '__main__':
# svc = Service.load(*cfgfiles)
# cgiHandler(svc)
  handler(urllib2.Request)