cgi script is not executing - perl

I have a perl.cgi file which has the content:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<h1>Hello World</h1>\n";
I made it executable. (chmod a+x perl.cgi)
Then I created a new file perl.htm in the same directory. Which has the following data:
Content-type: text/html
<p>RUN perl.cgi</p>
When I run the perl.htm in my browser then I get the output as:
Content-type: text/html
RUN perl.cgi
When I click on RUN perl.cgi another page opens and there the output is:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<h1>Hello World</h1>\n";
i.e. the perl.cgi is not executing. Only the file contents are being shown.
EDIT: From the answers and comments I came to know that I will have to configure my web server (apache) to run cgi scripts. How can I do that? Let me know.

Sounds like Apache is not configured to run *.cgi files:
AddHandler cgi-script cgi pl
<Directory /path/to/cgi/files>
Options +ExecCGI
</Directory>

Make sure you are loading the CGI module in the httpd.conf file:
LoadModule cgi_module modules/mod_cgi.so or
LoadModule cgi_module modules/mod_cgid.so depending on which version of Apache you are running.
You can also read about additional solutions for
Dynamic Content with CGI.

Problem has been solved. I was not using httpd service at all, was opening from file:// URL.
Perl script is not executing in HTML form

You need to setup your web server so that it executes *.cgi files (or, more normally, all the files in a particular directory) rather than just delivering them to the browser as it does .html files.
How this is done depends on your server.

Related

Why is a Perl(.pl) file showing text instead of executing the file?

I am trying to make a simple program in perl. But whenever I try to run my file it always show the plain text instead of executing it.
Here is the code of my program I am working with:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html>\n";
print "<title> PERL CGI</title>\n";
print "<body>";
print "hello perl-cgi!!!!!!!";
print "</body>";
print "</html>\n";
To run a '.pl or .pm' file in an Apache web server which has the compiler to execute, it should normally placed in a cgi-bin folder. Else you should add following content in your .htaccess file or in your Apache configuration file.
AddHandler cgi-script .cgi .pl .pm
Options +ExecCGI
After that you should give the file permission of that file as 755
You have to configure your web server to execute Perl scripts. There are several methods, the most simple is CGI, and the alternatives are mod_perl, FastCGI and PSGI. Configuration of each method depends on what HTTP server is used.
In case you use CGI in Apache2, make sure your script is executable (provided you use Linux/OSX/other UNIX: chmod +x hello.pl), and directory containing your script has Options +ExecCGI directive in Apache config file. Typically, /cgi-bin directory is set up correctly for CGI scripts.
Consult your HTTP server documentation on how to configure it to serve dynamic content.
It seems that you are trying to execute the CGI scripts. This CGI scripts can be a Perl Script, or a shell script, or C/C++ Program.
So You need to set up the HTTP server, so that whenever a file in a certain directory is requested, that file is not sent back; instead it is executed as a program, and whatever that program outputs is sent back for your browser to display. This function is called the Common Gateway Interface or CGI and the programs are called CGI scripts.
Again Before you proceed with CGI Programming, make sure that your Web Server supports CGI and it is configured to handle CGI Programs. All the CGI Programs will be executed by the HTTP server if kept in a pre-configured directory. This directory is called CGI Directory and by convention it is named as /cgi-bin. By convention PERL CGI files will have extension as .cgi.
Here is a simple link which is linked to a CGI script called hello.cgi. This file is being kept in /cgi-bin/ directory and it has following content. Before running your CGI program make sure you have chage mode of file using chmod 755 hello.cgi UNIX command.
Code used for this hello.cgi is written below:
#!/usr/bin/perl
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>';
1;

Setting up mod_perl on OSX Lion Apache server

I am new to both perl and apache servers. I'm trying to get a basic Hello World going for a CGI script. Here is the code for my hello world CGI file:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<H1>Hello World</H1>\n";
When I execute the CGI script on the command line, ./hello.cgi, it works, but when I open hello.cgi in my browser, it just displays the text of the cgi file.
When I look at my error_log in /var/log/apache2/error_log I can see that mod_perl is running:
Apache/2.2.21 (Unix) DAV/2 mod_perl/2.0.5 Perl/v5.12.3 configured -- resuming normal operations
but when I run the following perl program, it appears that I don't have the "MOD_PERL" env variable:
if (exists $ENV{"MOD_PERL"}) {
print "YAY!\n";
}
else{
print"mod_perl is not working\n";
}
By the way, my hello.cgi file and the perl script above are located in /Users/myusername/Sites/
Could someone help me configure mod_perl properly so that I can properly view hello.cgi in my browser? I've been reading the mod_perl documentation and searching forums for many, many hours with no avail. Thanks in advance
#SebastianStumpf I got your first example to work, but I still can't seem to get a mod_perl script going. I've been reading through the documentation, but I haven't been able to figure it out. Thanks for your help, by the way. I'm very grateful
UPDATE: i believe I got it working! Thanks for the help!
If you are using the stock Apache/Perl of Mac OS X Lion and don't need mod_perl then you don't have to configure anything. Just create your file with the .cgi extension in /Library/WebServer/CGI-Executables and adjust the permissions via sudo chmod 755 file.cgi. The script will be executed via CGI (not mod_perl). I tried this and it worked just fine:
$ sudo -s
# cat - > /Library/WebServer/CGI-Executables/test.cgi
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw/:standard/;
use Data::Dumper;
print header, start_html, h1('works'), end_html;
^D
# sudo chmod 755 /Library/WebServer/CGI-Executables/test.cgi
# exit
Testing it:
$ curl -i http://localhost/cgi-bin/test.cgi
HTTP/1.1 200 OK
Date: Mon, 11 Jun 2012 22:29:24 GMT
Server: Apache/2.2.21 (Unix) DAV/2
Transfer-Encoding: chunked
Content-Type: text/html; charset=ISO-8859-1
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>works</h1>
</body>
</html>
If you need mod_perl, then have a look at the documentation. The configuration part of the introduction should be all you need to get mod_perl running.
EDIT:
I've added the following lines to /etc/apache2/httpd.conf, restarted the web server and mod_perl works:
LoadModule perl_module libexec/apache2/mod_perl.so
Alias /perl /Library/WebServer/perl
<Location /perl>
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
Options ExecCGI
PerlSendHeader On
Order allow,deny
Allow from all
</Location>
If the script is saved in /Library/WebServer/perl/ you can see that all mod_perl headers are available now, but I'd rather setup a private installation of Apache/mod_perl. MacPorts makes this a lot easier...

How do I run my first Perl code on a Windows 7 system?

