说明:

操作系统:CentOS 6.x

web环境:php+nginx+mysql

nginx安装目录:/usr/local/nginx

nginx配置文件:/usr/local/nginx/conf/nginx.conf

nginx默认站点目录:/usr/local/nginx/html/

需求:让nginx能够解析.cgi后缀的文件

具体操作:

一、安装perl-fcgi依赖包,通过安装perl-fcgi来支持nginx运行.cgi

yum install perl-CPAN perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker

二、安装perl

cd /usr/local/src

wget http://www.cpan.org/src/5.0/perl-5.20.0.tar.gz #下载安装包,需要提前安装wget工具

tar -xzf perl-5.20.0.tar.gz #解压

cd perl-5.20.0 #进入目录

./Configure -des -Dprefix=/usr/local/perl #配置

make #编译

make install #安装

mv /usr/bin/perl /usr/bin/perl.bak #备份系统默认的perl

ln -s /usr/local/perl/bin/perl /usr/bin/perl #把刚刚安装好的新perl软连接到perl在系统中的默认位置

perl -v#查看perl版本

系统运维  www.osyunwei.com  温馨提醒:qihang01原创内容©版权所有,转载请注明出处及原文链

三、安装perl支持模块

1、安装perl fcgi模块

cd /usr/local/src

wget http://www.cpan.org/authors/id/F/FL/FLORA/FCGI-0.74.tar.gz

tar zxvf FCGI-0.74.tar.gz

cd FCGI-0.74

perl Makefile.PL #配置

make

make install

2、安装FCGI-ProcManager模块

cd /usr/local/src

wget http://www.cpan.org/authors/id/B/BO/BOBTFISH/FCGI-ProcManager-0.24.tar.gz

tar zxvf FCGI-ProcManager-0.24.tar.gz

cd FCGI-ProcManager-0.24

perl Makefile.PL

make

make install

3、安装IO模块

cd /usr/local/src

wget http://www.cpan.org/authors/id/G/GB/GBARR/IO-1.25.tar.gz

tar zxvf IO-1.25.tar.gz

cd IO-1.25

perl Makefile.PL

make

make install

4、安装IO::ALL模块

cd /usr/local/src

wget http://www.cpan.org/authors/id/I/IN/INGY/IO-All-0.79.tar.gz

tar zxvf IO-All-0.79.tar.gz

cd IO-All-0.79

perl Makefile.PL

make

make install

四、配置nginx支持.cgi

1、vi /usr/local/nginx/perl-fcgi.pl #编辑,添加以下代码

#!/usr/bin/perl

#

# author Daniel Dominik Rudnicki

# thanks to: Piotr Romanczuk

# email daniel@sardzent.org

# version 0.4.3

# webpage http://www.nginx.eu/

#

# BASED @ http://wiki.codemongers.com/NginxSimpleCGI

#

#

# use strict;

use FCGI;

use Getopt::Long;

use IO::All;

use Socket;

