Steps to Run and Configure the CGI Script

Step 1: ENABLE CGI SUPPORT IN APACHE

Before proceeding with the CGI programming, ensure that there is a web server setup on your system. Here, XAMPP server is used, as it will help in setting up the server Apache 2.0 on the system.

Also, before running or configuring the CGI scripts in Apache, you need to first check for enabling the CGI support in Apache. You can do so by accessing the httpd.conf file from the Config option.

When you open the httpd.conf file, locate to the below mentioned line, and remove the ‘#’ sign from its beginning (if it is not removed).

LoadModule cgi_module modules/mod_cgi.so

Step 2: CREATE A CGI SCRIPT

Once the CGI support is enabled, you can start creating your CGI scripts, using any of the Python, Perl or Shell Script. You can use any of the text editors to create the scripts and save it with .cgi extension.

Here, is a small CGI program on Hello_mycgi.py

#!C:\Python311\python.exe
print("Content-type:text/html\r\n\r\n")
print('<html>')
print('<head>')
print('<title>CGI Hello World</title>')
print('</head>')
print('<body>')
print('<h1>Hello, World!</h1>')
print('<p>This is a simple CGI program.</p>')
print('</body>')
print('</html>')

Note − First line in the script must be the path to Python executable. It appears as a comment in Python program, but it is called shebang line. If you will not put this line in your script then, you may encounter multiple errors and difficulties while running the program.

Step 3: CONFIGURE APACHE TO RECOGNIZE THE CGI SCRIPT

Once you are done with creating the CGI script, next you must configure Apache to recognize the script as the CGI script. For this, you need to do make the specific configurations in the Apache httpd.conf file to make sure that the web server is configured with all types of CGI program handlers. First of all, in the “AddHandler” column, make sure there has to be a .cgi and a .py extension, as shown below

AddHandler cgi-script .cgi .pl .asp .py

This will allow the Apache server to treat .py file as CGI script. Also, check for the “ExecCGI” in the “Options” directive, as shown below

This will allow to use CGI scripts outside of ScriptAliased directories.

All the CGI programs which are to be executed by the HTTP server are kept in a pre-configured directory, called as CGI Directory or C:/xampp/cgi-bin. Although, by convention, CGI files have .cgi as extension, but it can have Python extension .py as well.

By default, the Linux server is set up to only execute the scripts found in the cgi-bin directory under C:/xampp/cgi-bin. The following lines in the httpd.conf file should be commented if you want to choose any alternative directory to run your CGI scripts in. Save the configuration file and restart the Apache web server for the changes to take effect.

Step 4: TEST THE CGI SCRIPT

Now, that you are done with all the configurations, finally its time to test the CGI script. To do so, simply open your web browser and type the address of your script in the URL.

This is the output which you will receive on the STDOUT file, i.e., screen. The line Content-type:text/html\r\n\r\n will be sent back to the browser to specify the content type to be displayed on the browser screen.



Historical context and evolution of CGI.

When you click on any of the links on the webpage, your browser contacts the HTTP web server and demands the URL that is, the filename, and the web server passes the URL and looks for the filename, if it finds that file, it will send it back to the browser otherwise sends an indicating message that you have requested a wrong file. Then, the web browser takes a response from the web server and displays either the received file or the error message. However, it is possible to set the HTTP server so that whenever a file in a certain directory is requested, that file is not sent back. Instead, it is executed as a program. And, whenever that program shows any output, it’s sent back to the browser to display. So, this function is called Common Gateway Interface or CGI, and the program scripts are called CGI Scripts. These CGI scripts can be a Python script, PERL script, Shell script, C or C++ program, etc.

Similar Reads

What is CGI?

CGI or Common Gateway Interface, is the industry-accepted acronym for a set method that is used to define how information is exchanged between the web server and a custom script. It is a protocol that defines how web servers and external programs or scripts can communicate to generate dynamic content on the World Wide Web....

CGI Architeture

...

Historical Context of CGI

In 1993, the standard for invoking command line executables was written by the National Center for Supercomputing Applications (NCSA) team and posted to the www-talk mailing list. It was embraced by the other Web server creators, and ever since then, Web servers have used it as a standard. A work group headed by Ken Coar was established in November 1997 to formalize the NCSA definition of CGI. This effort led to the creation of RFC 3875, which described CGI Version 1.1. Specifically mentioned in the RFC are the following contributors:...

Evolution of CGI

Since it was first introduced in the early 1990s, the Common Gateway Interface (CGI) has seen tremendous development. The evolving demands of web development and technological advancements have influenced its progress. An overview of its evolution is shown below:...

Advantage and Diadvantage of CGI

Advantage of CGI...

Steps to Run and Configure the CGI Script

Step 1: ENABLE CGI SUPPORT IN APACHE...

Contact Us