Add Apache2 Perl Support in Ubuntu 12.04

Adam posted @ Tue, 08 Apr 2014 18:44:47 +0800 in Linux with tags linux web ubuntu apache , 2719 readers

Add Apache2 Perl Support in Ubuntu 12.04

  • sudo apt-get install libapache2-mod-perl2 (if Ubuntu Server use aptitude install)
  • ( if the perl-mod is not enabled, you may use: sudo a2enmod perl )
  • mkdir ~/www/cgi-bin (it could be anywhere, just take it as an example)
  • Add the following code to /etc/apache2/sites-enabled/000-default file before </VirtualHost> line.
# Use 'ModPerl' module to process '.pl' and '.cgi' file
<Files ~ "\.(pl|cgi)$">
    SetHandler perl-script
    PerlResponseHandler ModPerl::PerlRun
    Options +ExecCGI
    PerlSendHeader On

# Use '/home/www/cgi-bin/' as a CGI Directory
ScriptAlias /cgi-bin/ /home/www/cgi-bin/
<Directory "/home/www/cgi-bin/">
    Options ExecCGI
    AddHandler cgi-script cgi pl
  • Restart the apache2 service: sudo service apache2 restart
  • Test the perl environment:
  • Create file in /home/www/cgi-bin/:
#!/usr/bin/perl -w
print "Content-type: text/html\r\n\r\n";
print "Hello there!<br />\nJust testing .<br />\n";

for ($i=0; $i<10; $i++)
print $i."<br />";
  • Note 1: #!/usr/bin/perl -w is used to locate the perl parser which could be located by which perl.
  • Note 2: code print "Content-type: text/html\r\n\r\n"; is required to make it a valid CGI program.

Login *

loading captcha image...
(type the code from the image)
or Ctrl+Enter