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 ,结果如下即为成功

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

相关推荐
2603_955279702 分钟前
Cursor + GitOps:自动化运维新姿势
运维·自动化
Waay25 分钟前
面试口述版:个人对 Prometheus 完整理解
运维·学习·云原生·面试·职场和发展·kubernetes·prometheus
三8441 小时前
文件查找/文件压缩/解压缩
linux·运维·服务器
小猪写代码1 小时前
Linux 管道(Pipeline)作业
linux·运维·服务器
桌面运维家1 小时前
如何用半缓存云桌面将服务器硬盘容量扩展至本地终端?
运维·服务器·缓存
Jurio.1 小时前
Codex App SSH 远程开发教程:本地连接远程服务器项目
服务器·ssh·远程工作·codex
激情的学姐1 小时前
【大型网站技术实践】初级篇:借助Nginx搭建反向代理服务器
运维·nginx
ai_coder_ai2 小时前
编写自动化脚本,在自己后端服务中使用Open Api进行设备相关操作
java·运维·自动化
spider_xcxc3 小时前
Redis 数据库高质量实践指南(一)
运维·数据库·redis·oracle·云计算
2501_943782354 小时前
【共创季稿事节】摇骰子:用 ArkTS 实现随机动画与交互反馈
运维·nginx·交互·harmonyos·鸿蒙·鸿蒙系统