How to run perl/Python script from Linux Apache server

For httpd.conf ( /usr/local/apache/conf – if you compile by source OR /etc/httpd/conf/httpd -: if you compile by yum)

ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/"

If you want to run cgi script from under your domain , example , www.fosiul.com/cgi-bin/test.cgi , do as bellow

<VirtualHost *:80>
ServerAdmin fosiul@example.co.uk
DocumentRoot /usr/local/apache/htdocs/example/
ServerName www.example.co.uk
ServerAlias example.co.uk
......................................
......................................
 
<Directory "/usr/local/apache/htdocs/example/">
Options FollowSymLinks ExecCGI
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Directory "/usr/local/apache/htdocs/example/cgi-bin/">
        Order deny,allow
        Allow from all                
       Allow from xx.xx.xx.xx # If you just want to run cgi script from certain Ips , then you need to disable "Allow from All" options
       #Deny from all  # if you only want to allow cgi script from certain ip then you need to enable "Deny from all" options
        </Directory>
 
ScriptAlias /cgi-bin/ /usr/local/apache/htdocs/example/cgi-bin/

Now create a directory under : /usr/local/apache/example/cgi-bin
Create a cgi script

#!/usr/bin/perl -T
use strict;
use CGI;
my $cgi = new CGI;
print $cgi->header;
print $cgi->start_html('test world');
print $cgi->h1('Hellow test');
print $cgi->li('list');
print $cgi->end_html();

run this cgi script : http://www.example.co.uk/cgi-bin/test.cgi

How to run a python under cgi script

create a cgi script (testpy.cgi) as bellow to run python

#!/usr/bin/python
print "Content-Type: text/plain\n\n"
print "Hello, World!\n"

Now run this script as , www.example.co.uk/cgi-bin/testpy.cgi

Leave a Reply

*