Shared Hosting CGI Setup

CGI on shared hosting: cgi-bin, permissions, and getting your first script running

How do I run a CGI script on shared hosting?

On most shared hosts, CGI scripts must be placed in the cgi-bin directory (or a subdirectory of it), set to execute permissions (chmod 755), and begin with a shebang line pointing to the correct interpreter path on that host. The URL to reach the script is typically https://yourdomain.com/cgi-bin/scriptname.cgi. Errors nearly always fall into three categories: wrong interpreter path, wrong permissions, or output printed before the Content-Type header.

Request CGI help Getting started

Finding the cgi-bin directory

Most traditional cPanel-based shared hosts create a cgi-bin directory at the root of your hosting account, accessible via FTP at /home/username/public_html/cgi-bin or similar. The URL path is /cgi-bin/ on your domain. Some hosts configure the entire account home to allow CGI, others restrict it to specific directories. Log into your hosting control panel and look for a CGI or Perl section in the documentation, or ask your host's support.

Some hosts also allow you to create additional CGI-enabled directories using a .htaccess file with Options +ExecCGI and AddHandler cgi-script .cgi .pl. This lets you put CGI scripts in a more logical URL path rather than under /cgi-bin. Whether this is permitted depends on the host's server configuration; not all allow .htaccess overrides of ExecCGI.

File permissions and the shebang

CGI scripts need two things to execute: the correct shebang and execute permission. The shebang must point to the exact path of the interpreter on your host. Common paths are /usr/bin/perl, /usr/local/bin/perl, /usr/bin/python3, /usr/bin/env perl, and /usr/bin/env python3. Run 'which perl' or 'which python3' via SSH to find the right path, or ask your host. A wrong shebang path causes a 500 error that can look identical to a syntax error or permission problem.

Permissions must be exactly 755 (rwxr-xr-x) for CGI scripts on most hosts. 777 (world-writable) is often rejected as a security risk; 644 (no execute bit) means the server will not execute the script. Set permissions after uploading via your FTP client or with chmod 755 scriptname.cgi via SSH.

Testing before you go live

The fastest way to debug a CGI script on shared hosting is via SSH. Run perl -c scriptname.cgi to check for syntax errors without the web server involved. Then set the minimum required environment variables and run the script directly: REQUEST_METHOD=GET QUERY_STRING='' perl scriptname.cgi. You will see exactly what the script would print as an HTTP response, making header and output errors obvious without needing to check web server logs.

If SSH is not available, add use CGI::Carp qw(fatalsToBrowser) (Perl) or import cgitb; cgitb.enable() (Python) at the top of the script during debugging to redirect fatal errors to the browser. Remove these before leaving the script in production, since they expose internal paths and error details to visitors.

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

Where do I put CGI scripts on shared hosting?
On most shared hosts, CGI scripts go in the cgi-bin directory, accessible at /cgi-bin/ in the URL and typically at /home/username/public_html/cgi-bin/ on the server filesystem. Some hosts allow additional CGI-enabled directories via .htaccess, but this requires the host to permit Options +ExecCGI overrides. Check your host's documentation or control panel for the exact location and any restrictions.
My CGI script works from the command line but returns a 500 in the browser. Why?
The most common cause is output before the Content-Type header. When running from the command line, that output goes to your terminal where you do not notice it; in a browser the server sees unexpected output where headers are expected and returns 500. Check for any print statements, warnings, or use statements that produce output before print header() or print 'Content-Type: text/html\n\n'. Also confirm permissions are 755 and the shebang path is correct on the host.

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.