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.
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
- Apache needs mod_cgi and ScriptAlias for CGI. Enable mod_cgi with a2enmod cgi and point ScriptAlias at the scripts directory.
- Nginx needs fcgiwrap to execute CGI scripts. Nginx does not run CGI natively; fcgiwrap acts as a CGI-to-FastCGI bridge.
- Consider suEXEC for multi-user Apache hosts. Running scripts as the file owner prevents cross-user file access in shared virtual host environments.
- Update interpreters through your package manager. On a self-managed server, interpreter updates do not happen automatically; set up unattended-upgrades or a manual schedule.
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.
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.
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
Hosting setup help
Questions