sub init {

GetOptions( "h" => \$help,

"verbose!"=>\$verbose,

"pid=s" => \$filepid,

"l=s" => \$logfile,

"S:s" => \$unixsocket,

"P:i" => \$unixport) or usage();

usage() if $help;

print " Starting Nginx-fcgi\n" if $verbose;

print " Running with $> UID" if $verbose;

print " Perl $]" if $verbose;

if ( $> == "0" ) {

print "\n\tERROR\tRunning as a root!\n";

print "\tSuggested not to do so !!!\n\n";

exit 1;

}

if ( ! $logfile ) {

print "\n\tERROR\t log file must declared\n"

. "\tuse $0 with option -l filename\n\n";

exit 1;

}

print " Using log file $logfile\n" if $verbose;

"\n\n" >> io($logfile);

addlog($logfile, "Starting Nginx-cfgi");

addlog($logfile, "Running with $> UID");

addlog($logfile, "Perl $]");

addlog($logfile, "Testing socket options");

if ( ($unixsocket && $unixport) || (!($unixsocket) && !($unixport)) ) {

print "\n\tERROR\tOnly one option can be used!\n";

print "\tSuggested (beacuse of speed) is usage UNIX socket -S \n\n";

exit 1;

}

if ($unixsocket) {

print " Daemon listening at UNIX socket $unixsocket\n" if $versbose;

addlog($logfile, "Deamon listening at UNIX socket $unixsocket");

} else {

print " Daemon listening at TCP/IP socket *:$unixport\n" if $verbose;

#

addlog($logfile, "Daemon listening at TCP/IP socket *:$unixport");

}

if ( -e $filepid ) {

print "\n\tERROR\t PID file $filepid already exists\n\n";

addlog($logfile, "Can not use PID file $filepid, already exists.");

exit 1;

}

if ( $unixsocket ) {

print " Creating UNIX socket\n" if $verbose;

$socket = FCGI::OpenSocket( $unixsocket, 10 );

if ( !$socket) {

print " Couldn't create socket\n";

addlog($logfile, "Couldn't create socket");

exit 1;

}

print " Using UNIX socket $unixsocket\n" if $verbose;

} else {

print " Creating TCP/IP socket\n" if $verbose;

$portnumber = ":".$unixport;

$socket = FCGI::OpenSocket( $unixport, 10 );

if ( !$socket ) {

print " Couldn't create socket\n";

addlog($logfile, "Couldn't create socket");

exit 1;

}

print " Using port $unixport\n" if $verbose;

}

addlog($logfile, "Socket created");

if ( ! $filepid ) {

print "\n\tERROR\t PID file must declared\n"

. "\tuse $0 with option -pid filename\n\n";

exit 1;

}

print " Using PID file $filepid\n" if $verbose;

addlog($logfile, "Using PID file $filepid");

my $pidnumber = $$;

$pidnumber > io($filepid);

print " PID number $$\n" if $verbose;

addlog($logfile, "PID number $pidnumber");

}

sub addzero {

my ($date) = shift;

if ($date < 10) {

return "0$date";

}

return $date;

}

sub logformat {

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$iddst) = localtime(time);

my $datestring;

$year += 1900;

$mon++;

$mon = addzero($mon);

$mday = addzero($mday);

$min = addzero($min);

$datestring = "$year-$mon-$mday $hour:$min";

return($datestring);

}

sub addlog {

my ($log_file, $log_message) = @_;

my $curr_time = logformat();

my $write_message = "[$curr_time] $log_message";

$write_message >> io($log_file);

"\n" >> io($log_file);

}

sub printerror {

my $message = @_;

print "\n Nginx FastCGI\tERROR\n"

. "\t $message\n\n";

exit 1;

}

sub usage {

print "\n Nginx FastCGI \n"

. "\n\tusage: $0 [-h] -S string -P int\n"

. "\n\t-h\t\t: this (help) message"

. "\n\t-S path\t\t: path for UNIX socket"

. "\n\t-P port\t\t: port number"

. "\n\t-p file\t\t: path for pid file"

. "\n\t-l file\t\t: path for logfile"

. "\n\n\texample: $0 -S /var/run/nginx-perl_cgi.sock -l /var/log/nginx/nginx-cfgi.log -pid /var/run/nginx-fcgi.pid\n\n";

exit 1;

}

init;

#

END() { } BEGIN() { }

*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=".shift()."\n"; }; eval q{exit};

if ($@) {

exit unless $@ =~ /^fakeexit/;

} ;

# fork part

my $pid = fork();

if( $pid == 0 ) {

&main;

exit 0;

}

print " Forking worker process with PID $pid\n" if $verbose;

addlog($logfile, "Forking worker process with PID $pid");

print " Update PID file $filepid\n" if $verbose;

addlog($logfile, "Update PID file $filepid");

$pid > io($filepid);

print " Worker process running.\n" if $verbose;

addlog ($logfile, "Parent process $$ is exiting");

exit 0;

sub main {

$request = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket );

if ($request) { request_loop()};

FCGI::CloseSocket( $socket );

}

sub request_loop {

while( $request->Accept() >= 0 ) {

# processing any STDIN input from WebServer (for CGI-POST actions)

$stdin_passthrough = '';

$req_len = 0 + $req_params{'CONTENT_LENGTH'};

if (($req_params{'REQUEST_METHOD'} eq 'POST') && ($req_len != 0) ){

while ($req_len) {

$stdin_passthrough .= getc(STDIN);

$req_len--;

}

}

# running the cgi app

if ( (-x $req_params{SCRIPT_FILENAME}) &&

(-s $req_params{SCRIPT_FILENAME}) &&

(-r $req_params{SCRIPT_FILENAME})

){

foreach $key ( keys %req_params){

$ENV{$key} = $req_params{$key};

}

if ( $verbose ) {

addlog($logfile, "running $req_params{SCRIPT_FILENAME}");

}

# http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens

#

open $cgi_app, '-|', $req_params{SCRIPT_FILENAME}, $stdin_passthrough or print("Content-type: text/plain\r\n\r\n"); print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n"; # addlog($logfile, "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !");

if ($cgi_app) {

print ;

close $cgi_app;

}

} else {

print("Content-type: text/plain\r\n\r\n");

print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";

addlog($logfile, "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.");

}

}

}

