Execute a .cgi file in Ubuntu

17,909

Solution 1

Add these lines to your apache2.conf file

<Directory /usr/local/apache2/htdocs/somedir>
    Options +ExecCGI
</Directory>

AddHandler cgi-script .cgi .pl

Solution 2

Obviously, CGI execution is not set up properly. Did you try the tutorial?

Usually, all CGI scripts are put into a certain location such as cgi-bin. Only the files present in that folder are executed at all. Otherwise, they are treated as normal files and the web server simply returns them as source.

The tutorial shows you the alternatives that you can use to make the scripts being executed instead of just being returned as text files.

Solution 3

Make sure that the shebang line in your .cgi or .pl file is correct. The shebang line tells Apache where the Perl interpreter is (the Perl interpreter is the perl.exe file).

The Apache tutorial says:

Your first CGI program The following is an example CGI program that prints one line to your browser. Type in the following, save it to a file called first.pl, and put it in your cgi-bin directory. #!/usr/bin/perl print "Content-type: text/html\n\n"; print "Hello, World.";

So here the shebang line is #!/usr/bin/perl

However, if perl.exe is located in a different folder, then #!/usr/bin/perl needs to be changed! This is true even for Windows users who, normally, can completely ignore and omit the shebang line. (Why the tutorial doesn't mention this fact, i don't know - i guess they didn't realise that some people always ignore the shebang line?)

For example, when I installed Perl it was automatically suggested that i install Perl in C:\Program Files (x86)\Perl64 Inside my Perl64 folder is folder called 'bin' and inside that is perl.exe

So i had to change the shebang line to: #!C:/Program Files (x86)/Perl64/bin/perl.exe before i could get cgi scripts to work with Apache.

Share:
17,909
X10nD
Author by

X10nD

Updated on June 09, 2022

Comments

  • X10nD
    X10nD almost 2 years

    I am running Apache/PHP under Ubuntu

    When I run the .cgi file, by going at http://localhost/mycgi.cgi, the browser will display the code instead of running it.

    How do I make the browser execute the CGI file instead of showing its contents?