在搭建 LAMP/LNMP 服务器时,会经常遇到 PHP-FPM、FastCGI和CGI 这几个概念。

1.CGI

CGI(Common Gateway Interface)全称是“通用网关接口”,WEB 服务器与PHP应用进行“交谈”的一种工具,其程序须运行在网络服务器上。CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。如php、perl、tcl等。

WEB服务器会传哪些数据给PHP解析器呢?URL、查询字符串、POST数据、HTTP header都会有。所以,CGI就是规定要传哪些数据,以什么样的格式传递给后方处理这个请求的协议。仔细想想,你在PHP代码中使用的用户从哪里来的。

也就是说,CGI就是专门用来和 web 服务器打交道的。web服务器收到用户请求,就会把请求提交给cgi程序(如php-cgi),cgi程序根据请求提交的参数作应处理(解析php),然后输出标准的html语句,返回给web服服务器,WEB服务器再返回给客户端,这就是普通cgi的工作原理。

但是CGI有个蛋疼的地方,就是每一次web请求都会有启动和退出过程,也就是最为人诟病的fork-and-execute模式,这样一在大规模并发下,就死翘翘了。

CGI工作流程

ssl

2.FastCGI

从根本上来说,FastCGI是用来提高CGI程序性能的。类似于CGI,FastCGI也可以说是一种协议。fastCGI基于CGI协议之上做了一些性能优化。

FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一次。它还支持分布式的运算, 即 FastCGI 程序可以在网站服务器以外的主机上执行,并且接受来自其它网站服务器来的请求。

FastCGI是语言无关的、可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程保持在内存中,并因此获得较高的性能。众所周知,CGI解释器的反复加载是CGI性能低下的主要原因,如果CGI解释器保持在内存中,并接受FastCGI进程管理器调度,则可以提供良好的性能、伸缩性、Fail- Over特性等等

LNMP架构(linux Nginx Mysql PHP)

ssl

3.FastCGI模块

下面的这些配置项都是属于fastCGI模块传送门

fastcgi_pass

设置FastCGI服务器的地址,地址可以指定为域名或IP地址,以及端口:

Syntax:	fastcgi_pass address;
Default: 
Context: location, if in location

fastcgi_index

设置默认首页,结合fastcgi_param一起设置

Syntax:	fastcgi_index name;
Default: 
Context: http, server, location

fastcgi_param

通过fastcgi_param设置变量,并将设置的变量传递到后端的FastCGI server

Syntax:	fastcgi_param parameter value [if_not_empty];
Default: 
Context: http, server, location

4.基础环境安装

由于演示的环境是LNMP,需要安装php,mysql

mysql搭建

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。

yum install mariadb -y

yum -y install mariadb-server

systemctl start mariadb.service  #启用数据库

ss -luntp | grep 3306  #查看mysql是否启动起来

mysql -uroot -p    #登录mysql

php安装

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

yum install yum-utils

yum-config-manager --enable remi-php72

yum search php72

yum install php72-php-fpm php72-php-common php72-php-mbstring php72-php-xmlrpc php72-php-soap php72-php-gd php72-php-xml php72-php-intl php72-php-mysql php72-php-cli php72-php-zip php72-php-curl

检测php安装成功

php72 -version

启动fastcgi就是通过读取 vim /etc/opt/remi/php72/php-fpm.conf

vim /etc/opt/remi/php72/php-fpm.d/www.conf查看listen的端口listen = 127.0.0.1:9000表示监听的端口9000

启动服务

systemctl status php72-php-fpm.service

systemctl stop php72-php-fpm.service

systemctl start php72-php-fpm.service

检测fastcgi是否启动成功

ss -luntp 或 netstat -luntp | grep php #查看9000端口是否在监听

ps -ef | grep php  #查看php-fpm进程是否启动

5.nginx配置

服务目录

/opt/app/code5
|--info.php
|--index.html

/etc/nginx/conf.d
|-lnmp.conf

info.php

<?php

  phpinfo();

?>

index.html

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<h1>瓦力博客首页</h1>
</body>
</html>

lnmp.conf

server {
    listen       80; 
    server_name  localhost;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;

    location / { 
        root   /opt/app/code5;
        index  index.html;
    }   

    location ~ \.php$ {
        root /opt/app/code5;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }   
	
}

检测语法并重启

nginx -tc /etc/nginx/nginx.conf

nginx -s reload -c /etc/nginx/nginx.conf

ssl

ssl

nginx教程

nginx环境搭建(1) nginx基础知识(2) nginx的安装 卸载(3) nginx的基本参数使用(4) nginx分析默认配置(5) nginx 虚拟主机配置(6) nginx 日志(7) nginx 模块(8) nginx 访问控制(9) nginx 静态资源web服务(10) nginx 缓存(11) nginx 跨域访问(12) nginx 防盗链(13) nginx 正向,反向代理配置(14) nginx 代理缓存配置(15) nginx websocket(16) nginx fastcgi(17) nginx 搭建wordPress博客(18) nginx Fastcgi缓存配置(19) nginx uwsgi反向代理(20) nginx 负载均衡(21) [深] nginx 动静分离(22) [深] nginx rewrite规则(23) [深] nginx 平滑升级 添加模块 调试(24) [深] nginx secure_link_module模块(25) [深] nginx geoip_module模块(26) [深] nginx https(27) [深] nginx与lua的开发(28) [架] nginx常见问题(29) [架] nginx性能优化(30) [架] nginx 安全(31) [架] nginx 反向代理gRpc(32)