← ServerPilot Docs

How to Create a 'cgi-bin' Directory

A cgi-bin folder can be used to enable support for legacy CGI scripts. When you enable a cgi-bin folder, any executable program in that directory can be requested. Any program that outputs a valid HTTP response can be a CGI script.

Careful! We can't provide support for customizations or for any errors, downtime, or vulnerabilities you introduce through customizations.

Unless you're absolutely sure about what you're doing, you should stop now!

Step One: Customize Your App's Apache Vhost Configuration

To enable CGI scripts for an app, SSH in to your server as root and create this file (replacing APPNAME with the name of your app):

/etc/apache-sp/vhosts.d/APPNAME.d/cgi-bin.conf

with the contents:

Define CGI_BIN ${DOCUMENT_ROOT}/../cgi-bin/
ScriptAlias /cgi-bin/ ${CGI_BIN}
<Directory ${CGI_BIN}>
    Require all granted
</Directory>

Once you've created that file, restart Apache by running this command as root:

sudo service apache-sp restart

Step Two: Create and Test the cgi-bin Directory

Next, log out of your server as root and SSH back in as your app's system user.

Once logged in, create the cgi-bin directory with these commands:

mkdir ~/apps/APPNAME/cgi-bin
chmod 755 ~/apps/APPNAME/cgi-bin

You can now test the cgi-bin directory by creating a simple CGI script. Create this file:

apps/APPNAME/cgi-bin/test.sh

with the contents:

#!/bin/bash
echo "Content-type: text/html"
echo
echo "Hello, World!"

and make the script executable by running this command:

chmod 755 ~/apps/APPNAME/cgi-bin/test.sh

You should now be able to request this CGI script in your browser using the URL:

http://YOUR_DOMAIN/cgi-bin/test.sh

Debugging "Internal Server Error" for cgi-bin scripts

If you encounter "Internal Server Error" when making HTTP requests for your CGI scripts, these are the log files you should check for more information about the error:

/var/log/apache-sp/suexec.log

/var/log/apache-sp/error.log

/srv/users/SYSUSER/log/APPNAME/APPNAME_apache.error.log

A common error is to forgot to set the correct permissions on your scripts in the cgi-bin directory. See the above instructions for setting the correct permissions on your scripts in the cgi-bin directory.

Last updated: September 21, 2023

Launch your first site in 5 minutes