为了账号安全,请及时绑定邮箱和手机立即绑定

nginx fastcgi(17)

标签:
Nginx

获取全套nginx教程,请访问瓦力博客

在搭建 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模块传送门{:target="_blank"}

fastcgi_pass

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

Syntax:	fastcgi_pass address;
Default: —
Context: location, if in location
  • fastcgi_pass localhost:9000
  • fastcgi_pass unix:/tmp/fastcgi.socket (nginx 和 php在同一台机器,通过socket)

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

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消