Nginx 本身不能执行外部程序,Nginx 处理 PHP 是通过 PHP 的 fastcgi 管理器(php-fpm)进行处理,然后 nginx 再将结果返回给用户;所以如果我们需要通过 cgi 程序(shell、perl、c/c++ 等)来编写网站后台的话,就需要使用 fcgiwrap 这个通用的 fastcgi 进程管理器来帮助 nginx 处理 cgi。

  • 对于 PHP:只建议使用 PHP-FPM,因为这是官方的解决方案,性能和稳定性肯定是最好的。

  • 对于其它 CGI 程序:如 Shell、Perl、C/C++、Python,推荐使用 fcgiwrap,这是一个通用的 FCGI 管理器。

写这篇博客的主要目的也是为了让 Nginx 执行 Shell、Perl、C/C++、Python 程序,因为作为一个生信出身的伪 IT 工作者,Shell、Perl、Python 永远都是我们最熟悉的,用这些语言来编写网站后台可以更加节省我们的时间,效率更高。

概念

1. CGI 与 FastCGI

CGI 全称是 "公共网关接口" (Common Gateway Interface),HTTP 服务器与你的或其它机器上的程序进行 "交谈" 的一种工具,其程序须运行在网络服务器上。它和 FastCGI(快速通用网关接口)都是语言无关的协议。CGI 诞生已经非常久远了,由于它每次在处理一个请求(连接)时都要重新启动脚本(可执行文件),重新传递所有的环境变量(其中非常多是完全一样的),导致性能非常低下。虽然性能较低,但功不可没,后来出现了性能更高的 FastCGI。

FastCGI(简称 FCGI)是 CGI 的增强版本,FCGI 可以简单的理解为 CGI + 多进程模型。FCGI 的工作模式有点类似于 Nginx,一个 Master 进程和多个 Worker 进程。Master 进程主要用来监控 Worker 进程的运行情况,当某个 Worker 进程意外退出时,Master 进程会随即启动一个新的 Worker 进程;Worker 进程则是真正干活的进程,用来执行 CGI 程序(传递环境变量、标准输入),获取 CGI 程序的标准输出,再将其返回为 Web 服务器(如 Apache、Nginx)。Worker 进程处理完请求后不会结束运行,而是继续等待下一个请求的到来,直到我们手动关闭它们。

2. Spawn-FCGI 与 FcgiWrap

Spawn-FCGI 是一个通用的 FastCGI 管理服务器,它是 lighttpd 中的一部份,很多人都用 Lighttpd 的 Spawn-FCGI 进行 FastCGI 模式下的管理工作。之前一直以为 Nginx 执行 CGI 程序需要 spawn-fcgi 和 fcgiwrap 两个东西(网上很多文档都是抄来抄去,搞得我也一头雾水,只好照做),但是实际上只需要 fcgiwrap,spawn-fcgi 的作用仅仅是启动和配置 fcgiwrap,这个工作完全可以由 fcgiwrap 自己来完成,所以 spawn-fcgi 不安装也不会影响 fcgiwrap 的使用。

Spawn-FCGI 目前已经独成为一个项目,更加稳定一些,也给很多 Web 站点的配置带来便利。已经有不少站点将它与 nginx 搭配来解决动态网页。目前 Spawn-FCGI 的下载地址是:

http://redmine.lighttpd.net/projects/spawn-fcgi。

FcgiWrap:Simple FastCGI wrapper for CGI scripts。首先这个东西的作用。它为那些不支持直接运行 CGI 脚本的 Web 服务器提供一种运行 CGI 脚本的方式。NGINX 就是一个只支持 FastCGI,不支持 CGI 的 HTTP(Web)服务器之一。也是我用得最多最熟悉的 Web 服务器。虽然 Apache 支持直接跑 CGI,但很少使用它的我对它并不感冒,这里也就不再讨论了。

安装与配置

2.1 安装 fcgiwrap

安装依赖:

yum -y install autoconf automake libtool fcgi fcgi-devel spawn-fcgi

安装 fcgiwrap:

$ git clone https://github.com/gnosek/fcgiwrap
$ cd fcgiwrap
$ autoreconf -i
$ ./configure     # fcgiwrap 默认安装到 /usr/local/sbin/fcgiwrap
$ make
$ make install

2.2 配置 spawn-fcgi

通过 yum install spawn-fcgi 方式安装的 spawn-fcgi 配置文件默认为:/etc/sysconfig/spawn-fcgi,编辑该文件:

vi /etc/sysconfig/spawn-fcgi

# You must set some working options before the "spawn-fcgi" service will work.
# If SOCKET points to a file, then this file is cleaned up by the init script.
#
# See spawn-fcgi(1) for all possible options.
#
# Example :
#SOCKET=/var/run/php-fcgi.sock
#OPTIONS="-u apache -g apache -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/php-cgi"
FCGI_SOCKET=/var/run/fcgiwrap.sock
FCGI_PROGRAM=/usr/local/sbin/fcgiwrap
FCGI_USER=nginx
FCGI_GROUP=nginx
FCGI_EXTRA_OPTIONS="-M 0777"
OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi.pid -- $FCGI_PROGRAM"

