Common Gateway Interface (CGI) is a protocol or set of standards that executes scripts via web requests and explains how information exchange happens between the web server and a custom script.
CGI stands for:
Common - C: controls the interaction between multiple Operating systems (OS).
Gateway - G: through the gateway, we can access various programs, such as databases and image generators.
Interface - I: uses a well-defined method to interact with a Web server.
In Perl, creating a CGI application requires professional coding skills. But once we develop an application, we can use it almost anywhere, such as:
CGI is a standard application interface as it displays how a web server sends information to an external program.
That external program sends the processed data back to the webserver. The information is then delivered back to the browser.
With the help of HyperText Transfer Protocol (HTTP) running on a client machine, we can exchange information with the webserver.
As CGI programs and web servers usually run on the same system as the webserver, the webserver provides the document from the document directory or executes a CGI program depending on the request from the browser.
Consider the following illustration that demonstrates the architectural diagram of CGI:
With the help of CGI programming, we can send data such as texts, images, audio or voice notes, files, documents, etc.
CGI programming isn’t complicated, but a user must know how to debug, execute and structure a CGI code file.
As we have learned what CGI is and how we use it. Let’s bring our learning into reality by executing the code below.
Write these commands in the terminal below to install the required modules for CGI in Perl.
apt-get update
: downloads package information from all configured sources.
apt-get install perl
. Then press (shift + y) and wait for the packages to get installed. This command installs the latest version of Perl.
apt-get install apache2
. Then press (shift + y) and wait for the packages to get installed.
apt-get install sudo
: installs commands related to sudo. It allows you to run programs with the security privileges of another user.
sudo apt-get install apache2 libapache2-mod-wsgi python-dev
. Then press (shift + y).
a2enmod cgid
: a script that enables the specified module within the apache2
configuration.
service apache2 restart
: Apache the most commonly used Web server on Linux systems.
cd /usr/lib/cgi-bin
: locates and opens the directory named /usr/lib/cgi-bin
.
Now, create a Perl CGI by typing this: vi /usr/lib/cgi-bin/test.pl
.
In the file, write the following code for a test run:
#!/usr/bin/perl
print "Content-Type: text/html\n\n";
print "<html>";
print "<head>";
print "<title>CGI Perl</title>";
print "</head>";
print "<body>";
print "<h1>This is Perl CGI Test file.</h1>";
print "<p>Common Gateway Interface (CGI) is a protocol or set of standards that executes scripts via web requests and explains how information exchange happens between the web server and a custom script.</p>";
print "<h2>Hello Word! This is my first CGI program</h2>";
print "</body>";
print "</html>";
After typing the above code, press (ctrl + c) two times and write :wq
, and hit enter
to exit the code file.
chmod 755 /usr/lib/cgi-bin/test.pl
: we use chmod
command to change the read and write operations of a file.
755 means full permissions for the owner and read and execute permission for others.
Now, open a new browser tab and write your IP address plus the path of Perl file which is /cgi-bin/test.pl.
.
In this example, the IP address of the webserver is 172.17.0.2
.
The terminal commands from 1 to 6 install the necessary frameworks, packages, and libraries needed to execute the code file written in point 9 properly.
CGI module runs using an HTML form as written above. This form will get displayed on your local machine when you type in the IP address along with the path of the CGI file.
For example; the above example was executed on a machine having the IP address 172.17.0.2
so, the web address becomes:
172.17.0.2/cgi-bin/test.CGI.
Free Resources