在IIS上运行地图服务器

作者

塞思吉尔文

联系

在Compass.ie的sGirvin

最后更新

2022-04-04

本文详细介绍了如何配置预置版本的MapServer以在IIS(Microsoft的Web服务器-Internet信息服务)上运行。这些步骤的最低要求是IIS 7和Windows 7,并且已经在运行Windows 10的IIS 10上进行了测试。

MapServer也可以使用在Windows上运行 MS4W (用于Windows的MapServer)-安装其自己的预配置的ApacheWeb服务器,以及其他相关工具,如PHP、TinyOWS和MapCache。在部署到安装附加Web服务器受到限制的服务器,或者系统管理员在维护IIS方面经验较丰富的服务器上部署时,可能需要在IIS下运行MapServer。

备注

MapServer可执行文件来自 MS4W can also be used under IIS as well; for more information see the section in MS4W's README ,或向MS4W社区寻求帮助 (subscribe

地图服务器设置

预编译版本的地图服务器和GDAL(必需依赖项)可从http://www.gisinternals.com/release.php.下载这些下载是由Tamas Szekeres构建和维护的,他是MapServer的长期贡献者,也是项目指导委员会(PSC)的成员。

备注

我应该下载哪个版本?

  • 除非在映射文件中使用不推荐使用的功能,否则应下载最新的稳定版本。

  • 现在几乎所有服务器都将采用x64(64位)体系结构,因此除非您被限制为只能使用Win32(32位)服务器,否则请下载x64而不是Win32发行版。

  • 除非您有充分的理由,否则您应该使用最新的编译器版本。MSVC 2022表示二进制文件是使用Visual C++2022编译的。

  • 本安装指南使用 “在单个.zip包中编译的二进制文件” 下载(release-1911-x64-gdal-3-2-1-mapserver-7-6-2.zip).也有一个 “支持IIS注册的MapServer安装程序” 选项,它有一个内置的安装程序。

下载zip文件后,请按照以下步骤操作。

  1. 将文件夹解压缩到您的计算机。在本例中,我们将假设它已被解压缩为 C:\MapServer 。确保您已经“解锁”压缩,以避免和安全相关的问题。右键单击压缩文件,选择属性,然后单击General选项卡上的“Unblock”按钮。

    ../_images/iis-unblock-zip.png
  2. 通过双击测试MapServer是否工作 C:\MapServer\SDKShell.bat 要启动配置了MapServer环境设置的新Windows命令会话,请键入:

    mapserv -v
    

    mapserver版本和支持的格式类型应该显示在命令窗口中。

使用IIS配置

这些步骤假定开发计算机上安装了IIS 7或更高版本,并且MapServer使用默认网站位置 C:\Inetpub\wwwroot 。MapServer安装为 FASTCGI 应用程序,它允许MapServer在请求之间共享数据库连接等对象,从而提高了性能。

所有命令行步骤都应该是“以管理员身份运行”(要选择此选项,请右键单击Windows命令行图标)。确保计算机上有IIS-如果您有名为的文件夹 C:\Inetpub 则很可能已经安装了IIS。

您可以通过以下命令行安装IIS(适用于Windows 10):

dism /online /enable-feature /featurename:IIS-WebServer

