IIS (7/9)

Setting up CGI scripts

In this section we will look how to setup CGI scripts. We will take Python as an example. Python was chosen because this tutorial was original created to learn how to setup Mercurial on IIS. And this also dictated the version of Python we have chosen: 2.7.5. You can get it from here. For the rest of this tutorial we assume Python 2.7.5 has been installed in C:\Python27.

The first step is to enable support for CGI scripting to IIS. Go to to the control panel, then go to the "Programs" section and click on the "Turn Windows features on or off". In the IIS section of the dialog make sure that "CGI" is enabled as shown on figure 1.

Enabling CGI support
Figure 1: Enabling CGI support

This will cause a new icon to be available in the IIS Manager: the "CGI" icon. However this only contains some CGI settings that we won't change. To add support for Python scripts we will actually use the Handler Mappings icon which was already present.

IIS Manager with CGI icon
Figure 2: IIS Manager with CGI icon

Now we will add a handler mapping so that IIS recognizes files with the extension we have chosen for the Python scripts. In this case we will just use the "cgi" extension. We go to the default website in the IIS Manager and click on the "Handler Mappings" icon. Figure 3 shows the handler mappings as they are currently.

Handler Mappings
Figure 3: Handler Mappings

To add our new mapping we select the "Add Script Map..." action in the right-hand column. This launches the dialog shown in figure 4.

Adding a handler for CGI scripts
Figure 4: Adding a handler for CGI scripts

Once we click OK the following popup is displayed.

ISAPI extension authorization popup
Figure 5: "ISAPI extension authorization popup

We answer "Yes" to the question. If we answer no then the mapping is added but the needed authorization would be missing so the requests for *.cgi files would fail because IIS would not allow python.exe to be run.

The handler mappings now look like shown in figure 6.

Handler Mappings
Figure 6: Handler Mappings after successful setup

ISAPI and CGI restrictions

Before showing an example of the new setup in action we have a look at what answering "Yes" to the popup shown in figure 5 did.

If we go to the IIS Manager server screen there is a new icon: "IIS Manager with ISAPI and CGI Restrictions". It was installed when we enabled the CGI feature. Figure 7 shows the IIS Manager with the new icon.

IIS Manager with ISAPI and CGI Restrictions icon
Figure 7: IIS Manager with ISAPI and CGI Restrictions icon

If we click on this icon the dialog shown in figure 8 appears. You can see it has only one entry corresponding to the Python executable with arguments exactly as we specified when creating the handler mapping for Python scripts.

ISAPI and CGI Restrictions screen
Figure 8: ISAPI and CGI Restrictions screen

If we click on this entry we can view and edit it. This shown in figure 8.

Python authorization details
Figure 8: Python authorization details

Example of Python CGI file

Everything is now in place to add a Python file to the default website. We add the following file in C:\inetpub\root.

File: C:\inetpub\wwwroot\works.cgi
print 'Status: 200 OK'
print 'Content-Type: text/html'
print '<html><body>It works! (works.cgi)</body></html>'

And we can fetch it with cUrl as shown in figure 9.

curl localhost/works.cgi
Figure 9: curl localhost/works.cgi

Potential Errors

We just want to finish by showing a potential error screen that would appear if you tried to add a script with "Add Script Map..." but the necessary feature is not installed i.e. if we hadn't gone through the steps of figure 1 at the beginning of this page. Figure 10 shows the error message.

Failure Dialog
Figure 10: Failure Dialog

blog comments powered by Disqus

Copyright(c) 2006-2015 Xavier Leclercq | Privacy policy

Contact Us