1and1 Help Centre Categories

print article

What are Custom CGIs?

CGI (Common Gateway Interface) allows another executable on the server to handle the processing of a script. When you access a CGI, the webserver that normally handles the processing of scripts actually passes the script to another program to execute the code instead. Your CGI scripts do not need to be saved in a common folder like cgi-bin/ and can be saved and executed from any folder in your webspace.

CGI scripts can be created for and handled by Perl, Python, PHP, and Ruby. All of these scripts are able to be named with the .cgi extension or their own native extension (such as .rb for a Ruby script). Regardless of that, each type of script needs to include a shebang on the top line of the file referencing the correct executable for the script.

To find the correct executable for your script, you can also use the follwing command via SSH:

whereis

You may want to create a Python script and need to know the path to the Python executable (or binary). You can simply type this command and press Enter:

whereis python

The command line will output the various locations used for Python to the screen. The executable is also known as a binary, so you will be searching for a path that includes a bin/ folder such as /usr/bin/python or /usr/bin/python2.5. The whereis command is also helpful for determining if there is more than one version installed, as is the case with Python.

If your CGI script will be referenced via a browser, you will usually need to include the HTTP header before you are able to output any text to the page. If you do not include the HTTP header with a new line or two afterwards before your text, you might receive a 500 Error.

Perl

#!/usr/bin_uk/perl

print "Content-Type: text/html\n\n";
print "Hello World!";

Python

#!/usr/bin_uk/python

print "Content-Type: text/html\n\n";
print "Hello World!";

PHP

#!/usr/bin_uk/php5

<?php echo "Hello World!"; ?>

Ruby

#!/usr/bin_uk/ruby

puts "Content-Type: text/html\n\n"
puts "Hello World!"

You must also ensure that Read, Write and Execute permissions are set for the user, and that Read and Execute permissions are set for the group and other. You can use the following command via SSH to set these permissions:

chmod 755 myScript.cgi

Replace "myScript.cgi" with the name of your own file. You can usually set these permissions using an FTP program as well if you are not familiar with SSH commands.

For additional information, you may want to reference: