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...
Related
I am new to CGI. I wrote a very complex module around perl, cgi, html and javascript. And it runs perfectly on command line. But I am not able to run it via browser. I went on debugging line by line from bottom of my script, only to find that the issue was around the HTML::TableExtract module itself. So to make it simple -
# perl -c test.cgi
test.cgi syntax OK
test.cgi
#!/usr/bin/perl
use strict;
use warnings;
use HTML::TableExtract;
print "Content-type: text/html\n\n";
print <<htmlcode;
<html>
<head>
<title>CGI Perl Example</title>
</head>
<body>
<h1>CGI Perl Example</h1>
<p>CGI Perl Example</p>
</body>
</html>
htmlcode
This works perfectly on command line. But if I run it via Browser it just doesn't work. However, if I remove "use HTML::TableExtract" it works perfectly fine again - even in browser. The permissions are correctly set to 755.
Can someone please help me understand, what I am missing? And how can I fire it up from browser. How can I go about debugging this - My browser redirects me to page not found if I mention use HTML::TableExtract.
Note: Would like to point out one thing, this may be related to setting up some environment variable around HTML::TableExtract. When I first installed the module, there was an error which my hosting administrator helped resolve.
# ./test.cgi
Content-type: text/html
<html>
<head>
<title>CGI Perl Example</title>
</head>
<body>
<h1>CGI Perl Example</h1>
<p>CGI Perl Example</p>
</body>
</html>
Debugging perl CGI scripts
Add use CGI::Carp; to you script to make it report bugs to browser
[It produces http reply over STDOUT instead of default text over STDERR]
Most likely scripts executed from command line and from web server search for modules in different places e.g. due to different settings of PERL5LIB or PERLLIB environment variables.
WARNING
CGI::Carp may be a security threat in "production" version of cgi scripts.
It may provide crucial information to (potential) hackers.
Hi since other posts about this topic didn't do me much justice ( none of them seem to apply for Ubuntu 13.10, the version of Ubuntu I run), I decided to make another one. After running these lines ( a fellow stackoverflow member suggested running these)..
cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/cgi.load .
sudo ln -s ../mods-available/cgid.load .
sudo service apache2 restart
I placed the cgi files ( they are perl ones ) into my Apache2's cgi-bin # /usr/lib/cgi-bin. When typing localhost/cgi-bin/test.cgi, I got a 500 Internal server error. This is what the Server error log says..
[Fri Nov 22 21:23:29.045785 2013] [cgi:error] [pid 9559] [client 127.0.0.1:47663] AH01215: (8)Exec format error: exec of '/usr/lib/cgi-bin/test.cgi' failed
[Fri Nov 22 21:23:29.046720 2013] [cgi:error] [pid 9559] [client 127.0.0.1:47663] End of script output before headers: test.cgi
test.cgi looks like this...
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print <<HTML;
<html>
<head>
<title>A Simple Perl CGI</title>
</head>
<body>
<h1>A Simple Perl CGI</h1>
<p>Hello World</p>
</body>
HTML
exit;
Does anyone know what to do when then happens or have any suggestions? Thanks
EDIT:: Oddly enough, I got this cgi file i call test2.cgi to run.
#!/usr/bin/perl
use strict;
use warnings;
sub main {
print "Content-type: text/html\n\n";
print "Hello world\n\n";
print "What's your favorite food brah?\n";
}
main();
But the larger, more advanced cgi files that I need to work on wont run. These ones include stuff being printed out in html tags.
EDIT: Ignore any weird spacing in code. Its just how i copied it into the post.
Edit: Its not quite clear if there are extra spaces in the original code at the start of each line causing this issue, if not, ignore the rest of this! Thanks Amon
If you try running them from the console, it will help see immediately any errors. In this case...
Can't find string terminator "HTML" anywhere before EOF
#!/usr/bin/perl # make sure no space before # and this comment is not here
print "Content-type: text/html\n\n";
print <<HTML;
<html>
....
</body>
HTML
exit; # Make sure the HTML line before this has nothing either side of it
Basically, remove the single space before the #!/usr/bin/perl line as Amon points out if it exists, and also the closing HTML line should fix it (There shouldn't be anything surrounding the heredoc terminator, its not obvious there are spaces at the start of each line, but there are).
Sometimes when putting example code on a site, spaces etc may be introduced to help visually. I'm guessing some were introduced here that were copied into the code. Hard to spot!
I'm having problems with apache2 on Ubuntu ( 11.04 and 12.04 ) buffering all cgi output until the script terminates. If I run the same script on Centos/rhel 6.2 apache2, it runs normally.
#!/usr/bin/perl
$|=1;
print "Content-type: text/html\r\n\r\n";
print "hi..";
sleep 1;
print "hi..";
sleep 1;
print "hi..";
sleep 1;
I have verified that mod_deflate is disabled.
Also, it's NOT just a perl thing, the same cgi script written in bash behaves the same on Ubuntu VS centos/rhel.
I encountered a similar problem on Solaris 10, but found out, that is was actually not a problem of apache but instead of the web browser (firefox 15.0.1).
(I could verify this with telnet webserver 80 and speaking plain HTML, the response showed that the output was indeed not buffered)
I could solve this for firefox by also printing a header with a Content-Type meta tag:
print<<'_EOF_';
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
_EOF_
Explorer however still seems to wait for all data before rendering the page, other browsers I do not have available.
The debian/ubuntu ( and solaris too obviously ) apache package stock configs doesn't specify the character set like on Redhat. The real solution is to define simply it.
on ubuntu, add the following to /etc/apache2/httpd.conf
AddDefaultCharset UTF-8
Wihtout it, the browser caches the output of the cgi script.
For me it helps to disable the deflate module:
sudo a2dismod deflate
Can someone please explain to me the very basics of getting perl to work on a server. Do I need a module on the server? If so where does it go? What do I name my files and where do they go?
From my understanding you need a module and it goes in the cgi-bin. I can't get a clear answer whether I name the file .pl or .cgi and when I put it in the cgi-bin I am getting a server error. I also have my permissions set to 777, so that shouldn't be the problem.
Please help! I just want to understand the how to get the very basic program working such as the one below. Thanks in advance!
#!/usr/bin/perl
require("cgi-lib.pl");
print &PrintHeader;
print "<html>";
print "<head><title>Hello world!</title></head>";
print "<body>";
print "<p>Hello world!</p>";
print "</body>";
print "</html>";
The latest version of cgi-lib.pl is dated 1999 and is very out of date. I suggest you use the CGI library instead, which is almost bound to be installed already on your server and is kept up to date (most recently on August 16 2012)
Your program should look like this:
#!/usr/bin/perl --
use strict;
use warnings;
use CGI ':standard';
print header;
print <<END;
<html>
<head><title>Hello world!</title></head>
<body>
<p>Hello world!</p>
</body>
</html>
END
Also note that you can run the program from the command line to see if it compiles and what output it generates. Once you have it working there you can move it to the server
The problem you are facing is you probably edited the source file on a Windows machine, which inserts a CR character before each linefeed. Make sure your code does not include any CRs or change the first line to:
#!/usr/bin/perl --
(that's two dashes and a space at the end of the line)
I concur with Dave Cross' comment, you need to tell your school that they are teaching you incorrectly. We in the Perl land are trying to get people to stop using the CGI module, and you are using its predecessor!
Here is a hello world app in a modern Perl framework, Mojolicious:
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/' => 'hello';
app->start;
__DATA__
## hello.html.ep
<html>
<head><title>Hello world!</title></head>
<body>
<p>Hello world!</p>
</body>
</html>
which you place into a file, (lets say test.pl). Install Mojolicious by running:
curl get.mojolicio.us | sh
and then start your app by running
perl test.pl daemon
Now you can visit http://localhost:3000 in your browser to see the result, no Apache, no cgi-bin needed!
A more fun example takes an "argument" with a default:
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/:name' => { name => 'world' } => 'hello';
app->start;
__DATA__
## hello.html.ep
<html>
<head><title>Hello <%= $name %>!</title></head>
<body>
<p>Hello <%= $name %>!</p>
</body>
</html>
Run this and try visiting http://localhost:3000/SilverNightaFall and see what it does!
This process of interpolating dynamic values into your HTML is known as templating, and it is much preferred these days (vs generating your entire HTML on each request).
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.