Getting Started with CGI

Getting started with CGI: how the Common Gateway Interface works

What is CGI and how does a web server run a CGI script?

CGI (Common Gateway Interface) is a standard protocol for a web server to execute an external program and return the program's output as an HTTP response. When a request matches a CGI-enabled path, the server forks a new process, sets a collection of environment variables describing the request, runs the program, and sends whatever the program prints to stdout back to the client as the HTTP response. Each request is a fresh process invocation; no state persists in memory between requests.

Request CGI help Getting started

What the web server does when a CGI request arrives

The server identifies the request as CGI when the URL matches a cgi-bin directory or a .cgi/.pl extension (depending on configuration). It then sets a collection of environment variables: REQUEST_METHOD (GET or POST), QUERY_STRING (the part after ? for GET requests), CONTENT_LENGTH and CONTENT_TYPE (for POST), HTTP_* variables for each request header, PATH_INFO, SERVER_NAME, REMOTE_ADDR, and others. The request body for POST requests is available on the program's standard input. The program reads these, does its work, and prints the response to standard output, starting with HTTP headers.

The main performance characteristic of CGI is that every request forks a new process and loads the interpreter from scratch. A Perl or Python interpreter startup takes tens to hundreds of milliseconds, which is fine for low-traffic uses but adds up under load. FastCGI and WSGI address this by keeping the interpreter process alive between requests; the protocol differences are covered in the modern alternatives guide.

The CGI environment variables

The CGI standard defines a set of environment variables the server must set before invoking the script. The most commonly used ones are: REQUEST_METHOD (GET, POST, HEAD), QUERY_STRING (URL-decoded query parameters for GET), CONTENT_LENGTH (byte count of the POST body), CONTENT_TYPE (MIME type of the POST body, e.g. application/x-www-form-urlencoded), HTTP_HOST (the Host header), HTTP_REFERER (the Referer header), REMOTE_ADDR (client IP), SERVER_NAME (the hostname of the server), SCRIPT_NAME (the CGI script's URL path), and PATH_INFO (additional path information after the script name).

All HTTP request headers arrive as HTTP_* variables with hyphens replaced by underscores and names uppercased: the Accept-Language header becomes HTTP_ACCEPT_LANGUAGE. Under Perl's taint mode, all of these variables are tainted and must be explicitly validated before use. Under any language, treat all of them as untrusted user-controlled input.

Where CGI still fits in 2026

CGI is no longer the dominant model for web application development; frameworks, containers, and serverless functions handle most of what CGI once did. But CGI is still present and still appropriate in specific contexts. Shared hosting environments that do not offer PHP or Node.js often support CGI as the only way to run dynamic code. Legacy systems built around CGI scripts may not justify a full migration. Simple administrative scripts and internal tools benefit from the simplicity of CGI without needing a full application server.

Understanding CGI is also useful for debugging and for understanding how the web worked before application servers, since many concepts (environment-based request passing, stdout as response, process isolation per request) reappear in different forms in modern serverless and container architectures. For new projects, evaluate whether a modern alternative fits before defaulting to CGI, but do not mistake familiarity with better for actual technical superiority in every context.

What to know

Key points

Get help

Listings and a local agent, when you are ready

Need a CGI script built, debugged, or a hosting environment configured? Forms use a clearly-marked placeholder endpoint until wired to a real system.

Hosting partner CGI-compatible hosting for CGI hosting

Reserved for an affiliate link to a vetted shared or VPS hosting provider that supports CGI and Perl/Python execution. Operator wires affiliate link here.

Development Need a custom CGI script built?

Self-hosted lead form for custom CGI development inquiries. Placeholder endpoint until wired to the operator's CRM or email handler.

Open inquiry form →

Custom CGI development

No spam. Your inquiry is answered within 1 business day.

Hosting setup help

No spam. Your inquiry is answered within 1 business day.

Questions

Frequently asked questions

What does CGI stand for?
CGI stands for Common Gateway Interface. It is a standard protocol that defines how a web server passes request information to an external program and how the program returns its response. The 'gateway' in the name refers to the server acting as a gateway between the HTTP request and the external program.
Is CGI still used in 2026?
CGI is still supported on most traditional shared hosting environments and still runs on a large number of production websites, particularly legacy sites and sites on older shared hosts. For new projects, application frameworks and platforms have largely replaced raw CGI, but CGI knowledge is useful for maintaining existing scripts, understanding HTTP fundamentals, and working on shared hosting environments where CGI is the only dynamic execution option.
What is the difference between CGI and FastCGI?
Standard CGI forks a new process for every HTTP request, which includes the overhead of starting the interpreter each time. FastCGI keeps the interpreter process alive between requests; the web server sends each request through a socket to the persistent process, which handles it and stays running for the next request. FastCGI dramatically reduces per-request overhead for interpreted languages and is supported by most web servers that also support CGI.

CustomCGI publishes reference information on CGI programming and server-side web scripting for educational and informational purposes. Code examples are provided as-is for learning; always audit and test scripts in a staging environment before deploying to production. Security vulnerabilities in CGI scripts are a real risk; follow current best practices and review the security guide before deploying any user-facing code.