shivsta
shivsta

Reputation: 145

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!

Upvotes: 3

Views: 2163

Answers (1)

Sebastian Stumpf
Sebastian Stumpf

Reputation: 2791

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...

Upvotes: 3

Related Questions