## CGI How To

### Table of Contents

- Introduction
- Installation
- Configuration

### Introduction

The CGI (Common Gateway Interface) defines a way for a web server to interact with external content-generating programs, which are often referred to as CGI programs or CGI scripts.

Within Tomcat, CGI support can be added when you are using Tomcat as your HTTP server and require CGI support. Typically this is done during development when you don't want to run a web server like Apache httpd. Tomcat's CGI support is largely compatible with Apache httpd's, but there are some limitations (e.g., only one cgi-bin directory).

CGI support is implemented using the servlet class `org.apache.catalina.servlets.CGIServlet`. Traditionally, this servlet is mapped to the URL pattern "/cgi-bin/*".

By default CGI support is disabled in Tomcat.

### Installation

CAUTION - CGI scripts are used to execute programs external to the Tomcat JVM. If you are using the Java SecurityManager this will bypass your security policy configuration in `catalina.policy.`

Remove the XML comments from around the CGI servlet and servlet-mapping configuration in `$CATALINA_BASE/conf/web.xml`.

Only Contexts which are marked as privileged may use the CGI servlet (see the privileged property of the Context element).

### Configuration

There are several servlet init parameters which can be used to configure the behaviour of the CGI servlet.

- cgiPathPrefix - The CGI search path will start at the web application root directory + File.separator + this prefix. The default cgiPathPrefix is `WEB-INF/cgi`
- debug - Debugging detail level for messages logged by this servlet. Default 0.
- executable - The of the executable to be used to run the script. You may explicitly set this parameter to be an empty string if your script is itself executable (e.g. an exe file). Default is `perl`.
- executable-arg-1, executable-arg-2, and so on - additional arguments for the executable. These precede the CGI script name. By default there are no additional arguments.
- parameterEncoding - Name of the parameter encoding to be used with the CGI servlet. Default is `System.getProperty("file.encoding","UTF-8")`.
- passShellEnvironment - Should the shell environment variables (if any) be passed to the CGI script? Default is `false`.
- stderrTimeout - The time (in milliseconds) to wait for the reading of stderr to complete before terminating the CGI process. Default is 2000.

### Comments

Notice: This comments section collects your suggestions on improving documentation for Apache Tomcat.
If you have trouble and need help, read Find Help page and ask your question on the tomcat-users mailing list. Do not ask such questions here. This is not a Q&A section.
The Apache Comments System is explained here. Comments may be removed by our moderators if they are either implemented or considered invalid/off-topic.