CGI (Common Gateway Interface)

Hello Friends, Today, I’ll give you a brief idea about CGI.

INTRODUCTION:

The CGI(Common Gateway Interface) acts as an interface between web server and the programs to generate the content named as CGI programs or CGI scripts. Its the easiest way to put dynamic content on your web site. The CGI supports many programming languages like C/C++/Perl/Python/PHP etc. Here, I’m going to explain how to run basic CGI scripts with C++ programming language and scripts using Apache2 web server on Ubuntu operating system.

WORKING:

It’s working is very simple to understand. Whenever, we use dynamic websites and enter some entries in the form and get the desired feedback then behind this action the thing which is working is nothing else CGI. CGI is a part of web server. We can say this as it acts as a gateway between the server and the programs you write.

Let’s take an example diagrammatically represented below:

Here, user is submitting a form after filling some details, on the web browser. When submission got completed it moves to the server. Here, actually the CGI runs. It calls for the script to program it and give response back toi the server. At final step, the server displays it on the browser. So, the major role of CGI is to call the script which in return call the programs to run to generate the appropriate output and finally response will be passed to the user. That’s all.

What we require to work with it:

Basically, we require the following packages:
1. Apache or any other Web Server. (Here, I’m Showing its work on Apache2)

$ Sudo apt-get install apache2

2. G++ (By default, it is available in Ubuntu OS)
3. Cgicc files
4. Configured Web Server with CGI

Now restart your apache server as:-

$ sudo /etc/init.d/apache2 restart

or

$ sudo service apache2 restart

To check out its working, type localhost/html/ as a URL in web browser

13

Web Server Configuration:

You can configure CGI on Apache by three ways:

1. In /usr/lib/cgi-bin:- The required steps are:

1. Open the terminal by Ctrl+Alt+T.
2. Type command sudo a2enmod
3. CGI sudo a2enmod cgid
4. sudo service apache2 restart
5. cd /etc/apache2/sites-available
6. sudo nano 000-default.conf
7. Add the following script:-

26

8. sudo service apache2 restart

Check for its working by:

1. Type cd /usr/lib
2. cd CGI-bin
3. sudo nano hell.cpp

31

4. g++ hell.cpp -o hell
5. sudo chmod -R 777 /usrlib/cgi-bin
6. sudo chown root.root /usr/lib/cgi-bin
7. Now, go to the browser and type “localhost/cgi-bin/hell”.

43

2. In ~/public_html/cgi-bin:- For this follow the following steps:

1. Open the terminal by pressing Ctrl+Alt+T
2. Type sudo a2enmod CGI
3. sudo a2enmod cgid
4. sudo a2enmod userdir
5. sudo service apache2 restart
6. cd /etc/apache2
7. cd sites-available
8. sudo nano 000-default.conf. Here, change the path in script from usr/lib to ~public_html as given below:

ScriptAlias /cgi-bin/ /home/simran/public_html/cgi-bin/

9. sudo service apache2 restart

To check its working follow the given steps:

1. Type sudo mkdir ~/public_html/cgi-bin
2. cd ~/public_html/cgi-bin
3. sudo vim first.cpp Write the program and save changes.
4. sudo service apache2 restart
5. g++ first.cpp -o first
6. go to browser type “localhost/~username/cgi-bin/first” to see the out put.

3. In /var/www/cgi-bin:- For this follow the given steps:

1. Open the terminal.
2. Type $ sudo a2enmod cgi
3. sudo a2enmod cgid
4. sudo service apache2 restart
5. cd /etc/apache2
6. cd sites-available
7. sudo vim 000-deafault.conf (We can use any editor nano/vim/any other)
8. Add the script-

ScriptAlias /cgi-bin/ /var/www/cgi-bin/

9. sudo service apache2 restart.

To check its working follow the steps given below:

1. Type sudo mkdir /var/www/cgi-bin
2. cd /var/www/cgi-bin
3. sudo vim third.cpp, Write the program and save this.
4. sudo service apache2 restart.
5. g++ third.cpp -o third
6. sudo chmod -R 755 /var/www/cgi-bin
7. sudo chown root.root /var/www/cgi-bin
8. go to browser type “localhost/cgi-bin/third

Output will be displayed on the browser.

* A Little explanation about the code:
a2enmod and a2dismod are scripts used to enable or disable an module within the apache2 configuration. It does this by creating symlinks within /etc/apache2/mods-enabled. Similarly, a2dismod disables the module by removing those symlinks.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s