################################################

:wq! #保存退出

chmod 755 /usr/local/nginx/perl-fcgi.pl #添加脚本执行权限

备注:以上代码在拷贝的时候注意格式,否则出错。

2、vi /usr/local/nginx/start_perl_cgi.sh#编辑,添加以下代码,注意,这里nginx运行账户为www组的www用户

################################################

#!/bin/bash

#set -x

dir=/usr/local/nginx

stop ()

{

#pkill -f $dir/perl-fcgi.pl

kill $(cat $dir/logs/perl-fcgi.pid)

rm $dir/logs/perl-fcgi.pid 2>/dev/null

rm $dir/logs/perl-fcgi.sock 2>/dev/null

echo "stop perl-fcgi done"

}

start ()

{

rm $dir/now_start_perl_fcgi.sh 2>/dev/null

chown www.www $dir/logs

echo "$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock" >>$dir/now_start_perl_fcgi.sh

chown www.www $dir/now_start_perl_fcgi.sh

chmod u+x $dir/now_start_perl_fcgi.sh

sudo -u www $dir/now_start_perl_fcgi.sh

echo "start perl-fcgi done"

}

case $1 in

stop)

stop

;;

start)

start

;;

restart)

stop

start

;;

esac

################################################

:wq! #保存退出

chmod 755 /usr/local/nginx/start_perl_cgi.sh #添加脚本执行权限

/usr/local/nginx/start_perl_cgi.sh start #启动perl_cgi

cd /usr/local/nginx/logs #查看此目录下是否已生成perl-fcgi.sock文件

系统运维  www.osyunwei.com  温馨提醒:qihang01原创内容©版权所有,转载请注明出处及原文链

#如果没有生成此文件,请检查以上步骤,直到有这个文件,才能继续下面的操作。

3、添加perl_fcgi.conf文件

cd /usr/local/nginx/conf

vi perl_fcgi.conf #编辑,添加以下代码

location ~ .*\.(pl|cgi)?$

{

gzip off;

fastcgi_pass unix:/usr/local/nginx/logs/perl-fcgi.sock;

fastcgi_index index.cgi;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param QUERY_STRING $query_string;

fastcgi_param REQUEST_METHOD $request_method;

fastcgi_param CONTENT_TYPE $content_type;

fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

fastcgi_param REQUEST_URI $request_uri;

fastcgi_param DOCUMENT_URI $document_uri;

fastcgi_param DOCUMENT_ROOT $document_root;

fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REMOTE_ADDR $remote_addr;

fastcgi_param REMOTE_PORT $remote_port;

fastcgi_param SERVER_ADDR $server_addr;

fastcgi_param SERVER_PORT $server_port;

fastcgi_param SERVER_NAME $server_name;

fastcgi_read_timeout 60;

}

:wq! #保存退出

4、修改nginx配置文件/usr/local/nginx/conf/nginx.conf

cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf-bak #备份原文件

vi /usr/local/nginx/conf/nginx.conf #编辑修改,在server段添加include perlfcgi.conf;

server

