CentOS 7 安装 PHP 5.6
一、前言
在 CentOS 7 上安装特定版本的软件有时可能会比较复杂,尤其是较旧的版本如 PHP 5.6,本文将详细介绍如何在 CentOS 7 系统上安装 PHP 5.6,包括必要的依赖和配置步骤。

二、安装步骤
1. 检查当前安装的 PHP 包
检查系统是否已经安装了 PHP,如果已安装,先将其删除。
- 查看已安装的 PHP 包
- yum list installed | grep php
- 卸载已有的 PHP 包
- yum remove phpcommon y
2. 配置 yum 源
为了安装 PHP 5.6,需要添加 EPEL 和 REMI 仓库。
- 安装 EPEL 仓库
- sudo yum install epelrelease y
- 安装 REMI 仓库
- sudo rpm ivh http://rpms.famillecollet.com/enterprise/remirelease7.rpm
- 启用 REMI 仓库中的 PHP 5.6 模块
- sudo yumconfigmanager enable remiphp56
3. 安装 PHP 5.6 及其扩展
使用yum
命令安装 PHP 5.6 以及常用的扩展模块。

- 安装 PHP 5.6 和常用扩展
- sudo yum install y php phpopcache phpdevel phpmbstring phpmcrypt phpmysqlnd phpphpunitPHPUnit phppeclxdebug phppeclxhprof
4. 验证 PHP 安装
安装完成后,可以通过以下命令验证 PHP 是否安装成功以及查看版本信息。
- 查看 PHP 版本
- php v
输出示例:
- PHP 5.6.40 (cli) (built: Mar 17 2019 18:31:29)
- Copyright (c) 19972016 The PHP Group
- Zend Engine v2.6.0, Copyright (c) 19982016 Zend Technologies
5. 启动并配置 PHPFPM
安装完 PHP 后,还需要配置并启动 PHPFPM。
- 创建并编辑 PHPFPM 配置文件
- sudo cp /usr/share/phpfpm5.6/www.conf.default /etc/opt/remi/phpfpm5.6/phpfpm.d/www.conf
- 修改配置文件以适应需求(可选)
- sudo nano /etc/opt/remi/phpfpm5.6/phpfpm.d/www.conf
启动 PHPFPM:
- 启动 PHPFPM
- sudo systemctl start phpfpm5.6
- 设置开机自启
- sudo systemctl enable phpfpm5.6
6. 配置 Nginx(可选)
如果你使用 Nginx 作为你的 web 服务器,你需要配置它与 PHPFPM 一起工作。
编辑 nginx 配置文件:
- sudo nano /etc/nginx/conf.d/default.conf
添加以下内容到server
块中:
- location ~ \.php$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- include fastcgi_params;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- }
重启 Nginx:
- 重启 Nginx
- sudo systemctl restart nginx
三、常见问题及解决方案
Q1:安装过程中遇到缺少依赖怎么办?
A1:如果在安装过程中遇到缺少依赖的问题,可以使用以下命令安装所需的依赖库:
- sudo yum update y
- sudo yum groupinstall "Development Tools" y
- sudo yum install gcc make libjpegdevel libpngdevel libxml2devel bzip2devel libcurldevel y
Q2:如何升级 PHP 到新版本?
A2:如果需要升级 PHP 到新版本,PHP 7.4,可以按照以下步骤操作:
1、禁用现有 PHP 模块:
- sudo yumconfigmanager disable remiphp56
2、启用新的 PHP 模块:
- sudo yumconfigmanager enable remiphp74
3、安装 PHP 7.4:
- sudo yum install y php phpopcache phpdevel phpmbstring phpmcrypt phpmysqlnd phpphpunitPHPUnit phppeclxdebug phppeclxhprof
4、重启相关服务:
- sudo systemctl restart phpfpm7.4
- sudo systemctl restart nginx