关于mod_perl 1.0 的安装笔记

起因
前天突然发现服务器负载很高,达到18,非常奇怪。检查了一下,发现进程 perl 的 cpu 占用率很高。因为论坛是用 perl 写的,所以检查了 apache 的日志,发现有人是用离线浏览器多线程访问论坛。看了论坛的相关说明,提到使用 mod-perl 可以提高性能,所以想试用一下 mod-perl。

perl.apache.org

什么是 mod_perl?
Apache/Perl 整合项目的目的是将 Perl 程序语言和 Apache 服务器的能力进行整合。使用 mod-perl,将有可能完全用 Perl 写 Apache 的模块,而且很容易做一些事情,这些事情是平常的 cgi 程序不可能做到的。除此之外镶嵌在服务器中的 Perl 解释器可以节省开启外部解释器所耗费的资源。mod-perl 另一个很重要的功能叫代码缓冲,所谓代码缓冲是指模块和脚本只需要被载入和编译一次,当需要的时候直接从缓存中取出。所以,服务器只需要花费很少的时间来运行已经编译好的脚本和模块,这样服务器速度将会非常快。

mod_perl 的安装
首先要注意的:

Apache 2.0 doesn’t work with mod_perl 1.0.
Apache 1.0 doesn’t work with mod_perl 2.0.

最基本的安装总结


% cd /usr/src
% lwp-download http://www.apache.org/dist/httpd/apache_1.3.xx.tar.gz
% lwp-download http://perl.apache.org/dist/mod_perl-1.xx.tar.gz
% tar xzvf apache_1.3.xx.tar.gz
% tar xzvf mod_perl-1.xx.tar.gz
% cd mod_perl-1.xx
% perl Makefile.PL APACHE_SRC=../apache_1.3.xx/src DO_HTTPD=1 USE_APACI=1 EVERYTHING=1
% make && make test && make install
% cd ../apache_1.3.xx
% make install

freebsd下安装非常简单,有相关的 ports,感觉非常好。

设置和启动 mod_perl 服务
首先要保证 Apache 能正常服务普通的静态文件,这样有助于排错。
然后在 httpd.conf 添加以下字段

Alias /perl/ /home/stas/modperl/

PerlModule Apache::Registry

SetHandler perl-script
PerlHandler Apache::Registry
Options +ExecCGI
PerlSendHeader On
allow from all

测试
写一个测试的脚本 test.pl,内容如下

print “Content-type: text/plainnn”; print “mod_perl rules!n”;

你注意到,mod_perl 是不需要 #! 这一行的。

http://localhost/perl/test.pl
如果输出是以下这样子的

Content-type: text/plain

mod_perl rules!

恭喜你,你的 mod_perl 搭建成功了。

Post a Comment