Keywords: PHP Upgrade | CentOS 7 | Package Conflict Resolution | IUS Repository | Apache Configuration
Abstract: This article provides a comprehensive guide to completely uninstall old PHP versions and upgrade to PHP 7 on CentOS 7 systems. By analyzing common package conflict issues, it introduces the safe upgrade method using the IUS Community Project repository, including key steps such as removing old packages, installing new versions, and configuring web servers. The article also offers detailed command-line operations and troubleshooting guidance to ensure a smooth PHP upgrade process without conflicts.
Problem Background and Challenges
When upgrading PHP versions in CentOS 7 environments, developers often encounter package dependency conflicts. Typical scenarios include conflicts between php56w-common and php-common-5.4.16 when upgrading from PHP 5.4 to higher versions, which prevent PDO drivers from loading properly and affect the normal operation of frameworks like Laravel.
Solution Overview
To thoroughly resolve PHP version conflicts, a systematic uninstall and reinstall strategy is recommended. The core approach involves completely removing all old PHP packages first, then installing the new version through a reliable third-party repository to avoid dependency conflicts in the package manager.
Detailed Implementation Steps
Configuring the IUS Community Project Repository
The IUS Community Project provides tested PHP packages that ensure compatibility with CentOS systems. First, download and execute the installation script:
cd ~
curl 'https://setup.ius.io/' -o setup-ius.sh
sudo bash setup-ius.shThis process configures the system's yum repository by adding the IUS project software sources.
Removing Old PHP Packages
For environments using Apache and mod_php, completely remove existing PHP packages:
sudo yum remove php-cli mod_php php-commonWhen executing this command, the system will prompt for confirmation; enter y and press Enter to continue. This step ensures all related components of the old version are completely cleared.
Installing New PHP 7
Install PHP 7 series packages from the IUS repository:
sudo yum install mod_php70u php70u-cli php70u-mysqlndHere, three core components are installed: mod_php70u provides Apache module support, php70u-cli provides command-line interface, and php70u-mysqlnd provides MySQL native driver support. Confirmation is required during installation.
Restarting the Web Server
After installation, restart the Apache service to load the new PHP module:
sudo apachectl restartThe Apache service status can be checked using systemctl:
systemctl status httpdAlternative Approach Comparison
Although using yum -y remove php* can quickly remove all PHP packages, this method may be too aggressive and delete some unnecessary dependency packages. In contrast, the selective removal of specific packages is safer and more controllable.
Verification and Testing
After the upgrade is complete, verify the PHP version and module loading by creating a test file:
<?php
phpinfo();
?>Accessing this page confirms the PHP version information and whether extensions like PDO are correctly loaded.
Troubleshooting
If issues persist after upgrade, check the following aspects: ensure all old PHP packages have been completely removed; verify the IUS repository configuration is correct; check Apache error logs for detailed error information; confirm all necessary PHP extensions are properly installed.