Ubuntu 24.04
Sponsored Link

Apache2 : Configure mod_perl2024/05/31

 

Install [mod_perl] to make Perl scripts be fast.

[1] Install [mod_perl].
root@www:~#
apt -y install libapache2-mod-perl2
[2] Configure [PerlRun] mode which always put Perl interpreter on RAM.
root@www:~#
vi /etc/apache2/conf-available/mod_perl.conf
# create new
# for example, set PerlRun mode under the "/var/www/perl"

PerlSwitches -w
PerlSwitches -T

Alias /perl /var/www/perl
<Directory /var/www/perl>
    AddHandler perl-script .cgi .pl
    PerlResponseHandler ModPerl::PerlRun
    PerlOptions +ParseHeaders
    Options +ExecCGI
</Directory>

<Location /perl-status>
    SetHandler perl-script
    PerlResponseHandler Apache2::Status
    Require ip 127.0.0.1 10.0.0.0/24
</Location>

root@www:~#
a2enconf mod_perl

Enabling conf mod_perl.
To activate the new configuration, you need to run:
  systemctl reload apache2

root@www:~#
a2enmod cgid

Enabling module cgid.
To activate the new configuration, you need to run:
  systemctl restart apache2

root@www:~#
systemctl restart apache2
[3] Create a test Script to make sure the settings are no problem. It's OK if the result like follows is displayed.
root@www:~#
mkdir /var/www/perl

root@www:~#
vi /var/www/perl/test-mod_perl.cgi
#!/usr/bin/perl

use strict;
use warnings;

print "Content-type: text/html\n\n";

my $a = 0;
&number();

sub number {
    $a++;
    print "number \$a = $a \n";
}

root@www:~#
chmod 705 /var/www/perl/test-mod_perl.cgi

root@www:~#
curl localhost/perl/test-mod_perl.cgi

number $a = 1
[4] Configure [Registry] mode which has caches of executed codes on RAM.
root@www:~#
vi /etc/apache2/conf-enabled/mod_perl.conf
Alias /perl /var/www/perl
<Directory /var/www/perl>
    AddHandler perl-script .cgi .pl
    # comment out PerlRun mode and add Registry mode like follows
    #PerlResponseHandler ModPerl::PerlRun
    PerlResponseHandler ModPerl::Registry
    PerlOptions +ParseHeaders
    Options +ExecCGI
</Directory>

root@www:~#
systemctl reload apache2
[5] Access to the test script which is an example of [3] section, then variable increases by reloading because variable is cached on RAM. So it's necessary to edit the code for Registry mode.
root@www:~#
curl localhost/perl/test-mod_perl.cgi

number $a = 1
root@www:~#
curl localhost/perl/test-mod_perl.cgi

number $a = 2
root@www:~#
curl localhost/perl/test-mod_perl.cgi

number $a = 3
root@www:~#
vi /var/www/perl/test-mod_perl.cgi
#!/usr/bin/perl

use strict;
use warnings;

print "Content-type: text/html\n\n";

my $a = 0;
&number($a);

sub number {
    my($a) = @_;
    $a++;
    print "number \$a = $a \n";
}

root@www:~#
curl localhost/perl/test-mod_perl.cgi

number $a = 1
root@www:~#
curl localhost/perl/test-mod_perl.cgi

number $a = 1
root@www:~#
curl localhost/perl/test-mod_perl.cgi

number $a = 1
[6] By the way, it's possible to see the status of [mod_perl] to access to [(your hostname or IP address)/perl-status].
Matched Content