{

listen 80;

#server_name localhost;

index index.php default.php index.html index.htm default.html default.htm ;

location ~ .*\.(php|php5)?$

{

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

include perl_fcgi.conf;

:wq! #保存退出

五、测试nginx支持.cgi

1、vi /usr/local/nginx/html/perlinfo.cgi #编辑,添加以下代码

########################################

#!/usr/bin/perl

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

#Location of Perl

$output = `whereis perl`;

@locations = split(" ",$output);

foreach $line (@locations)

{

$whereperl .= "$line
";

}

#Location of Sendmail

$output = `whereis sendmail`;

@locations = split(" ",$output);

foreach $line (@locations)

{

$wheresendmail .= "$line
";

}

#Location of Current Directory

$currentdirectory = `pwd`;

#Perl Variables

$perlversion = $];

#Perl Os

$perlos = $^O;

#Module Paths

foreach $line (@INC)

{

$modulepaths .= "$line
";

}

#Environment Variables

$environment = qq~

Environment Variables

~;

@allkeys = keys(%ENV);

foreach $key (@allkeys)

{

$value = $ENV{$key};

if ($value eq "") {$value = "-";}

$environment .= qq~

$key$value

~;

}

$environment .= qq~

~;

$documentroot = $ENV{'DOCUMENT_ROOT'};

if ($documentroot ne "")

{

@lines = `du -c -k $documentroot`;

$lastline = @lines-1;

($diskusage) = split/[\t| ]/,$lines[$lastline];

}

#Server Software

$serverip = $ENV{'SERVER_ADDR'};

$servername = $ENV{'SERVER_NAME'};

$serverport = $ENV{'SERVER_PORT'};

$serversoftware = $ENV{'SERVER_SOFTWARE'};

$serveruptime =`uptime`;

#Localtime

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

@months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

$date = sprintf("%02d-%s-%04d",$mday,$months[$mon],$year+1900);

$time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);

$localtime = "$date, $time";

#GMTtime

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);

@months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

$date = sprintf("%02d-%s-%04d",$mday,$months[$mon],$year+1900);

$time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);

$gmttime = "$date, $time";

print qq~

Perlonline.com - Perlinfo.cgi

Server Information
Name $servername
IP $serverip
Listing Port $serverport
Document Root $documentroot
Disk Usage by Root $diskusage Kb
Software's Installed $serversoftware
Perl Information
Perl version $perlversion
Compiled For $perlos
Module Paths $modulepaths
Location of Important Unix Programs
Perl $whereperl
Sendmail $wheresendmail
Time
Server Time (Local) $localtime
Server Time (GMT) $gmttime

$environment

All rights Reserved 2001. Perlonline.biz

~;

########################################

:wq! #保存退出

2、vi /usr/local/nginx/html/test.cgi#编辑,添加以下代码

#!/usr/bin/perl

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

print "

Hello, world.";

:wq!#保存退出

chown www.www -R /usr/local/nginx/html/

chmod 744 -R /usr/local/nginx/html/perlinfo.cgi

chmod 744 -R /usr/local/nginx/html/test.cgi

在浏览器中打开以上2个测试页面,如下图所示:

至此,Linux下配置nginx支持.cgi教程完成。

扩展阅读:

Linux下安装Bugzilla

一、下载Bugzilla

http://ftp.mozilla.org/pub/mozilla.org/webtools/archived/bugzilla-3.4.6.tar.gz

http://ftp.mozilla.org/pub/mozilla.org/webtools/bugzilla-4.4.5.tar.gz

解压Bugzilla到nginx站点根目录

二、创建数据库

mysql -u root -p

create database bugs; #创建数据库

insert into mysql.user(Host,User,Password) values('localhost','bugs',password('bugs')); #新建账户bugs,密码bugs

flush privileges; #刷新系统授权表

grant all on bugs.* to 'bugs'@'localhost' identified by 'bugs' with grant option;

flush privileges; #刷新系统授权表

三、安装Bugzilla

perl -MCPAN -e shell#设置参数,设置cpan镜像,cpan会让你设置参数 一直按回车 到最后输入一个cpan的镜像就好了,选择中科大的源,

进入到Bugzilla文件根目录

./checksetup.pl --check-modules #检查模块

/usr/bin/perl install-module.pl DateTime #安装模块

/usr/bin/perl install-module.pl List::MoreUtils

/usr/bin/perl install-module.pl DateTime::Locale

yum install mysql-devel #安装mysql数据库驱动,根据提示安装相应的模块,直到检查全部通过。

./checksetup.pl #根据提示安装输入管理员邮箱、账号密码等信息

提示:bugzilla-3.4.6需要再CentOS5.x下安装,CentOS6.x安装会出错。

