转载:CGI编程之Apache服务器安装配置:Perl

出自:

http://www.jb51.net/article/33909.htm

http://www.cnblogs.com/hustcat/archive/2008/06/11/1217337.html

 

 

windows下

使用perl进行CGI开发必须安装WEB服务器,一般用Apache比较好,因为它可跨平台,并且可以经perl、python等模块编译其中,速度更快,下面就简单介绍一下Apache在windows下的安装和配置:

    (1)安装Apache

     在windows下下载Apache的安装包,直接安装即可,例如本人将Apache安装在D:\Program Files\Apache2.2中,cgi_bin目录为 D:\Program Files\Apache2.2\cgi-bin;perl.exe目录为d:/perl/bin

    (2)配置Apache

    在开始菜单中用文本编辑器打开Apache的配置文件httpd.conf文件,

   1.搜索cgi-bin,找到ScriptAlias /cgi-bin/ "D:/Program Files/Apache2.2/cgi-bin/",如果前面有#号,则删除(windows中默认没有#号),这是存放cgi文件的路径
   2. 搜索AddHandler  找到AddHandler  cgi-script .cgi ,这是定义可执行cgi文件扩展名,可以把.cgi 改为 .pl 或加上”, .pl” ,成为“AddHandler cgi-script .pl ,.cgi“这样两个后缀都可以用了。
  3.修改cgi端口
   默认为80端口,可修改为其他端口。方法是找到Listen项,Listen  80那一行,后面的80改为一个不常用的端口,如1087
    
  4.更改Options、Allow Override的参数为All。
      改完之后象这样:
      #
      # "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin" should be changed to whatever your ScriptAliased
      # CGI directory exists, if you have that configured.
      #
      <Directory "d:/wamp/apache2/cgi-bin">
          AllowOverride all
          Options all
          Order allow,deny
          Allow from all
      </Directory>
  5.测试
       建立文件 test.pl , 内容如下:
      #!d:/Perl/bin/perl   
      ##   写成!d:/Perl/bin/perl.exe也可以
      ## 注意,如果没有第一行或写错,apache找不到perl解释器
      ## ,会出现500 Internal Server     
      print "Content-type:text/html\n\n";
      print "CGI执行成功!"
      将此文件放入D:\Program Files\Apache2.2\cgi-bin中
 
      在任意位置建立一个html文件,内容如下:
      <a href="http://localhost/cgi-bin/test.pl" > CGI</a>
      IE截图:
   
      如果没有错误的话,至此配置成功! 
 
 
 
 
 
linux下以centos6.5为例:

1、linux系统一般自带perl
可运行程序在:/usr/bin/perl

2、perl测试程序
#!/usr/bin/perl -w
use warnings;
print "Hello, Perl works!  ";

 

 

命名为test.pl

在终端下,定位到该目录,输入perl test.pl,perl正常工作的话,会显示输出
Hello, Perl works!

3、让apache2以cgi方式支持perl
a. apache2安装完成之后,
配置文件位于/etc/httpd目录下,主要是httpd.conf。
但是修改的时候主要可以修改sites-available下面的default;
修改完成之后重启httpd,需要在终端输入/etc/init.d/httpd之后添加参数stop, start, restart  或者  service httpd 后跟参数 restart、start、stop。

b. 修改apache2的配置文件使得其以cgi方式支持perl

ScriptAlias /cgi-bin/ /var/www/cgi-bin/
    <Directory "/var/www/cgi-bin">
        AllowOverride all      
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

 

设置目录/cgi-bin/对应的为/var/www/cgi-bin/
(即http://localhost/cgi-bin/目录下为cgi脚本)
同时修改AllowOverride, Options, Order, Allow等如上。
还可以添加
AddHandler cgi-script .cgi .pl

自定义支持的后缀(默认情况下支持cgi和pl后缀)

4、编写cgi测试程序
cgi_test.pl

#!/usr/bin/perl -w
use warnings;
use CGI qw(:standard);
#! must use 'my' to define a variable
print header;
my $now_string = localtime();
print "<b>Hello, CGI using Perl!</b><br/>It's $now_string NOW!<br />";

 

5、综上,按照上面介绍的方法:
a. perl程序在Linux下运行正常。
b. apache2以cgi支持perl cgi程序
遇到错误可以查看,apache2的erro日志文件,位于 /var/log/apache2/error.log 进一步查询和排除错误。

c. 另一个方面,对于cgi的perl程序可以先写好,在命令行下perl cgi_test.pl查看是否程序本身有错。
如果没有错,就可以完全将问题定位成apache2对perl cgi程序的支持问题。

 

 

posted @ 2014-09-23 17:13  宁静的天空  阅读(889)  评论(0)    收藏  举报