WWW FAQs: How do I run CGI programs on IIS?


2006-06-26: Most web developers who choose Microsoft's Internet Information Server do their dynamic web design in ASP, ASP.NET or C#.NET, Microsoft's homegrown technologies for creating web applications. But you can run CGI programs on IIS. You simply have to configure IIS correctly.

But first, what kind of CGI script or program do you want to run? It makes a difference! If your CGI programs are written in Perl, you need to install the ActivePerl software - the Perl "interpreter" that runs Perl programs. Installing ActivePerl will also take care of telling IIS what to do with .pl files. If your programs are written in Python, you'll have to install the Python interpreter and then do a little bit more work to tell IIS what to do with .py files.

If your CGI programs are compiled .exe files, written in C or C++ or any other language that produces .exe files, then you'll just need to configure IIS to allow executables to be run... and make very sure you don't leave any non-CGI .exe files in your web space!

If you're distributing downloadable software on your website, you don't want your server to consider .exe files in those folders to be CGI programs - otherwise your server will run the downloadable programs instead of sending them to the user. Ouch! The simplest workaround is to distribute software inside a .zip file. You can also set up a separate folder outside your home directory with different rules that don't run CGI programs.
"What about .cgi? I see .cgi everywhere in Unix CGI scripts." Yes, you could use .cgi as a file extension on Microsoft Windows. But then you would have no way of distinguishing between programs written in different scripting languages. Unlike Unix/Linux, Windows doesn't look at the first line of a script to figure out what language it's written in. Windows is not Unix - Unix CGI programs usually won't run without some changes in Windows. If you really want Unix, consider setting up a Linux server.
Are you trying to run someone else's Unix CGI program on Windows? Usually, that's a bad idea. Getting a script to start running is only half the battle if the code assumes lots of Unix programs are out there to be relied on. Usually, they won't be, and you'll have to edit more and more code. If you're using IIS, then use it wholeheartedly - locate or write an ASP or ASP.NET script that does the same job in a Windows-friendly way.

Adding Support For Python, Perl, and .EXE CGI Programs

Sure this is what you want? OK, enough talk - let's do it!

Before you do this, make sure you have no .py, .pl or .exe files anywhere in your web space that you wouldn't want your server to actually run. Always practice safe CGI!
Working at the server, do the following:

1. To support Python scripts, install the Python interpreter. Fetch and run the latest Windows Python installer from the official Python download page.

2. To support Perl scripts, install the Perl interpreter. Fetch and run the latest ActivePerl (Perl for Windows) installer from the official ActivePerl download page.

3. All done with both installs? Great! Configuration for Perl CGI programs (with a .pl extension) should be completely finished at this point. The rest of tutorial assumes you also want to support compiled .exe CGI programs and interpreted Python .py CGI scripts. To enable these, Locate the IIS control panel:

Start -> Control Panel -> Administrative Tools -> Internet Information Services

These steps are correct for the Windows XP "Classic View." If you are using the standard view, the steps are slightly different:

Start -> Control Panel -> Performance and Maintenance -> Administrative Tools -> Internet Information Services

We recommend the "Classic View" for those who are running websites on their PCs.

4. Double-click your computer's name in the left-hand area, then double-click "Websites," then right-click your website's name or "Default Website."

5. A menu appears. Select "Properties."

6. Select the "Home Directory" tab.

7. From the "Execute Permissions:" menu, select "Scripts and Executables" to allow compiled .exe CGI programs (if desired) as well as Python and Perl CGI scripts.

8. If you want to run Python-based CGI programs, see Microsoft Knowledge Base article #276494, Using Python Scripts with IIS.

You should now be ready to roll with Perl CGI scripts, Python CGI scripts, and compiled executable CGI programs under Internet Information Server. Just remember that scripts "imported" from the Unix/Linux world will usually need some modifications, especially if they assume that Unix programs like sendmail are installed. In many cases, it's better to start over and do it the Windows way... or set up a Linux server!

Legal Note: yes, you may use sample HTML, Javascript, PHP and other code presented above in your own projects. You may not reproduce large portions of the text of the article without our express permission.

Got a LiveJournal account? Keep up with the latest articles in this FAQ by adding our syndicated feed to your friends list!