有关使用IIS设置FastCGI应用程序的更多详细信息,请参阅 Microsoft FastCGI documentation

  1. 首先将必需的.exes复制到与mapserver dll相同的文件夹中。可以使用以下命令完成此操作:

    xcopy C:\MapServer\bin\ms\apps\* C:\MapServer\bin
    
  2. 创建新文件夹 C:\Inetpub\wwwroot\mapserver

    md C:\Inetpub\wwwroot\mapserver
    
  3. 创建一个新的、空的 C:\Inetpub\wwwroot\mapserver\web.config 文件,然后(以管理员身份)在记事本中打开:

    copy NUL C:\Inetpub\wwwroot\mapserver\web.config
    notepad C:\Inetpub\wwwroot\mapserver\web.config
    
  4. 按以下配置粘贴:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <handlers>
                <add name="MapServerFastCgi" path="*" verb="*" type="" modules="FastCgiModule" scriptProcessor="C:\Mapserver\bin\mapserv.exe"
                resourceType="Unspecified" requireAccess="Script" allowPathInfo="true" preCondition=""  />
            </handlers>
            <caching enabled="true" enableKernelCache="true" />
        </system.webServer>
    </configuration>
    
  5. 下一步在IIS中设置Web应用程序:

    "%systemroot%\system32\inetsrv\appcmd" add app /site.name:"Default Web Site" /path:/mapserver /physicalPath:"C:\Inetpub\wwwroot\mapserver"
    
  6. 允许在web.config文件(如上)中设置请求处理程序,而不仅仅是在网站级别。

    %windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/handlers
    
  7. 接下来,使用以下命令确保在IIS中安装了所需的IIS CGI模块(包括FastCGI):

    Dism /Online /Enable-Feature /FeatureName:IIS-CGI
    

    备注

    您还可以通过用户界面启用IIS模块,如下所示:

    1. 点击“开始按钮”

    2. 在搜索框中,输入“打开或关闭Windows功能”

    3. 在“功能”窗口中,单击“Internet信息服务”

    4. 点击:“万维网服务”

    5. 单击:“应用程序开发功能”

    6. 检查(启用)功能。

    7. 然后可能需要重新启动IIS

    ../_images/windows_features.jpg
  8. 接下来,我们将mapserver.exe设置为fastcgi应用程序,并允许它在IIS中运行:

    "%systemroot%\system32\inetsrv\appcmd" set config -section:system.webServer/fastCgi /+"[fullPath='C:\Mapserver\bin\mapserv.exe']" /commit:apphost
    "%systemroot%\system32\inetsrv\appcmd" set config /section:isapiCgiRestriction /+"[path='C:\MapServer\bin\mapserv.exe',description='MapServer',allowed='True']"
    

    还可以使用IIS管理器查看和配置这些设置:

    ../_images/iis-cgi-settings.png
  9. 通过在浏览器中输入以下URL来测试设置是否成功:http://localhost/mapserver/.如果显示以下文本,则表明MapServer已成功配置为IIS中的FastCGI应用程序。

    No query information to decode. QUERY_STRING is set, but empty.
    

地图服务器配置文件

MapServer 8.0版本引入了一个新的 CONFIG 文件。这允许在单个位置设置所有MapServer全局环境变量,这比IIS FastCGI变量更易于管理,并且在不同环境和操作系统之间更易于移植。

对于此示例,我们将在以下位置创建一个空配置文件 C:\MapServer\apps\mapserver.config 。创建文件后,可以复制以下示例配置:

CONFIG
  ENV

    # allow any map from any location - suitable for development only
    MS_MAP_PATTERN "."

    # allow maps from anywhere in C:/MapServer/apps/ or subfolders
    # MS_MAP_PATTERN "^(C:)?\/MapServer\/apps\/((?!\.{2})[_A-Za-z0-9\-\.]+\/{1})*([_A-Za-z0-9\-\.]+\.(map))$"

    # path to the Proj4 projection files
    PROJ_LIB "C:/MapServer/bin/proj7/SHARE"

    # point to the certs file for HTTPS access to OWS servers
    CURL_CA_BUNDLE "C:\MapServer\bin\curl\curl-ca-bundle.crt"

    # to set a default Mapfile uncomment below
    # MS_MAPFILE "C:/MapServer/apps/test/test.map"

    # to block MapServer from accesssing Mapfile by path uncomment below
    # MS_MAP_NO_PATH TRUE
  END

  # allow Mapfiles to be accessed by key
  MAPS
    "test" "C:/MapServer/apps/tests/test.map"
  END

  PLUGINS
    # new keyed approach to plugins
    "mssql" "C:/MapServer/bin/ms/plugins/mssql2008/msplugin_mssql2008.dll"
    "oci" "C:/MapServer/bin/ms/plugins/oci/msplugin_oracle.dll"
  END
