商城首页欢迎来到中国正版软件门户

您的位置: 首页 > 文章列表 > 编程开发 > php nginx 整合_centos nginx整合php

php nginx 整合_centos nginx整合php

  发布于2026-07-20 阅读(0)

扫一扫,手机访问

在CentOS 7上部署LNMP环境时,PHP的安装和配置是核心环节之一。这里整理了一份完整的操作指南,从安装到配置,再到与Nginx的整合,尽量做到一步到位。

1. 安装PHP

首先,安装PHP及其相关扩展:

yum install php php-mysql php-fpm

安装过程中,你可能会遇到这样的问题:

2:postfix-2.10.1-6.el7.x86_64 有缺少的需求 libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-6.el7.x86_64 有缺少的需求 libmysqlclient.so.18(libmysqlclient_18)(64bit)

这时候,最直接的解决办法是:把php-mysql换成php-mysqlnd。也就是说,执行:

yum install php php-mysqlnd php-fpm

2. 配置PHP处理器

接下来,需要调整PHP的配置文件。先打开/etc/php.ini

vim /etc/php.ini

找到cgi.fix_pathinfo这一项,将;cgi.fix_pathinfo=1改为cgi.fix_pathinfo=0

然后,编辑/etc/php-fpm.d/www.conf

vim /etc/php-fpm.d/www.conf

user = nobodygroup = nobody改为:

user = nginx
group = nginx

当然,前提是系统里已经创建了nginx用户和nginx组。如果没有,可以这样操作:

# 添加nginx用户和用户组
groupadd -r nginx
useradd -r -g nginx nginx

3. 启动PHP-FPM

配置完成后,启动PHP-FPM服务:

systemctl start php-fpm

4. 设置开机自启

为了让PHP-FPM在系统重启后自动运行,需要设置开机启动:

systemctl enable php-fpm

5. 配置Nginx

接下来,要让Nginx能够处理PHP请求。打开/etc/nginx/conf.d/default.conf(如果不存在,就手动创建),并写入以下配置:

server {
    listen 80;
    server_name www.twogether.cn;

    # 注意:这几行来自原来的 "location /" 块
    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        root /usr/share/nginx/html;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi.conf;
    }
}

6. 重启Nginx

配置文件修改后,需要重启Nginx使改动生效:

systemctl restart nginx

7. 测试PHP

最后,验证PHP是否正常工作。在/usr/share/nginx/html目录下创建一个测试文件:

vim /usr/share/nginx/html/info.php

输入以下内容:

3fc6c3116a3e

然后,在浏览器中访问这个文件,比如http://your-server-ip/info.php。如果能看到PHP的信息页面,就说明配置成功了。

本文转载于:https://blog.csdn.net/weixin_35853499/article/details/115091448 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注