What is CGI Script?
CGI stands for Common Gateway Interface. CGI runs on a web server to enhance the site. We can simply define CGI script is any program that runs on a web server and it a standard way how the information will be passed to and from the browser and server. It is the most common way for web servers to interact with users. CGI is not a programming language or a telecommunication protocol. It is an interface between the web application and the web server. A CGI program can be written in any programming language.
In simple words, a script is a program which executes and its output is sent to the browser. While using CGI, the server will understand that any file requested from the special cgi-bin directory should be executed instead of simply be read and sent the output. The output of the executed program will be sent to the browser.
Any file that includes the cgi-script handler will be treated as a CGI script. Website files can acquire this handler in two ways; either via AddHandler directive or by being in a ScriptAlias directory. You need to configure Apache with CGI execution permissions, in order to your CGI programs to work properly by adding the following code on your httpd.conf file.
LoadModule cgid_module modules/mod_cgid.so
The other ways to configuring Apache to permit CGI are discussed below.
If you are using Script Alias Directive, which tells Apache that the particular directory is set for CGI programs. When you are requesting a particular resource in that directory, Apache will execute the files and then send the output. The format of the ScriptAlias directive is like.
ScriptAlias “/cgi-bin/” “/usr/local/apache/cgi-bin/”
The example tells Apache that any request for a resource beginning with /cgi-bin/ should be served from the directory /usr/local/apache/cgi-bin/, and should be treated as a CGI program.
For example, if you are browsing the URL https://www.example.com/cgi-bin/simple.pl, the server will identify the simple.pl is in the cgi-bin directory, there for first the server executes the file simple.pl and the output will sent to your browser.
For security reasons, CGI programs are often restricted to ScriptAliased Directive. If you wish each user have their own CGI programs but don’t have access to the main cgi-bin directory. To allowing CGI execution to an arbitrary directory first you need to activate the cgi-script handler using the AddHandler or SetHandler directive. Second, ExecCGI must be specified in the Options directive.
This will tell Apache to give permissions to execute CGI files for the respective directory. And after that, you will need to tell the server which files are CGI files by using the AddHandler directive.
AddHandler cgi-script .cgi .pl
This will tell the server to treat all files with extension .cgi or .pl as CGI programs. Please make sure that the .pl and .cgi file have execution permission. If not run the below command to set the permission.
# chmod 755 file name
You can also activate CGI programs through the .htaccess file if you have no access to httpd.conf by adding the below code.
AddHandler cgi-script .cgi
If you wish to create a specific directory under user directory where everything will be treated as a CGI program, add the below code in your .htaccess file.
Note: Replace “/home/*/public_html/cgi-bin” with the path of your CGI directory.
You can write your own CGI scripts if you are familiar with any of the programming languages like C, C++ or Perl etc. The main aim of CGI scripting is to create dynamic content each time the script executes; because the output may be different on each execution.
If you need any further help, please do reach our support department.