END

IIS FastCGI环境变量 MAPSERVER_CONFIG_FILE 但是,仍然需要创建指向此配置文件的文件。这可以使用IIS管理器进行设置,然后选择服务器>FastCGI设置> C:\MapServer\bin\mapserv.exe >环境变量:

../_images/iis-config-file.png

或者,也可以在命令行中使用以下命令进行设置:

%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='C:\MapServer\bin\mapserv.exe'].environmentVariables.[name='MAPSERVER_CONFIG_FILE',value='C:\MapServer\apps\mapserver.conf']" /commit:apphost

设置应用程序

MapServer应用程序包括一个映射文件,以及用于数据、符号、日志和图像的其他可选文件。同一个MapServer实例可以用于许多应用程序。在这个例子中,我们将创建一个新的 test 中的测试应用程序的子文件夹 C:\MapServer\apps 文件夹。

md C:\MapServer\apps\test

现在创建一个新的 test.map 文件,并粘贴到下面的测试映射文件中。

copy NUL C:\MapServer\apps\test\test.map
notepad C:\MapServer\apps\test\test.map
MAP
    EXTENT -180 -90 180 90
    # Set the path to where the map projections are stored
    CONFIG "PROJ_LIB" "C:\MapServer\bin\proj\SHARE"
    NAME "TestMap"
    WEB
      METADATA
        "ows_enable_request" "*"
      END
    END
    PROJECTION
      "init=epsg:4326"
    END
    LAYER
      NAME "country_bounds"
      TYPE RASTER
      CONNECTION "http://demo.mapserver.org/cgi-bin/wms?"
      CONNECTIONTYPE WMS
      METADATA
        "wms_srs"             "epsg:4326"
        "wms_name"            "country_bounds"
        "wms_server_version"  "1.1.1"
        "wms_format"          "image/png"
      END
    END
END

现在,您应该能够测试通过使用以下URL通过IIS访问应用程序以通过WMS检索图像:http://localhost/mapserver/mapserv?map=c:/mapserver/apps/test/test.map&service=wms&version=1.3.0&request=getmap&layers=countrybounds&styles=&crs=epsg:4326&bbox=-180,-90180,90&width=400&height=300&format=image/png

../_images/iis-working.png

设置日志记录

您可以通过将以下行添加到 MAP 挡路。

DEBUG 5 # a value from 0 (OFF) to 5 (detailed)
CONFIG "MS_ERRORFILE" "logs\ms.log"

日志文件的路径是相对于映射文件的,因此在本例中,您需要创建一个新的 C:\MapServer\apps\test\logs 文件夹:

md C:\MapServer\apps\test\logs

您可能需要将日志文件的读写权限设置为IIS应用程序池用户(在本例中,我们使用的是默认权限 IIS AppPoolDefaultAppPool 运行MapServer FastCGI的帐户)。在IIS10中,“经过身份验证的用户”组通常对C盘上的所有文件夹具有修改访问权限,因此可能没有必要执行此步骤。

icacls "C:\MapServer\apps\test\logs" /grant "IIS AppPool\DefaultAppPool":(OI)(CI)RW

如果使用了其他应用程序池,或者未设置权限,则将收到以下消息(同时检查 C:\MapServer\apps\test\logs 文件夹存在,因为它不会自动创建)。

msSetErrorFile(): General error message. Failed to open MS_ERRORFILE C:/MapServer/apps/test/logs/ms.log

进一步配置

备注

自MapServer 8.0起 环境变量 可以在 CONFIG 文件,而不是作为IIS中的变量

MapServer利用 环境变量 要创建应用程序范围的设置,请执行以下操作。使用IIS时,需要将这些设置为FastCGI环境设置,而不是标准的系统范围环境变量。每个FastCGI应用程序都可以配置不同的环境变量,并且可以通过IIS管理器或使用命令行进行修改。

../_images/iis-environ-settings.png

FastCGI配置存储在 C:\Windows\System32\inetsrv\config\applicationHost.config 格式如下:

<environmentVariables>
   <environmentVariable name="CURL_CA_BUNDLE" value="C:\MapServer\bin\curl\curl-ca-bundle.crt" />
</environmentVariables>

设置默认 Mapfile

这个 MS_MAPFILE 可以将环境变量设置为默认映射,如下所示:

%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='C:\Mapserver\bin\mapserv.exe'].environmentVariables.[name='MS_MAPFILE',value='C:\MapServer\apps\test\test.map']" /commit:apphost

您可以发出请求,而不需要查询字符串 map=C:/MapServer/apps/test/test.maphttp://localhost/mapserver/mapserv?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&LAYERS=country_bounds&STYLES=&CRS=EPSG:4326&BBOX=-180,-90,180,90&WIDTH=400&HEIGHT=300&FORMAT=image/png

除了此方法外, URL Rewrite module 可用于根据查询字符串参数或域名设置不同的映射文件。

设置证书文件

要通过HTTPS访问远程数据源(例如,在级联WMS时),您可能需要指向证书文件。这可以使用 CURL_CA_BUNDLE 环境变量:

%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='C:\Mapserver\bin\mapserv.exe'].environmentVariables.[name='CURL_CA_BUNDLE',value='C:\MapServer\bin\curl\curl-ca-bundle.crt']" /commit:apphost

看见 通过HTTPS访问OGC服务 了解更多详细信息。

保护IIS中的地图服务器

MapServer有两个环境变量,可用于限制可以访问哪些 Mapfile 。

备注

从MapServer8.0开始,这些变量可以在 CONFIG 文件,而不是作为IIS中的变量

MS_MAP_NO_PATH 可用于确保用户不能提供他们自己的 map= 参数添加到查询字符串。它可以设置为任何值(在本例中为“True”),因此只有 MS_MAPFILE 可以使用环境值(请参阅上面的“设置默认映射文件”)。

%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='C:\Mapserver\bin\mapserv.exe'].environmentVariables.[name='MS_MAP_NO_PATH',value='True']" /commit:apphost

MS_MAP_PATTERN can be set to limit the folders from which Mapfiles can be accessed using a regular expression. For example to limit to the C:\MapServer\apps (note this path is case-sensitive) folder use the following regular expression: "^(C:)?\/MapServer\/apps\/((?!\.{2})[_A-Za-z0-9\-\.]+\/{1})*([_A-Za-z0-9\-\.]+\.(map))$"

%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi /+"[fullPath='C:\Mapserver\bin\mapserv.exe'].environmentVariables.[name='MS_MAP_PATTERN',value='"^(C:)?\/MapServer\/apps\/((?!\.{2})[_A-Za-z0-9\-\.]+\/{1})*([_A-Za-z0-9\-\.]+\.(map))$"']" /commit:apphost

除了这些方法之外,IIS本身还可用于阻止或筛选查询字符串参数。这个 denyQueryStringSequences 元素可以设置为阻止由URL设置的映射文件。两者都有 map= 和编码后的 map%3d 都被屏蔽了。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <security>
            <requestFiltering>
                <denyQueryStringSequences>
                     <add sequence="map=" /> <!-- will return HTTP Error 404.18 - Not Found -->
                 </denyQueryStringSequences>
            </requestFiltering>
        </security>
        <handlers>
            <add name="MapServerFastCgi" path="*" verb="*" type="" modules="FastCgiModule" scriptProcessor="C:\Mapserver\bin\mapserv.exe" resourceType="Unspecified" requireAccess="Script" allowPathInfo="false" preCondition="" responseBufferLimit="4194304" />
        </handlers>
        <httpErrors errorMode="Detailed" />
    </system.webServer>
