X10nD
X10nD

Reputation: 22050

Execute a .cgi file in Ubuntu

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?

Upvotes: 5

Views: 18746

Answers (4)

Hugh Barnard
Hugh Barnard

Reputation: 352

This foxed me on Amazon Ubuntu 14.04. I had a configuration that had 'worked' for a few years:

sudo a2enmod cgi
sudo service apache2 restart

Sorted this out for some existing Perl scripts with a .cgi extension and the existing virtual host configuration. Also wrong [not this problem but associated with this upgrade]:

virtual.host

needed to become

virtual.host.conf

before it was recognised as valid.

Upvotes: 0

Saariko
Saariko

Reputation: 1703

Add these lines to your apache2.conf file

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

AddHandler cgi-script .cgi .pl

Upvotes: 13

Literat
Literat

Reputation: 869

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.

Upvotes: 1

eckes
eckes

Reputation: 67167

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.

Upvotes: 2

Related Questions