24 January, 2011

Setting up CGI-BIN in apache in Ubuntu linux

Defining the CGI...

The Common Gateway Interface (CGI) is a method used for server programming in such a way that, web applications can be equipped with scripting languages like python as their back end, for processing the client requests. CGI are external gateway programs to interface with information servers such as HTTP servers.

CGI defines a way for a web server to interact with external 'content generating' programs, which are often referred to as CGI programs or CGI scripts. It is the simplest, and most common, way to put dynamic content on your web site. 

You might be quite familiar with the html-php web pages, where the php processor manages the dynamic content generation, while html & css gives the presentation of this content as a webpage. In CGI programming we can use scripting languages such as python, instead of php, for processing data coming from the html pages. 

Configuring Apache server to run CGI scripts  . . .

Here i will show you how to setup CGI on Apache web server in Ubuntu 10.10. 

1. First we have to enable three modules cgi, cgid and userdir Userdir is the module that enables the user to change the web server's root from /var/www to the public_html folder in the home directory. Replace the module_name with the above modules one after another

$ sudo a2enmod module_name

2. After enabling the modules we have to restart the apache server to make the changes take into effect.  This can be done as follows

$ sudo /etc/init.d/apache2  restart

3. Create the public_html inside your home folder to set it as the web root, and cgi-bin directory where the cgi-programs are placed.

$ cd ~

                                    $ mkdir public_html
                                    $ mkdir public_html/cgi-bin

4. Now we have to edit some configuration files of the apache server in the /etc/apache2 directory.  

$ cd /etc/apache2

The directory looks as follows..


5. Now move to the sites-available directory and open the file named default for editing it.

$ sudo gedit  sites-available/default

6. Move on to the line in the default starting with ScriptAlias, replace this line with the text inside the quotes given below. Don't forget to replace 'jo' with your username.

" ScriptAlias /cgi-bin/ /home/jo/public_html/cgi-bin/ "

By aliasing we can refer to a script inside the cgi-bin directory by just using /cgi-bin/scriptname.py instead of typing the full path.
7. Now change the <Directory> tag just below it with the same location given in the ScriptAlias. Please refer the screenshot to view the changes
  
8. Add the text inside quotes given below, just after the line starting with Options +ExecCGI...... (in screenshot)

" SetHandler cgi-script "

This line will tell the apache to consider any script (python, perl, php...) inside the cgi-bin directory as executable...

9. It's time to restart the apache server to apply the changes.

$ sudo /etc/init.d/apache2  restart

This is all about configuring the apache server to accomodate cgi scripts... 

Writing the first CGI program . . .

Two things have to be considered while writing the cgi scripts. First, all output from your CGI program must be preceded by a particular type HTTP header that tells the client what sort of content it is receiving. It looks as follows . . .

Content-type: text/html 


Secondly, your output needs to be in HTML, or some other format that a browser will be able to display. Most of the time, this will be HTML, but occasionally you might write a CGI program that outputs a gif image, or other non-HTML content.

Taking the first scoop...

Let's try a small cgi script in python as shown below.


 #!/usr/bin/python
 print "Content-type: text/html\n\n"
 print "Hello, World."
 for i in 'hello':
     print i+'\n' 

Save the progam with name sample.py in the public_html/cgi-bin/ directory. 


Now make the sample.py as an executable program with chmod command in bash as shown below


                                    $ cd ~/public_html/cgi-bin/
                                    $ chmod 777 *


Now take the web browser and type in the address bar as follows: 

                                    localhost/cgi-bin/sample.py
or
                                    localhost/~jo/cgi-bin/sample.py
[ replace 'jo' with your username ]  

Type the above line and hit the enter key. If the browser shows hello world (refer screenshot below) then everything is working right. 

4 comments:

yotam on Saturday, June 25, 2011 said...

Is there a clean way to allow _all_ users
to have such
/home/*/public_html/cgi-_bin
and not just USER=jo ?

Suggestion. Instead of
$ cd ~/public_html/cgi-bin/
$ chmod 777 *
it would be safer to
$ cd ~/public_html/cgi-bin/
$ chmod 755 *
That is allow others to use but not modify.

Unknown on Monday, October 10, 2011 said...

Need some help !!
I have a python code in which I want user to enter some file name and then use the file name to process the data.
e.g.,
fname = raw_input("Enter file name: ")
So how do I create a box for the user to enter the name and then assign that name to variable "fname" ?

Unknown on Tuesday, April 17, 2012 said...

So good topic really i like any post talking about Business Ideas and Advices but i want to say thing to u Business not that only ... you can see in Business Business and Profit and more , you shall search in Google and Wikipedia about that .... thanks a gain ,,,

rajesh on Monday, July 16, 2012 said...

Thank you so much dude...it worked for me.

Post a Comment

speak out... itz your time !!!

 

Terminal Diary | techblog Copyright © 2009 Gadget Blog is Designed by jintu jacob Powered by Blogger