Linux下安装PHP环境并配置Nginx支持php-fpm模块

时间:2022-10-26 21:36:47

修改php配置

vi /etc/php.ini
打开php配置文件/etc/php.ini找到cgi.fix_pathinfo配置项,这一项默认被注释并且值为1,根据官方文档的说明,这里为了当文件不存在时,阻止Nginx将请求发送到后端的PHP-FPM模块,从而避免恶意脚本注入的攻击,所以此项应该去掉注释并设置为0
重启php

kill php进程ID

/usr/sbin/php-fpm

修改nginx配置


vi `/etc/nginx/conf.d/default.conf`

修改文件为:

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        location ~ .*\.php$ {
           root           html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
        }

        error_page 404 /404.html;
                location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
                location = /50x.html {
        }
}
重启nginx
kill nginx进程ID

/usr/sbin/nginx

测试php能不能用

cd /usr/share/nginx/html

touch a.php
写以下内容
<?php
echo 12345;