We write this tutorial basically because there are one of customers experience issue running CGI program on IIS server. So, this is only simple and quick tutorial how to run your CGI program on your IIS server and we hope that you can find this post useful.
Steps to Run CGI Program
1. Write a quick CGI
Take the following code and save it as simplecgi.cs in the directory c:\inetpub\wwwroot\cgi
using System; using System.Collections; class SimpleCGI { static void Main(string[] args) { Console.WriteLine("\r\n\r\n"); Console.WriteLine("<h1>Environment Variables</h1>"); foreach (DictionaryEntry var in Environment.GetEnvironmentVariables()) Console.WriteLine("<hr><b>{0}</b>: {1}", var.Key, var.Value); } }
2. Change into the C:\inetpub\wwwroot\cgi directory and compile the source by using the following command-line:
%windir%\Microsoft.NET\Framework\v2.0.50727\csc.exe SimpleCGI.cs
Simplecgi.exe will be located in the cgi directory. To view its output, run it using the command-line.
3. Every CGI must be registered in the ISAPI/CGI Restriction list for security reasons. You must first open INETMGR, select the machine node (your machine’s name), and then locate the ISAPI/CGI Restriction List menu icon.
Choose the item, then enter the following information in the dialog box:
You can also use this command as an alternative:
%windir%\system32\inetsrv\appcmd set config -section:isapiCgiRestriction
/+[path='c:\inetpub\wwwroot\cgi\simplecgi.exe',allowed='true',description='SimpleCGI']
4. The next step is to build a virtual directory where CGIs can run. Select “Add Virtual Directory” by performing a right-click on “Default Web Site” in INETMGR. Make the following additions:
Here is the command-line which does the same:
appcmd add vdir /app.name:"Default Web Site/" /path:/cgi /physicalPath:"c:\inetpub\wwwroot\cgi"
5. One more thing: “Execute” access is still not permitted in this directory. To do this, navigate to the Handler Mappings menu in the CGI virtual directory (be sure to choose it from the left-hand menu!).
Go to the “Edit Feature Permissions” link in the Actions Menu on the right hand side, open it and check “Execute” and click “OK”.
via command-line:
appcmd set config "Default Web Site/cgi" /section:handlers -accessPolicy:"Read,Script,Execute"
You are now prepared to move forward. You should get the following result when you type “http://localhost/cgi/simplecgi.exe”:
Final Conclusion
Is it quite easy right to run your CGI program on IIS server? If you find this tutorial helpful, please feel free to share it. If you have any questions, please comment below.
Are you looking for an affordable and reliable Asp.net core hosting solution?
Get your ASP.NET hosting as low as $1.00/month with ASPHostPortal. Our fully featured hosting already includes
- Easy setup
- 24/7/365 technical support
- Top level speed and security
- Super cache server performance to increase your website speed
- Top 9 data centers across the world that you can choose.
Andriy Kravets is writer and experience .NET developer and like .NET for regular development. He likes to build cross-platform libraries/software with .NET.