Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run cgi script on apache server

Tags:

apache

cgi

perl

This is my program:

[root@localhost cgi-bin]# locate first.pl 
/home/Ram/Desktop/work/first.pl
/usr/local/apache2/cgi-bin/first.pl
[root@localhost cgi-bin]# cd /usr/local/apache2/cgi-bin/
[root@localhost cgi-bin]# vi first.pl 
#!/usr/bin/perl -w
use warnings;
use warnings;
use CGI;
print "content-type: text/html\n\n";
print "<h2>Hello, World!</h2>\n";

I am calling the script from my browser like this:

http://localhost/usr/local/apache2/cgi-bin/first.pl

I didn't get output, but I'm getting an error:

Not Found

The requested URL /usr/local/apache2/cgi-bin/first.pl was not found on this server.

Apache/2.2.15 (CentOS) Server at localhost Port 80

I checked in web browser whether the Apache web server is working or not by using :

https://localhost

It us showing the welcome page.

How do I resolve this error?

like image 913
Ram Avatar asked Dec 19 '22 18:12

Ram


2 Answers

You basically need to change two files after installing apache2 on linux.

Go to terminal and set the following configs:

  1. sudo vim /etc/apache2/sites-enabled/000-default.conf and add the follwing:

    <Files ~ "\.(pl|cgi)$">
        SetHandler perl-script
        PerlResponseHandler ModPerl::PerlRun
        Options +ExecCGI
        PerlSendHeader On
    </Files>
    
  2. sudo vim /etc/apache2/apache2.conf and add the following:

    <Directory /var/www/cgi-bin/> AddHandler cgi-script .cgi .pl Options FollowSymLinks ExecCGI AllowOverride None </Directory>

After adding these two config changes, write a perl script, place it in the cgi-bin directory, and then give it sufficient privileges (sudo chmod 755 <filename>)

Finally, restart apache2: sudo apache2ctl restart

Screenshots: default.conf apache2.conf

like image 50
Chinmay Hegde Avatar answered Dec 27 '22 08:12

Chinmay Hegde


Yes, the above process works but the easy way is:

  1. Enable the CGI- sudo a2enmod cgi
  2. Restart the Apache and it works -service apache2 restart
  3. Run the cgi file http://localhost/cgi-bin/1.sh

Best Of Luck !!

like image 24
Arzoo Singh Avatar answered Dec 27 '22 08:12

Arzoo Singh