linux cgi c环境配置,Linux下配置nginx支持.cgi | 系统运维相关推荐

  1. wsl2设置挂载_Windows下的Linux子系统安装,WSL 2下配置docker

    Windows下的Linux子系统安装,WSL 2下配置docker 前提条件: 安装WSL 2需要Windows 10版本是Build 18917或更高,首先先确认系统版本已升级. 在"启 ...

  2. linux php mysql 中文_Linux下PHP+MySQL+CoreSeek中文检索引擎配置 | 系统运维

    说明: 操作系统:CentOS 5.X 服务器IP地址:192.168.21.127 Web环境:Nginx+PHP+MySQL 站点根目录:/usr/local/nginx/html 目的:安装co ...

  3. linux下php远程连接mysql_Linux下PHP远程连接Oracle数据库 | 系统运维

    说明: Web服务器环境:CentOS 5.8 32位+Nginx 1.2.3+Mysql 5.5.27+php 5.3.16 Web服务器IP:192.168.21.149 php源码编译目录:/u ...

  4. memcached linux 配置文件,Linux下Memcached服务器部署 | 系统运维

    操作系统:CentOS 6.x 64位 实现目的:安装部署Memcached服务器 一.防火墙设置 vi /etc/sysconfig/iptables #编辑防火墙配置文件,添加下面代码 -A IN ...

  5. linux怎么用命令打开wine,Linux系统运维:10分钟教你如何使用Wine在Linux下玩魔兽世界...

    本文主要向大家介绍了Linux系统运维的如何使用Wine在Linux下玩魔兽世界,通过具体的步骤向大家展现,希望对大家学习Linux系统运维有所帮助. 目标:在 Linux 中运行魔兽世界 发行版:适 ...

  6. linux框架下搭建orl,DevOps和自动化运维实践/Linux\Unix技术丛书

    导语 内容提要 随着云计算.Docker.Kubernetes技术的流行,相信大家经常会听到"容器云"这个专业词汇,容器技术的兴起,对于传统的运维知识体系而言也是一种冲击和挑战.& ...

  7. linux忘记mysql密码_linux下忘记mysql root密码解决办法 | 系统运维

    引言:在linux系统中,如果忘记了MySQL的root密码,有没有办法重新设置新密码呢? 答案是肯定的,下面教大家一个比较简单的重置MySQL root密码的办法: 1.编辑MySQL配置文件my. ...

  8. 写给Linux系统运维的朋友

    本人是linux运维工程师,对这方面有点心得,现在我说说要掌握哪方面的工具吧.说到工具,在行外可以说是技能,在行内我们一般称为工具,就是运维必须要掌握的工具.我就大概列出这几方面,这样入门就基本没问题 ...

  9. linux系统运维面试题

    标签:linux系统运维面试题 1.     简述常用高可用技术 解答: Keepalived:Keepalived是一个保证集群高可用的服务软件,用来防止单点故障,使用VRRP协议实现.在maste ...

最新文章

  1. 【每日一题】剑指 Offer 10- I. 斐波那契数列
  2. 生物信息课程学习 --- 比对,BLAST,马尔可夫
  3. python3.7安装turtle步骤-Python3安装turtle问题
  4. Python 字符串操作方法大全
  5. 釜底抽薪:掌控能源成本,根治企业能源损耗
  6. 病毒周报(100111至100117)
  7. C和指针之函数之可变参数
  8. 工作68:json校验工具
  9. webpack 生成dist,打zip包
  10. 转)VCSA 6.5重启无法访问,报错“503 Service Unavailable”的解决方法
  11. 什么是重绘repaint?什么是回流reflow?
  12. js layui 弹出子窗体_layui 弹出界面弹框
  13. shell基础入门1.1shell特性
  14. 使用pm2管理项目(指令)
  15. drupal mysql hash密码_變更drupal7用戶密碼加密方式
  16. Linux学习笔记Day01-03 Xshell,Xfpt下载安装,使用
  17. TencentOS-Tiny之GCC
  18. python+vue 税务申报系统
  19. 假定系统四个进程,p1、p2、p3、p4三种资源r1、r2、r3数量分别为9、3、6在T0时刻资源分配为下表:
  20. 快速开始keras 教程

热门文章

  1. 2020 5 20,100天纪念日,用C语言打印3D动态立体爱心!!!
  2. CentOS内核版本升级至4.4
  3. Vulkan并非“灵药“
  4. android实现相机功能,Android开发实现拍照功能的方法实例解析
  5. 扫雷——如何实现点击“0”打开一大片“雷区”
  6. 在微信开放平台上创建移动应用
  7. django 设置 数据库缓存
  8. EDI是什么意思啊,用它有什么好处呢?
  9. https://docs.icons8.com/ 切片 钢笔 布尔计算
  10. (错误)SpringBoot 中使用HikariPool 报错