VPS and Dedicated CGI Setup

CGI on a VPS: enabling and securing CGI in Apache and Nginx

How do I enable CGI on an Apache or Nginx VPS?

On Apache, enable CGI with mod_cgi (or mod_cgid for multi-threaded MPMs), add a ScriptAlias directive mapping a URL path to a filesystem directory, and set Options +ExecCGI in a Directory block for that path. On Nginx, use fastcgi_pass to a running CGI-to-FastCGI wrapper like fcgiwrap, since Nginx does not natively execute CGI scripts; it forwards requests to a FastCGI process. In both cases, ensure the CGI user has minimal filesystem permissions and consider suEXEC (Apache) to run scripts as the file owner.

Request CGI help Getting started

Apache CGI configuration

To enable CGI in Apache, ensure mod_cgi or mod_cgid is loaded (a2enmod cgi on Debian-based systems). Add a ScriptAlias directive in your virtual host: ScriptAlias /cgi-bin/ /var/www/cgi-bin/. This tells Apache that requests to /cgi-bin/ should be executed as CGI scripts from /var/www/cgi-bin/. In the Directory block for that path, set Options +ExecCGI and AddHandler cgi-script .cgi .pl .py.

suEXEC is an Apache module that runs CGI scripts as the file owner rather than as the Apache process user (typically www-data). This provides process isolation between virtual hosts: a script owned by one user cannot affect files owned by another. Configure it by setting the User and Group directives in the virtual host and ensuring suEXEC is compiled in. The trade-off is increased configuration complexity and stricter file ownership requirements.

Nginx and CGI

Nginx does not execute CGI scripts natively. The standard approach is to install fcgiwrap, a minimal CGI-to-FastCGI gateway that accepts FastCGI connections and executes CGI scripts for each request. Install fcgiwrap (apt install fcgiwrap on Debian/Ubuntu), configure it to listen on a Unix socket or TCP port, and add a location block in your Nginx configuration that uses fastcgi_pass to send matching requests to fcgiwrap with the standard FastCGI parameters.

fcgiwrap re-forks a new process for each CGI request, giving you the same isolation model as standard CGI while fitting Nginx's FastCGI upstream model. It is not as fast as a persistent FastCGI application, but it is simpler than converting existing CGI scripts to a persistent server model and gives you a working CGI environment on Nginx with minimal changes to existing scripts.

Security hardening on a self-managed server

On a VPS you control the full server stack, which means you own the security configuration. Restrict the CGI directories to a dedicated user and group with minimal permissions. Disable the ability to execute scripts from upload directories explicitly. Keep the Perl and Python interpreters updated through your distribution's package manager. Consider running CGI scripts in a chroot or container if the threat model justifies the added complexity.

Use a web application firewall or mod_security on Apache to add a layer of input filtering in front of your scripts. This does not replace proper validation inside the scripts, but it can catch common attack patterns before the script ever runs. Log all CGI requests with access and error logging enabled, and set up log monitoring to alert on unusual patterns like repeated 500 errors or large numbers of requests to a single CGI path.

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 VPS 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

How do I run Perl CGI scripts on Nginx?
Nginx does not execute CGI scripts natively. Install fcgiwrap (a CGI-to-FastCGI gateway), configure it to listen on a Unix socket, and add a location block in your Nginx server configuration that sends matching requests to fcgiwrap via fastcgi_pass. fcgiwrap executes the CGI script for each request and returns the output through the FastCGI protocol to Nginx, which sends it to the client.

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.