I have run this Perl code:
#!/usr/bin/perl
print "content-type: text/html \n\n";
print "Hello World.\n";
I have tried it in two ways, first one is Testing your Perl installation, but when I run by this way, it has some troubles, it asks me to choose a program with which I can run it, but no running yet.
Second way is first script with Padre, the Perl IDE, but when I write Perl code and try to save it, it does not show me Perl file's extension, so I can't save it as Perl file, so what could I do?
Your code looks like you want a CGI program. CGI means that you call your program through a web browser and get a website back. While vstm's comment was of course right for non-cgi programs, your example requires a little more stuff in order to work like that.
You will need to install a web server. Take a look at xampp. It is simple to install and maintain and comes with a mysql as well as an apache installation. I recommend the lite version since that does not have all the overhead.
Once you've installed it, you need to make some configuration so it can run your perl scripts. I take it you have already installed Active Perl. You then need to tweak the apache config.
in c:\xampp\apache\conf\httpd.conf you need to find the line that says
<Directory "C:/xampp/htdocs">
and read the comments (marked with #). You have to add ExecCGI inside the <Directory> section. Do that for every directory you want perl scripts to be run. Then look for a line that says
AddHandler cgi-script .cgi .pl .asp
and make sure it is not commented out.
Once you're done, place your program in the c:\xampp\htdocs folder (cgi-bin should also work) and change the shebang-line (the first line with #!) to where you've installed Active Perl, e.g. C:\perl\bin\perl.exe. It tells apache what program it should use to execute the perl script.
Also, add some more lines to your code:
#!C:\perl\bin\perl.exe
use strict;
use warnings;
use CGI;
use CGI::Carp('fatalsToBrowser');
print "Content-type: text/html \n\n";
print "Hello World.\n";
Now you need to run the apache web server. In the xampp installation dir there are several batch files that control apache and mysql. There's also a xampp-control.exe. Run it. In the new window, click on the Start button next to Apache.
In your browser, go to http://localhost/<yourscript.pl>. It should now say "Hello World!".
If it does not, make sure you're not running Skype. It blocks your port 80 which the apache tries to run on. You need to change apache's port to something else. Refer to this video.
A few words on the changes in the code I made and what they do:
use strict; should always be in your code. It forces you to honor certain guidelines and to write better code. This might seem strange in a Hello World program, but please do it anyway.
use warnings; tells you about things that might go wrong. Warnings are not errors but rather perl being helpful about stuff you might not know yourself. Use it.
use CGI makes the program's output go to the web server. You need that when you work with CGI programs.
print "Content-type: text/html \n\n"; is needed so the browser knows what to expect. In this case, an HTML website. It is called the HTTP-Header and contains a mime-type.
use CGI::Carp('fatalsToBrowser'); makes errors go to the browser. Without it, you'd never know about them unless you look in apache's error log.

How do I run Perl files over XAMPP on Windows?

I am new to the Perl language, and I tried running it as I do for PHP files, by putting files in htdocs and then accessing them over localhost.
Below is the Perl file which I created, but wasn't able to run over localhost:
-----hello.pl---------------
#!/usr/bin/perl
print "Hello World.\n";
Install xampp. during installation, Make sure that, you have checked perl to be installed.
I assumed that, you have installed xampp in c:/xampp directory.
Now go to c:/xampp/htdocs directory. Inside htdocs directory create a directory perl. Now inside the perl directory, make a file named hello.cgi .
In hello.cgi write the following code snippet.
hello world program:
#!C:\xampp\perl\bin\perl.exe
# The above line is perl execution path in xampp
# The below line tells the browser, that this script will send html content.
# If you miss this line then it will show "malformed header from script" error.
print "Content-type: text/html\n\n";
print "Hello world."
Now start apache from xampp control panel. And in browser's url, enter localhost/perl/hello.cgi.
If your PHP install has the Perl module, you can evaluate Perl code directly from PHP.
<?php
print "Hello from PHP!";
$perl = new Perl();
$perl->require("test1.pl");
print "Bye!";
?>
First fix the "shebang" line to point to your Perl executable (I use WampDeveloper, not XAMPP, so your path will be different)...
#!C:/WampDeveloper/Tools/Perl/perl/bin/perl.exe
print "Hello World.\n";
Then create a "cgi-bin" directory inside the DocRoot and place you Perl script inside.
In this directory also create an .htaccess file with this inside...
DefaultType text/html
Options -Indexes +ExecCGI
SetHandler cgi-script
Go to the URL: http://www.example.com/cgi-bin/perlscript.pl
Note: This assumes the above directory does not have the htaccess option disabled for it in the main Apache configuration.
Please follow these steps:
Configure your web server to run Perl script (you may follow this url for more info http://editrocket.com/articles/perl_apache_windows.html).
Create your file (perl script ) and save it in your cgi-bin directory under root xampp.
(i.e : C:\xampp\cgi-bin).
N.B : your file should contain header info like
print "Content-type:text/html\r\n\r\n"; in top of script which will help browser to understand the type of information coming form web server.
Your script should have "shebang" line or else the server will throw an error.

Why doesn't my Perl CGI script work?

I really do not get how to run a Perl file. I have uploaded my .pl to the cgi-bin then chmod to 755. Then when i go to run the file i just get a 500 internal server error.
**/cgi-bin/helloworld.pl**
#!/usr/bin/perl
print 'hello world';
Any ideas as to what I am doing wrong?
Read the official Perl CGI FAQ.
That'll answer this, and many other questions you may have.
For example: "My CGI script runs from the command line but not the browser. (500 Server Error)"
Hope this helps!
You probably need something like
print "Content-type: text/html\n\n";
before your print statement. Take a look at http://httpd.apache.org/docs/2.0/howto/cgi.html#troubleshoot
It would help to know what server you are using, and the exact error message that's showing up in the server's logs. I'd guess that, if you are using Apache, you'll see something like "Premature end of script headers".
Look into using CGI::Carp to output fatal errors to the browser. use CGI::Carp qw(fatalsToBrowser);
Also, please definitely do use the CGI module to output any needed information such as headers/html/whatever. Printing it all is the wrong way to do it.
EDIT: You will also definitely be able to check an error log of some sort.
Perhaps you need my Troubleshooting Perl CGI scripts
First, find out the path to perl on that system and make sure the shebang line is correct. Giving more information about the system and the web server would also help others diagnose.
Then, try:
#!/path/to/perl/binary
use strict;
use warnings;
$| = 1;
use CGI qw( :default );
print header('text/plain'), "Hello World\n";
Make sure that you can run the script from a shell prompt, without invoking it through Perl. In other words, you should be able to go to your cgi-bin directory and type:
./helloworld.pl
and get output. If that doesn't work, fix that. In looking at the output, the first line must be:
Content-Type: text/html
(Or text/plain or some other valid MIME type.)
If that's not the case, fix that.
Then you must have an empty line before the body of your page is printed. If there's no empty line, your script won't work as a CGI script. So your total output should look like this:
Content-Type: text/html
hello world
If you can run your script and that's the output, then there's something weird going on. If Apache is not logging the error to an error_log file somewhere, then maybe there's some problem with it.
Did you enable Apache to server .pl files as CGI scripts? Check your Apache config file, or (quick but not guaranteed test) try changing the file extension to .cgi. Also, make sure your shebang line (#!) is at the very top. Finally, check the line endings are Unix if your server is Linux. And yes, test it from the command-line, and use strict; for better feedback on potential errors.