CentOS7 配置 nginx 和 php 方案

配置方案

鉴于网上教程错综复杂,写下一这篇文章

本教程只需要三步即可

一、安装软件

bash 复制代码
yum install -y nginx php php-fpm

二、编写配置文件,连接PHP

一般情况下在安装完 nginx 后会有 /etc/nginx/default.d/ 这个目录

在这个目录中新建一个 php-fpm.conf

bash 复制代码
vim /etc/nginx/default.d/php-fpm.conf

把下面内容复制进去

bash 复制代码
location ~* \.php$ {
    fastcgi_index   index.php;
    fastcgi_pass    127.0.0.1:9000;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

更改php-fpm的用户和用户组,不配值也行,但是防止后期php要用nginx用户权限更改文件还是配置一下比较好

bash 复制代码
vim /etc/php-fpm.d/www.conf

看一下 12 行 listen = 127.0.0.1:9000 是否与上面配置中的 fastcgi_pass 值一致

改 39 行为 user = nginx

改41 行为 group = nginx

三、引用文件

上面文件编写完成后,只需要在需要用到的地方输入 include /etc/nginx/default.d/*.conf;,比如:

bash 复制代码
 server {
        listen       80;
        server_name  php.server;
        root         /usr/share/nginx/html;
        index index.php; # 一定要写!如果编写的是 /etc/nginx/nginx.conf 文件,这一行可能没写
        
        # 一定要写在 server 块中
        include /etc/nginx/default.d/*.conf;

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

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

四、测试

测试应该不算在总步骤中吧,要是算就是四步完成

把文件 /usr/share/nginx/html/index.html 改为 /usr/share/nginx/html/index.php,并写入 <?php phpinfo(); ?>

bash 复制代码
mv /usr/share/nginx/html/index.html /usr/share/nginx/html/index.php
bash 复制代码
echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/index.php

启动 nginx 和 php-fpm 服务

bash 复制代码
systemctl start nginx php-fpm && systemctl enable nginx php-fpm

访问 http://php.server.ip ,结果如下即为成功

如果没有看见,仔细看一下有没有写错的,如果都没问题证明这一篇文章不适合你

相关推荐
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
AWS官方合作商9 天前
AWS ACM 重磅上线:公有 SSL/TLS 证书现可导出,突破 AWS 边界! (突出新功能的重要性和突破性)
服务器·https·ssl·aws
小米里的大麦9 天前
014 Linux 2.6内核进程调度队列(了解)
linux·运维·驱动开发
程序员的世界你不懂9 天前
Appium+python自动化(三十)yaml配置数据隔离
运维·appium·自动化
算法练习生9 天前
Linux文件元信息完全指南:权限、链接与时间属性
linux·运维·服务器
忘了ʷºᵇₐ9 天前
Linux系统能ping通ip但无法ping通域名的解决方法
linux·服务器·tcp/ip
浩浩测试一下9 天前
渗透测试指南(CS&&MSF):Windows 与 Linux 系统中的日志与文件痕迹清理
linux·运维·windows·安全·web安全·网络安全·系统安全
敏叔V5879 天前
大模型Text2SQL之在CentOS上使用yum安装与使用MySQL
linux·mysql·centos
weixin_425023009 天前
Spring Boot使用MCP服务器
服务器·spring boot·后端·spring ai·mcp
小生云木9 天前
Linux离线编译安装nginx
linux·运维·nginx