2.3 启动 spawn-fcgi 服务

[root@ecs-steven conf]# systemctl enable spawn-fcgi     # 添加开机启动(或者:chkconfig spawn-fcgi on)
spawn-fcgi.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig spawn-fcgi on

$ systemctl start spawn-fcgi    # 启动 spawn-fcgi 服务(或者:service spawn-fcgi start)

spawn-fcgi 启动出现下面报错:

[root@ecs-steven ~]# service spawn-fcgi start
Starting spawn-fcgi (via systemctl):  Job for spawn-fcgi.service failed because the control process exited with error code. See "systemctl status spawn-fcgi.service" and "journalctl -xe" for details.
                                                           [FAILED]

[root@ecs-steven ~]# systemctl status spawn-fcgi.service
● spawn-fcgi.service - LSB: Start and stop FastCGI processes
   Loaded: loaded (/etc/rc.d/init.d/spawn-fcgi; bad; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2019-04-26 08:31:51 CST; 9min ago
     Docs: man:systemd-sysv-generator(8)
  Process: 7069 ExecStart=/etc/rc.d/init.d/spawn-fcgi start (code=exited, status=1/FAILURE)

Apr 26 08:31:51 ecs-steven systemd[1]: Starting LSB: Start and stop FastCGI processes...
Apr 26 08:31:51 ecs-steven spawn-fcgi[7069]: Starting spawn-fcgi: spawn-fcgi: can't find user name nginx
Apr 26 08:31:51 ecs-steven spawn-fcgi[7069]: [FAILED]
Apr 26 08:31:51 ecs-steven systemd[1]: spawn-fcgi.service: control process exited, code=exited status=1
Apr 26 08:31:51 ecs-steven systemd[1]: Failed to start LSB: Start and stop FastCGI processes.
Apr 26 08:31:51 ecs-steven systemd[1]: Unit spawn-fcgi.service entered failed state.
Apr 26 08:31:51 ecs-steven systemd[1]: spawn-fcgi.service failed.

这种情况我们需要创建 nginx 用户,然后启动 spawn-fcgi.service:

[root@ecs-steven conf]# /usr/sbin/useradd nginx -s /bin/false

[root@ecs-steven conf]# service spawn-fcgi start
Starting spawn-fcgi (via systemctl):                       [  OK  ]

2.4 配置 Nginx

[root@ecs-steven conf]# vim nginx.conf

user  nginx nginx;
worker_processes  1;

error_log      logs/error.log;
pid                  logs/nginx.pid;

events {
worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile                    on;
    #tcp_nopush                 on;
    client_max_body_size  50m;

    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    include sites-available/*.conf;
}

在 Nginx 目录下添加 sites-available/fcgi.conf 文件:

[root@ecs-steven conf]# cat sites-available/fcgi.conf
server {
    listen 80;

    access_log  logs/fcgi_access.log;
    error_log   logs/fcgi_error.log debug;

    # 开启 SSL 配置
    #listen  443;
    #ssl on;
    #server_name tools.shenweiyan.com;
    #ssl_certificate   ../cert/tools/tools.shenweiyan.com.pem;
    #ssl_certificate_key  ../cert/tools/tools.shenweiyan.com.key;
    #ssl_session_timeout 5m;
    #ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    #ssl_prefer_server_ciphers on;

    root /apps/tools;

    location / {
        index  index.html index.htm;
    }

   location ~ .*\.(pl|py|cgi)?$ {
        include        /usr/local/software/nginx/conf/fastcgi_params;
        fastcgi_pass   unix:/var/run/fcgiwrap.sock;
        fastcgi_index  index.cgi;
        fastcgi_param  SCRIPT_FILENAME /apps/tools/$fastcgi_script_name;
    }
}

最后,重启 Nginx:

$ service nginx reload 

或者:
$ systemctl restart nginx

添加 CGI 程序

编写 CGI 测试程序:

$ vim /apps/tools/test.cgi

#!/usr/bin/perl -w
print "Content-type: text/html\n\n";    
print "<html><head><title>Hello World!</title></head>\n";
print "<body><h1>Hello world, CGI work!</h1>
</body></html>\n";

设定权限:

$chmod 0755 /apps/tools/test.cgi
$ chown nginx.nginx /apps/tools/test.cgi

最后,利用 firefox/chrome 测试!http://192.168.xxx.xxx/test.cgi 访问出现 "Hello world, CGI work!" 即说明配置部署成功。

——The  End——

本文分享自微信公众号 - 生信科技爱好者(bioitee)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

CentOS Linux 7 配置 nginx 支持 CGI的更多相关文章

  1. CentOS 6.7 配置nginx支持SSL/https访问

    一.安装必要的包 yum install openssl openssl-devel 二.配置编译参数,增加对SSL的支持 ./configure –with-http_ssl_module 三.修改 ...

  2. Linux下安装php环境并且配置Nginx支持php-fpm模块[www]

    Linux下安装php环境并且配置Nginx支持php-fpm模块 http://www.cnblogs.com/freeweb/p/5425554.html 5分钟搭建 nginx +php --- ...

  3. centos下配置nginx支持php

    添加nginx 默认主页index.php vim .../etc/nginx/conf.d/default.conf location / { root   /usr/share/nginx/htm ...

  4. Linux - 配置php-fpm 以及 配置nginx支持php

    配置php-fpm [root@localhost php7]# which php-fpm /usr/local/php7/sbin/php-fpm [root@localhost php7]# p ...

  5. LNMP搭建04 -- 配置Nginx支持PHP

    首先建立存放网页文件的目录,执行 mkdri /usr/local/server/www  然后进入到该目录中 cd /usr/local/server/www 然后创建一个测试文件: phpinfo ...

  6. Ubuntu下安装LNMP之php7的安装并配置Nginx支持php及卸载php

    据了解,php7是比之前的版本性能快很多的.http://php.net/get/php-7.2.2.tar.gz/from/a/mirror 安装前也可提前将相关依赖库安装好,或者在安装php时若安 ...

  7. 在CentOS/Windows下配置Nginx(以及踩坑)

    在CentOS/Windows下配置Nginx(以及踩坑) 1. 序言 因为这类文章网上比较多,实际操作起来也大同小异,所以我并不会着重于详细配置方面,而是将我配置时踩的坑写出来. 2. CentOS ...

  8. CentOS 7 下配置 Nginx + PHP7.1 + MariaDB 以及 Laravel 框架 2018.3.11

    CentOS 7 下配置 Nginx + PHP7.1 + MariaDB 以及 Laravel 框架 阿里云服务器的选择 当然是选择学生优惠啦.这里阿里云还提供了轻量级服务器这个选项,可以预装 LA ...

  9. CentOS 7 下配置 Nginx + PHP7.1 + MariaDB 以及 Laravel 框架

    <!doctype html> CentOS 7 下配置 Nginx + PHP7.1 + MariaDB 以及 Laravel 框架.mdhtml {overflow-x: initia ...

  10. 配置nginx支持Https

    配置nginx支持Https一定要先使用stop停止nginx然后重新代开不能使用reload 一:安装letsencrypt 1.从git上克隆下来git clone https://github. ...

随机推荐

  1. InnoDB 索引深入剖析

    InnoDB页 将数据划分为若干个页(page),以页作为磁盘和内存之间交互的基本单位,InnoDB中页的大小一般为 16KB.也就是在一般情况下,一次最少从磁盘中读取16KB的内容到内存中,一次最少 ...

  2. 微软 New Bing AI 申请与使用保姆级教程(免魔法)

    本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 [彭旭锐] 提问. 大家好,我是小彭. 最近的 AI 技术实在火爆,从 OpenAI 的 ChatGPT,到微软的 New Bi ...

  3. 官方文档 | 【JVM调优体系】「GC底层调优实战」XPocket为终结性能问题而生—开发指南

    XPocket 用户文档 XPocket 是PerfMa为终结性能问题而生的开源的插件容器,它是性能领域的乐高,将定位或者解决各种性能问题的常见的Linux命令,JDK工具,知名性能工具等适配成各种X ...

  4. Mybatis的整体理解

    I有关于我的对ybatis的设想: 简单总结-下有关于我对wybat is的架构理解: 总体分为三个层面: 1.对外接口API 2.MapStatement数据处理 3.执行及其数据存储 两个主要的对 ...

  5. day64:Linux:nginx模块之限制连接&状态监控&Location/用nginx+php跑项目/扩展应用节点

    目录 1.nginx模块:限制连接 limit_conn 2.nginx模块:状态监控 stub_status 3.nginx模块:Location 4.用nginx+php跑wordpress项目 ...

  6. Java与Mysql锁相关知识总结

    锁的定义 在计算机程序中锁用于独占资源,获取到锁才可以操作对应的资源. 锁的实现 锁在计算机底层的实现,依赖于CPU提供的CAS指令(compare and swsp),对于一个内存地址,会比较原值以 ...

  7. 《爆肝整理》保姆级系列教程-玩转Charles抓包神器教程(14)-Charles过滤网络请求

    1.简介 在日常工作测试中,经常要抓包看请求的request,response是不是传的对,返回的字段值对不对,众多的请求中看得眼花缭乱,如何找到自己想要的请求,那么我们就需要过滤请求.Charles ...

  8. Django笔记二十五之数据库函数之日期函数

    本文首发于公众号:Hunter后端 原文链接:Django笔记二十五之数据库函数之日期函数 日期函数主要介绍两个大类,Extract() 和 Trunc() Extract() 函数作用是提取日期,比 ...

  9. 《爆肝整理》保姆级系列教程-玩转Charles抓包神器教程(15)-Charles如何配置反向代理

    1.简介 在App开发的过程当中,抓包是一个很常见的需求,而有些app的请求不会在网络设置代理时被抓到数据包,这里若是需要抓包就需要搭建反向代理. 2.什么是代理? 什么是代理,来一张图了解一下. 代 ...

  10. vue 项目全局修改element-ui的样式/主题颜色

    一,安装sass 1 npm i sass sass-loader --save 二,安装element主题生成工具 // 全局安装npm i element-theme --save // 安装主体 ...