Running Python CGI program in Ubuntu using apache2

 

Step1: Check if apache2 is isntalled in your ubuntu or not.

 

dulton@dulton-inspiron:~$ apache2 -version

Server version: Apache/2.4.18 (Ubuntu)

Server built: 2017-09-18T15:09:02

 

In mycase apache2 is already installed

 

If you don’t have apache2 installed then installed is using the below command

dulton@dulton-inspiron:~$sudo apt-get install apache2

 

Step2: Create directory

 

mkdir /var/www/cgi-bin

 

Step3: We have to edit two configuration files in apache to handle python cgi scripts bye apache.

Followings are the file:

  • /etc/apache2/apache2.conf
  • /etc/apache2/conf-available/serve-cgi-bin.conf

Step3.1: Editing the /etc/apache2/apache2.conf file
$sudo gedit /etc/apache2/apache2.conf
and add the following code in the end

###################################################################

#########     Adding capaility to run CGI-scripts #################

ServerName localhost

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

Options +ExecCGI

AddHandler cgi-script .cgi .pl .py

Step3.2: Editing the /etc/apache2/conf-available/serve-cgi-bin.conf

Edit the file so that the file looks like below

<IfModule mod_alias.c>

        <IfModule mod_cgi.c>

                Define ENABLE_USR_LIB_CGI_BIN

        </IfModule>

 

        <IfModule mod_cgid.c>

                Define ENABLE_USR_LIB_CGI_BIN

        </IfModule>

 

        <IfDefine ENABLE_USR_LIB_CGI_BIN>

                #ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

                #<Directory "/usr/lib/cgi-bin">

                #       AllowOverride None

                #       Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

                #       Require all granted

                #</Directory>

 

                ## cgi-bin config

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

            <Directory "/var/www/cgi-bin/">

                AllowOverride None

                Options +ExecCGI

            </Directory>

 

        </IfDefine>

</IfModule>

 

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

 

Step4: Restart apache2

$sudo service apache2 restart

Step5:

 cd /var/www/cgi-bin

/var/www/cgi-bin$ touch hello.py

##  make it executable for you and others

 

/var/www/cgi-bin$ chmod o+x hello.py

 

##  Put the following content just for testing inside `hello.py`

/var/www/cgi-bin$ sudo nano hello.py

#!/usr/bin/env python

 

import cgitb

cgitb.enable()   

print("Content-Type: text/html;charset=utf-8")

 

print "Content-type:text/html\r\n\r\n"

print '<html>'

print '<head>'

print '<title>Hello Word - First CGI Program</title>'

print '</head>'

print '<body>'

print '<h2>Hello Word! This is my first CGI program</h2>'

print '</body>'

print '</html>'

Run the Script:

Open your browser and enter the following link
http://localhost/cgi-bin/hello.py
And the script should run just fine.




Debugging :

If the script is not running, you can check the logs stored in
/var/log/apache2/error.log




If the code is not displaying in the browser and you getting the downloadable python file, run the following and restart your apache2

$sudo a2enmod cgi

 

Comments

Popular posts from this blog

IDEs for Python Beginners