美文网首页Nginx
12. Nginx实现FastCGI

12. Nginx实现FastCGI

作者: 随便写写咯 | 来源:发表于2021-03-21 03:31 被阅读0次

    6.3 实现FastCGI

    图片.png
    nginx只支持基于FastCGI协议, 与后端的php服务器相连, 不支持模块形式
    客户端与nginx通过http/https协议通信, nginx通过FastCGI与php-fpm通信, php-fpm与MySQL通过MySQL协议通信
    属于异构代理
    

    6.3.1 FastCGI案例: Nginx与php-fpm部署在同一服务器, 部署WordPress

    图片.png

    配置php-fpm

      1. 安装php-fpm
    [01:26:30 root@nginx ~]#dnf -y install php-fpm
    
      1. 修改www.conf文件, 指定php-fpm以nginx账号运行
    [01:26:54 root@nginx ~]#vim /etc/php-fpm.d/www.conf 
    user = nginx
    group = nginx
    
      1. php-fpm监听端口
    # 因为是和nginx部署在同一个服务器, 所以监听socket即可
    listen = /run/php-fpm/www.sock
    
      1. 修改socket文件的权限
    listen.owner = nginx
    listen.group = nginx                                                                                                                                                                
    listen.mode = 0660
    
      1. 开启php的状态页面和ping探测
    pm.status_path = /status
    ping.path = /ping
    
    # 利用sed命令修改
    sed -ri.bak -e 's#^user = apache#user = nginx#' -e 's#^group = apache#group = nginx#' -e 's#^;listen.owner = nobody#listen.owner = nginx#' -e 's#^;listen.group = nobody#listen.group = nginx#' -e 's#^;listen.mode = 0660#listen.mode = 0660#' -e 's#^;(pm.status_path = /status)#\1#' -e 's#^;(ping.path = /ping)#\1#' /etc/php-fpm.d/www.conf
    
      1. 启动php-fpm, 验证配置
    [01:35:20 root@nginx ~]#systemctl enable --now php-fpm
    Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
    
    [01:35:38 root@nginx ~]#ll /run/php-fpm/www.sock 
    srw-rw----+ 1 root root 0 Mar 21 01:35 /run/php-fpm/www.sock
    

    创建存放php文件的目录

      1. 创建目录
    [01:35:51 root@nginx ~]#mkdir /data/php
    
      1. 创建php信息页面
    [01:37:43 root@nginx ~]#vim /data/php/info.php
    
    <?php
        phpinfo();                                                                                                                                                                      
    ?>
    
    

    yum安装nginx

    [00:17:50 root@nginx-php ~]#yum -y install nginx
    [00:17:56 root@nginx-php ~]#rm -rf /etc/nginx/conf.d/php-fpm.conf # yum安装的nginx, 主配置文件包含了conf.d的include指令
    [00:21:02 root@nginx-php ~]#rm -f  /etc/nginx/default.d/php.conf # yum安装的nginx, 主配置文件包含了default.d的include指令
    # 删除两个配置文件, 这两个文件会由php-fpm包提供, 文件定义了upstream php-fpm模板的FastCGI配置文件, 会影响之后单独为wordpress创建子配置文件
    

    配置nginx, 将php请求转发到本地的php-fpm

    [00:18:04 root@nginx-php ~]#vim /etc/nginx/nginx.conf
    include /etc/nginx/conf.d/*.conf;  # yum安装的nginx, 在http语句块自带include指令
    
    [00:20:07 root@nginx-php ~]#mkdir -pv /etc/nginx/conf.d
    
    
    # 这里建立一个专门的虚拟主机, wordpress.wang.org做实验
    # 以下两种fastcgi_param的配置方法都可以
    
    server {                                                                                                                                                                            
        listen 80;
        server_name wordpress.wang.org;
        index index.php index.html index.htm;  
        root /data/php;
    
        location ~ \.php$ {                                                                                                                                                        
        root           /data/php;
        fastcgi_pass   unix:/run/php-fpm/www.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        }
    
    }
    server {
        listen 80;
        server_name wordpress.wang.org;
        index index.php index.html index.htm;
        root /data/php;
    
        location ~ \.php$ {
        root           /data/php;
        fastcgi_pass   unix:/run/php-fpm/www.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /data/php$fastcgi_script_name;
        include        fastcgi_params;
        }
    
    }
    
    
    [01:48:11 root@nginx /apps/nginx/conf.d]#nginx -t
    nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
    [01:48:12 root@nginx /apps/nginx/conf.d]#systemctl restart nginx
    

    客户端测试

    添加DNS解析
    10.0.0.86 wordpress.wang.org
    
    [01:52:23 root@client ~]#curl -I wordpress.wang.org/info.php
    HTTP/1.1 200 OK
    Server: nginx/1.18.0
    Date: Sat, 20 Mar 2021 17:52:32 GMT
    Content-Type: text/html; charset=UTF-8
    Connection: keep-alive
    X-Powered-By: PHP/7.2.24
    
    
    图片.png

    10.0.0.85- 安装MySQL

    [00:38:09 root@mysql ~]#yum -y install mysql-server
    
    # 创建wordpress账号, 并授权
    [01:56:23 root@mysql ~]#mysql -e 'create database wordpress'
    [01:55:17 root@mysql ~]#mysql -e 'create user wordpress@"10.0.0.%" identified by "wordpress"'
    [01:55:57 root@mysql ~]#mysql -e 'grant all on wordpress.* to wordpress@"10.0.0.%"'
    

    部署wordpress

    [02:02:27 root@nginx ~]#tar xf wordpress-5.4.2-zh_CN.tar.gz 
    [02:07:40 root@nginx ~]#mv wordpress /data/php
    [02:07:54 root@nginx ~]#ls /data/php
    info.php  wordpress
    
    [02:08:01 root@nginx ~]#yum -y install php-mysqlnd php-json
    
    # 将nginx配置文件中, 虚拟主机的root, 修改为/data/php, 否则只有对于.php页面的请求会被转发到/data/php下
    # 而其他静态资源, 因为没有定义location, 因此会去默认站点定义的家目录查找, 结果就是静态资源不会显示
    # 或者把wordpress整个目录, 拷贝一份到默认站点家目录也行
    server {
        listen 80;
        server_name wordpress.wang.org;
        index index.php index.html index.htm;
        root /data/php;    # 定义root为/data/php, 这样就需要访问wordpress.wang.org/wordpress才会访问到wordpress主页面.     
                                                                                                                                                    
        location ~ \.php$ {
        root           /data/php;
        fastcgi_pass   unix:/run/php-fpm/www.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        }
    
    }
    
    
    # 另一个方法是定义两个location
    
    [23:09:45 root@wordpress ~]#vim /etc/nginx/conf.d/wordpress.conf 
    
    server {
        listen 80;
        server_name wordpress.wang.org;
        index index.php index.html index.htm;
    
        location ~ \.php$ {                                                                  
        root           /data/php;
        fastcgi_pass   unix:/run/php-fpm/www.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        }
    
        location / {
        root /data/php;
        index index.php index.html index.htm;
       }
    
    }
    # 也可以把root都定义为/data/php/wordpress, 这样只要访问wordpress.php.org就可以访问到主页面了
    
    [02:08:46 root@nginx ~]#systemctl restart php-fpm
    [02:08:46 root@nginx ~]#systemctl restart nginx
    
    # 准备wordpress配置文件, 配置数据库信息
    
    [00:31:42 root@php-fpm ~]#mv /data/php/wordpress/wp-config-sample.php  /data/php/wordpress/wp-config.php 
    
    [00:31:42 root@php-fpm ~]#vim /data/php/wordpress/wp-config.php
    
    define( 'DB_NAME', 'wordpress' );
      
    /** MySQL数据库用户名 */
    define( 'DB_USER', 'wordpress' );
    
    /** MySQL数据库密码 */
    define( 'DB_PASSWORD', 'wordpress' );
    
    /** MySQL主机 */
    define( 'DB_HOST', '10.0.0.85' );
    
    
    
    [00:39:14 root@nginx-php ~]#chown -R nginx.nginx /data/php/wordpress # 修改wordpress目录权限为nginx
    
    图片.png

    配置状态页面, 和ping检查

        location ~ ^/(ping|status)$ {                                                                                                                                                
        
            include fastcgi_params;
            fastcgi_pass unix:/run/php-fpm/www.sock;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
        }
    
    nginx -s reload
    
    图片.png 图片.png

    6.3.2 FastCGI案例: Nginx与php-fpm部署在不同服务器

    图片.png

    静态资源由nginx响应, 动态php程序通过FastCGI转给php-fpm服务器, 因此, 在nginx和fpm上, 都要有wordpress文件

    10.0.0.84-安装,配置php-fpm

    1. 安装php-fpm
    [02:30:42 root@CentOS-8-4 ~]#hostname php-fpm
    [02:33:38 root@CentOS-8-4 ~]#yum -y install php-fpm 
    [02:48:34 root@php-fpm ~]#yum -y install php-mysqlnd php-json
    
    1. 配置fpm
    user = nginx 
    group = nginx
    listen = 9000  
    listen.owner = nginx
    listen.group = nginx                                                                                                                                                                
    listen.mode = 0660
    ;listen.allowed_clients = 127.0.0.1   # 注释掉这行, 允许所有主机访问
    pm.status_path = /status 
    ping.path = /ping
    
    [23:56:58 root@php-fpm ~]#sed -ri.bak -e 's#^user = apache#user = nginx#' -e 's#^group = apache#group = nginx#' -e 's#^listen = /run/php-fpm/www.sock#listen = 9000#' -e 's#^;listen.owner = nobody#listen.owner = nginx#' -e 's#^;listen.group = nobody#listen.group = nginx#' -e 's#^;listen.mode = 0660#listen.mode = 0660#' -e 's#^(listen.allowed_clients = 127.0.0.1)#;\1#' -e 's#^;(pm.status_path = /status)#\1#' -e 's#^;(ping.path = /ping)#\1#' /etc/php-fpm.d/www.conf 
    
    
    1. 创建nginx账号
    [02:38:34 root@php-fpm ~]#useradd -r nginx -s /sbin/nologin
    
    1. 启动php-fpm
    [02:38:55 root@php-fpm ~]#systemctl enable --now php-fpm
    Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
    
    
    # 确保监听9000端口
    [02:39:41 root@php-fpm ~]#ss -ntl
    State                   Recv-Q                  Send-Q                                    Local Address:Port                                     Peer Address:Port                  
    LISTEN                  0                       128                                             0.0.0.0:22                                            0.0.0.0:*                     
    LISTEN                  0                       128                                                   *:9000                                                *:*                     
    LISTEN                  0                       128                                                [::]:22                                               [::]:*    
    

    修改nginx配置

    [00:03:58 root@nginx ~]#vim /etc/nginx/conf.d/wordpress.conf
    
    server {                                                                                                                                                                            
    
        listen 80;
        server_name wordpress.wang.org;
    
    
        location / {
    
            root /data/php; # 静态资源, 由nginx本地予以响应, 因此, 在nginx本地的root目录下, 也要有wordpress文件
            index index.php index.html index.htm;
        }
    
        location ~ \.php$ {
    
            root /data/php;
            index index.php index.html index.htm;
            fastcgi_pass 10.0.0.88:9000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_index index.php;
    
        }
        location ~ ^/(ping|status)$ {                                                                                                                                                
     
            include fastcgi_params;
            fastcgi_pass 10.0.0.88:9000;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
        }
    
    
    }
    
    
    
    [02:41:44 root@nginx ~]#systemctl restart nginx
    

    准备wordpress文件和info.php

    # 准备info.php
    [00:06:23 root@php-fpm ~]#mkdir -pv /data/php
    mkdir: created directory '/data/php'
    [00:06:30 root@php-fpm ~]#cd /data/php
    [00:06:33 root@php-fpm /data/php]#vim info.php
    <?php
        phpinfo();                                                                                                                                                                      
    ?>
    
    
    # 准备wordpress
    [00:22:55 root@php-fpm /data/php]#tar xvf wordpress-5.7.2.tar.gz
    [00:23:08 root@php-fpm /data/php]#ll
    total 15392
    -rw-r--r-- 1 root   root         24 Jun  5 00:12 info.php
    drwxr-xr-x 5 nobody nobody     4096 May 13 07:49 wordpress
    -rw-r--r-- 1 root   root   15750424 Jun  1 18:24 wordpress-5.7.2.tar.gz
    
    [00:23:10 root@php-fpm /data/php]#mv /data/php/wordpress/wp-config-sample.php  /data/php/wordpress/wp-config.php 
    [00:23:41 root@php-fpm /data/php]#vim /data/php/wordpress/wp-config.php
    
    /** The name of the database for WordPress */
    define( 'DB_NAME', 'wordpress' );
    
    /** MySQL database username */
    define( 'DB_USER', 'wordpress' );
    
    /** MySQL database password */
    define( 'DB_PASSWORD', 'wordpress' );
    
    /** MySQL hostname */
    define( 'DB_HOST', '10.0.0.84' );
    
    # nginx上创建/data/php目录
    [00:26:39 root@nginx ~]#mkdir /data/php
    
    # 将wordpress目录从php-fpm拷贝到nginx
    [00:28:33 root@php-fpm /data/php]#scp -r wordpress/ 10.0.0.87:/data/php
    
    

    部署MySQL

    # 10.0.0.84
    [00:19:06 root@mysql ~]#yum -y install mysql-server; systemctl enable --now mysqld
    mysql> create database wordpress;
    mysql> create user wordpress@"10.0.0.%" identified by "wordpress";
    mysql> grant all on wordpress.* to wordpress@"10.0.0.%";
    

    测试状态页和ping检测

    图片.png 图片.png

    测试wordpress访问

    图片.png

    6.3.3 实现动静分离

    动静分离: 即把静态和动态资源的请求, 通过nginx, 转发到后端不同的服务器上
    通过用户访问的URL路径, 进行动静判断, 使用location进行调度
    实现动静分离还需要看开发的代码. 因为需要把静态资源部署到静态服务器, 动态资源部署到动态服务器. 最简单的办法就是在静态和动态服务器上都拷贝相同的代码
    
    • 动静分离场景
    图片.png
    案例: 将客户端对php以外的资源的请求转发到后端服务器10.0.0.83上
    
    图片.png

    6.3.3.1 配置nginx实现反向代理的动静分离

    [00:42:43 root@nginx ~]#yum -y install nginx; systemctl enable --now nginx
    
    • 子配置文件定义location
    [00:54:49 root@nginx ~]#vim /etc/nginx/conf.d/test.conf
    
    server {
        listen 80;
        server_name test.wang.org;
    
        location ~ \.php$ {
    
        root           /data/php; # 在php服务器上创建php文件家目录
    
        index index.php index.html index.htm;
        fastcgi_pass   10.0.0.88:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        }
    
        location  / {
            proxy_pass http://10.0.0.85;
            root /var/www/html;
            index index.html;
        }
    
        location ~ ^/(ping|status)$ {
    
            include fastcgi_params;
            fastcgi_pass 10.0.0.88:9000;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
    
        }
    }   
    
    [00:55:05 root@nginx ~]#systemctl restart nginx
    [00:55:13 root@nginx ~]#ss -ntl
    State                   Recv-Q                  Send-Q                                    Local Address:Port                                     Peer Address:Port                  
    LISTEN                  0                       128                                             0.0.0.0:22                                            0.0.0.0:*                     
    LISTEN                  0                       128                                             0.0.0.0:80                                            0.0.0.0:*                     
    LISTEN                  0                       128                                                [::]:22                                               [::]:*                     
    LISTEN                  0                       128                                                [::]:80                                               [::]:* 
    

    6.3.3.2 准备apache服务器

    # 10.0.0.83
    
    [03:17:09 root@apache ~]#yum -y install httpd; echo 'webpage in 10.0.0.85' > /var/www/html/index.html; systemctl enable --now httpd
    
    
    [03:17:07 root@apache ~]#curl 10.0.0.83
    webpage in 10.0.0.83
    
    

    6.3.3.3 准备php服务器

    [00:42:49 root@php-fpm ~]#yum -y install php-fpm
    
    [01:01:23 root@php-fpm ~]#sed -ri.bak -e 's#^user = apache#user = nginx#' -e 's#^group = apache#group = nginx#' -e 's#^listen = /run/php-fpm/www.sock#listen = 9000#' -e 's#^;listen.owner = nobody#listen.owner = nginx#' -e 's#^;listen.group = nobody#listen.group = nginx#' -e 's#^;listen.mode = 0660#listen.mode = 0660#' -e 's#^(listen.allowed_clients = 127.0.0.1)#;\1#' -e 's#^;(pm.status_path = /status)#\1#' -e 's#^;(ping.path = /ping)#\1#' /etc/php-fpm.d/www.conf 
    
    [00:57:47 root@php-fpm ~]#systemctl enable --now php-fpm
    Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
    
    
    # 准备php文件
    [00:57:53 root@php-fpm ~]#mkdir /data/php
    [00:58:16 root@php-fpm ~]#vim /data/php/info.php
    
    <?php
        phpinfo();                                                                                                                                                                      
    ?>
    

    6.3.3.4 修改DNS解析, 测试访问

    10.0.0.87 test.wang.oog
    
    
    图片.png 图片.png

    相关文章

      网友评论

        本文标题:12. Nginx实现FastCGI

        本文链接:https://www.haomeiwen.com/subject/jfekcltx.html