How to configure Apache2 with CGI

Apache2 is a web server and CGI is a common gateway interface that defines how information will be exchange between the web server and script. But firstly you have to configure your apache2 server with CGI. Follow below steps:

1.Install apache2 from terminal

 $ sudo apt-get install apache2

2. For configuration of cgi ,You have to add  CGI code  in your apache file. Go to /etc/apache2/sites-available directory and open 000-default.conf for adding CGI code. FIrst of all change permission of this file  using chmod command.

$ sudo chmod 661 000-default.conf

3. Now add CGI code in 000-default.conf  between:

ServerAdmin webmaster@localhost
 DocumentRoot /var/www/
----// CGI code
ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

4. Add below CGI code .

   <Directory />
        Options FollowSymLinks
        AllowOverride None

      <Directory /var/www/>
        Options ExecCGI Indexes FollowSymLinks MultiViews
        AllowOverride None
        Require all granted
        Addhandler cgi-script .py
     ScriptAlias /cgi/bin/ /usr/bin/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
        AllowOverride None
         Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
         Require all granted

5. Run

$ sudo service apache2 restart

6 .Now check if your CGI works or not.Type simple program in python. You don’t have a need to install python Because it is preinstall.
#!/usr/bin/python     //don't miss it. gives information about language.
print "content-type:text/html\r\n\r\n"   //Through it,Browser understand the content.
print ("Python run through cgi")

place in /usr/lib/cgi-bin folder. All script files are stored in this. Basically /var/ww/html stores html files , But script files can be stored in /var/ww/html

7. Run this command on terminal

$ sudo chmod 755

8. Type ” localhost/cgi-bin/” in browser address bar.

9. If your file is in /var/www/html ,Then type “localhost/” in browser.

10.  Done


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s