Linux系统下Nginx+Perl环境搭建

1、 下载所需程序

wget http://www.cpan.org/modules/by-module/FCGI/FCGI-0.78.tar.gz
wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz
wget http://www.cpan.org/authors/id/I/IN/INGY/IO-All-0.86.tar.gz
#nginx-fcgi.txt 百度找资源下载
wget http://www.nginx.eu/nginx-fcgi/nginx-fcgi.txt
cp nginx-fcgi.txt /usr/local/nginx/nginx-fcgi.pl

2、 安装相关模块

(1) 安装FCGI模块

tar xvf FCGI-0.78.tar.gz
cd FCGI-0.78
perl Makefile.PL
make
make install

(2) 安装IO模块

tar xvf IO-1.25.tar.gz
cd IO-1.25
perl Makefile.PL
make
make install

(3) 安装IO-ALL模块

tar xvf IO-All-0.86.tar.gz
cd IO-All-0.86
perl Makefile.PL
make
make install

3、 编写nginx-fcgi启动脚本

[root@10 nginx]# cat nginx-fcgi
#!/bin/bash
nginxroot=/usr/local/nginx

start ()
{
chown nobody.root $nginxroot/logs
echo "$nginxroot/nginx-fcgi.pl -l $nginxroot/logs/nginx-fcgi.log -pid 
$nginxroot/logs/nginx-fcgi.pid -S $nginxroot/logs/nginx-fcgi.sock" > $nginxroot/nginx-fcgi.sh
chown nobody.nobody $nginxroot/nginx-fcgi.sh
chmod 755 $nginxroot/nginx-fcgi.sh
sudo -u nobody $nginxroot/nginx-fcgi.sh
echo "start nginx-fcgi done"
}

stop ()
{
kill $(cat $nginxroot/logs/nginx-fcgi.pid)
#rm $nginxroot/logs/nginx-fcgi.pid 2 >/dev/null
#rm $nginxroot/logs/nginx-fcgi.sock 2 >/dev/null
#rm $ngnixroot/nginx-fcgi.sh 2 >/dev/null
rm -f $nginxroot/logs/nginx-fcgi.pid
rm -f $nginxroot/logs/nginx-fcgi.sock
rm -f $ngnixroot/nginx-fcgi.sh
}

case $1 in
stop)
stop
;;

start)
start
;;

restart)
stop
start
;;

*)
echo $"Usage:perl-cgi {start|stop|restart}"
exit 1
esac
[root@10 nginx]#chmod 755 nginx-fcgi
[root@10 nginx]#chmod 755 nginx-fcgi.pl
[root@10 nginx]#/usr/local/nginx/nginx-fcgi start | stop | restart

4、 为Nginx添加FCGI支持

location ~ \.cgi$ {
         root /var/www;
         fastcgi_pass unix:/usr/local/nginx/logs/nginx-fcgi.sock;
         fastcgi_index index.cgi;
         fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
         include fastcgi_params;
}

5、 测试Nginx+Perl(FCGI)

在web根目录下新建一个123.cgi文件。文件内容如下:

[root@10 www]vim 123.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, world. ";

[root@10 www]# chmod +x 123.cgi

在浏览器中访问该页面,看是否能正常访问

http://IP/123.cgi

发表回复

Your email address will not be published.

名字 *
电子邮件 *
站点