I am trying to run this script
mail.pl
#!/usr/bin/perl
print "Content-type: text/html \n\n"; #HTTP HEADER
#CREATE
$email = "pt-desu\#*****-****.com";
#PRINT
print "$email<br />";
The file could be accessed via http://mysite.com/p/cgi-bin/mail.pl but when you go there the browser proms to download the file and does not display anything
Your webserver isn't set up to process *.pl files as Perl; it's instead just serving them up as plain-text. Consult your webserver's documentation for setting this up.
For Apache, try consulting this tutorial. The key bits are:
AddHandler cgi-script .cgi .pl
and
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
and
Options FollowSymLinks +ExecCGI
...in your httpd.conf file.
Related
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...
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.
I am fairly new with perl and apache and seem to be having a small problem with my code.
I have 3 files:
hw.pm
package hw;
sub calc {
my $num1 = shift;
my $num2 = shift;
return $num1 + $num2;
}
1;
startup.pl
use lib qw(path to where hw.pm is located);
1;
hel.pl
#!/usr/bin/perl -w
use hw;
use CGI qw(:standard);
print header;
my $ans = calc(5,4);
print $ans;
I have no problem restarting apache but when I access hel.pl from the browser I get an error Can't locate hw.pm in #INC
Should the startup.pl have already included it in #INC? Or am I missing something?
I am using perl v5.10.1 and Apache2 v2.2.16
Perl is not finding hw.pm.
Try copying this line from startup.pl
use lib qw(path to where hw.pm is located);
to hel.pl, replacing the "use hw;" there. But first make sure the path is correct.
#INC - The array #INC contains the list
of places to look for Perl scripts to
be evaluated by the do EXPR , require
, or use constructs. It initially
consists of the arguments to any -I
command line switches, followed by the
default Perl library, probably
"/usr/local/lib/perl", followed by
".", to represent the current
directory.
I managed to solve it. initially i had this in my apache2.conf:
PerlRequire startup.pl
but after adding this code:
<Directory /var/www>
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
PerlOptions +ParseHeaders
Options +ExecCGI
</Directory>
I was able to access my modules from hel.pl
Thanks guys for your help.
I have a script that's written in perl, and executed as CGI. It works fine. Recently I have installed the mod_perl module into apache, and used the PerlModule ModPerl::Registry directive.
PerlModule ModPerl::Registry
PerlModule CGI
PerlSendHeader On
Alias /perl/ /real/path/to/perl/scripts/
<Location /perl>
SetHandler perl-script
PerlHandler ModPerl::Registry
Options ExecCGI
</Location>
<Files *.perl>
SetHandler perl-script
PerlHandler ModPerl::Registry
Options ExecCGI
</Files>
I've read that using this I do not need to modify my cgi perl code. (I always use strict pragma, so don't worry about uninitialized global variables and stuff like that).
My original script still works as intended, except for one thing, files that I included with the require() function can no longer be resolved.
script.cgi:
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard Vars);
require "includes/functions.cgi";
#blah blah, more stuff
script.perl
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard Vars);
require "includes/functions.perl"; # <---- Returns error: Can't locate includes/functions.perl in #INC
#blah blah, more stuff
The directory structure works like this:
$ ls
script.cgi script.perl includes/
$ ls includes/
functions.cgi functions.perl
From: http://perl.apache.org/docs/2.0/api/ModPerl/Registry.html
META: document that for now we don't chdir() into the script's dir, because it affects the whole process under threads. ModPerl::RegistryPrefork should be used by those who run only under prefork MPM.
so, if you're using Apache2's prefork MPM, you should try using ModPerl::RegistryPrefork. If you're using worker, or event, or windows, you're going to have to change your program to not assume that the cwd is the directory that that the perl is sitting in.
I want to learn DOCUMENT_ROOT in startup.pl, but the best I can do is to learn server_root:
use Apache2::ServerUtil ();
$server_root = Apache2::ServerUtil::server_root();
which is quite useless. I can set an environment variable with
SetPerlEnv DOCUMENT_ROOT /path/to/www
but I don't like extra configuration if possible.
Is there a way to get DOCUMENT_ROOT by other means?
See Apache2::Directive. For example, on my development system:
use Apache2::Directive ();
my $tree = Apache2::Directive::conftree();
my $vhost = $tree->lookup(VirtualHost => 'unur.localdomain:8080');
File::Slurp::write_file "C:/bzzzt.txt", [ $vhost->{DocumentRoot}, "\n" ];
created a file C:/bzzzt.txt with the contents "E:/srv/unur/deploy/htdocs" after I discovered that I had to specify my virtual hosts using
<VirtualHost unur.localdomain:8080>
...
</VirtualHost>
<VirtualHost qtau.localdomain:8080>
...
</VirtualHost>
rather than <VirtualHost *:8080>. Otherwise, each <VirtualHost *:8080> section was overwriting the previous one.
This is annoying. I would have thought each VirtualHost entry would have been keyed by the ServerName used.
As for if there is an easier way, I am afraid there isn't if you want to do this in startup.pl. However, I am not sure if it is necessary to do it in startup.pl. You can find out the document root while processing a request as well using Apache2::RequestUtil::document_root.
If you are running Registry scripts, and want to change to DOCUMENT_ROOT, then you should be able to add:
chdir $ENV{DOCUMENT_ROOT}
or die "Cannot chdir to '$ENV{DOCUMENT_ROOT}': $!";
to the script instead of having to mess around with startup.pl and handlers etc.