</configuration>

故障排除

下面列出了常见错误消息及其解决方法。

  • 如果 MAPSERVER_CONFIG_FILE 环境变量尚未在IIS中设置,或者它指向丢失或无法访问的文件,您将收到以下消息:

    msLoadConfig(): Unable to access file. See mapserver.org/config_file.html for more information.
    
  • 如果 MAPSERVER_CONFIG_FILE 是空的或不是以 CONFIG 您将收到以下消息:

    msLoadConfig(): Unknown identifier. First token must be CONFIG, this doesn't look like a mapserver config file.
    
  • 如果 MAPSERVER_CONFIG_FILE 具有无效语法,您将收到类似以下内容的消息:

    msLoadConfig(): Unknown identifier. Parsing error near (ENVS):(line 2)
    
  • 如果满足以下条件,则(从MapServer 8.0)返回以下错误 allowPathInfo="false" 设置在 handlers 中有关MapServer的部分 web.config 文件。应将其设置为 true

    msLoadMap(): Regular expression error. MS_DEFAULT_MAPFILE_PATTERN validation failed.
    
  • 如果 CONFIG 用于限制对 Mapfile 的访问的文件安全正则表达式与提供的 Mapfile 不匹配,您将收到以下信息:

    msCGILoadMap(): Web application error. CGI variable &quot;map&quot; fails to validate.
    
  • 如果收到以下错误,请检查文件是否存在。如果它确实存在,请检查文件权限。

    msLoadMap(): Unable to access file. (C:/MapServer/apps/test/test.map)
    
  • 地图服务器投影存储在 C:\MapServer\bin\proj\SHARE (或 C:\MapServer\bin\proj6\SHARE 当使用项目6时)。当以以下形式指定投影时 "init=epsg:3857" ,MapServer将尝试在 epsg 文件,其中它被列为 <3857> +proj=merc +a=6378137... 。如果您收到下面的错误,请确保 CONFIG "PROJ_LIB" "C:\MapServer\bin\proj\SHARE" 设置指向正确的路径。 注意:在一次安装中,此消息显示为使用正斜杠而不是反斜杠。

    msProcessProjection(): Projection library error. proj error "No such file or directory" for "init=epsg:3857"
    
  • 如果IIS已被锁定,因此无法在 web.config 水平。

    HTTP Error 500.19 - Internal Server Error
    The requested page cannot be accessed because the related configuration data for the page is invalid.
    
    This configuration section cannot be used at this path. This happens when the section is locked at a parent level.
    Locking is either by default (overrideModeDefault="Deny"), or set explicitly by a location tag with
    overrideMode="Deny" or the legacy allowOverride="false".
    

    在命令窗口中运行以下命令(具有管理员权限)。这使得/mapserver/web.config文件中的handlers部分可以覆盖在计算机级别设置的部分。请参阅http://stackoverflow.com/questions/9794985/iis-this-configuration-section-cannot-be-used-at-this-path-configuration-lock

    %windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/handlers
    
  • 检查 C:\MapServer\bin\mapserv.exe 如果遇到下面的错误,则存在。

    HTTP Error 500.0 - Internal Server Error
    The FastCGI executable could not be found
    
  • 要查看向IIS注册的CGI应用程序,可以使用下面的命令(这将读取 C:\Windows\System32\inetsrv\config ):

    "%systemroot%\system32\inetsrv\appcmd" list config /section:isapiCgiRestriction
    

    对于本节,这将输出类似以下的XML:

    <system.webServer>
      <security>
        <isapiCgiRestriction>
          <add path="C:\MapServer\bin\mapserv.exe" allowed="true" description="MapServer" />
        </isapiCgiRestriction>
      </security>
    </system.webServer>