Articles

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; we use the serverpilot user in this tutorial.

Once logged in as serverpilot, 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