How to Safely Upgrade PHP 7.0 to 7.2 on CentOS Without Breaking Production Environment


11 views

When upgrading from PHP 7.0 to 7.2 on CentOS, it's crucial to understand that these versions cannot coexist through the default package manager. The Webtatic repository (which provides php70w/php72w packages) doesn't support parallel installation of major PHP versions.

# Check current PHP version
php -v

# List installed PHP packages
rpm -qa | grep php

# Backup important configurations
sudo cp -a /etc/php.d /etc/php.d.backup
sudo cp -a /etc/php.ini /etc/php.ini.backup

First, remove existing PHP 7.0 packages:

sudo yum remove php70w* -y

Install PHP 7.2 repository:

sudo yum install https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
sudo yum install php72w php72w-opcache php72w-common php72w-mysqlnd php72w-mbstring php72w-gd php72w-xml php72w-pecl-redis php72w-pecl-memcached -y
php -v
# Should show PHP 7.2.x

php -m
# Verify all required extensions are loaded

Problem: Web server fails to start after upgrade
Solution: Check error logs and reinstall web server modules:

# For Apache:
sudo yum install php72w-httpd -y
sudo systemctl restart httpd

# For Nginx:
sudo systemctl restart php-fpm

Create a test script to verify functionality:

<?php
// test_compatibility.php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Test database connectivity
try {
    $pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'password');
    echo "Database connection successful<br>";
} catch (PDOException $e) {
    echo "Database error: " . $e->getMessage();
}

// Test common functions
echo "Mbstring: " . (function_exists('mb_strlen') ? 'OK' : 'Missing') . "<br>";
echo "GD: " . (function_exists('imagecreate') ? 'OK' : 'Missing') . "<br>";
?>

In case of critical failures, you can revert to PHP 7.0:

sudo yum remove php72w* -y
sudo yum install php70w* --skip-broken -y
sudo cp -a /etc/php.d.backup/* /etc/php.d/
sudo cp /etc/php.ini.backup /etc/php.ini

PHP 7.2 includes several optimizations. Benchmark your application with:

ab -n 1000 -c 50 http://yourserver.com/test_page.php

When upgrading PHP on a production CentOS server, the key consideration is maintaining stability while transitioning between major PHP versions. PHP 7.0 and 7.2 cannot safely coexist through parallel installation due to shared dependencies and configuration conflicts.

Before proceeding with the upgrade:


# Check current PHP version
php -v

# List installed PHP packages
rpm -qa | grep php

# Backup critical configurations
sudo cp -R /etc/php.d/ /root/php.d_backup
sudo cp /etc/php.ini /root/php.ini_backup

The most reliable approach involves removing PHP 7.0 packages before installing PHP 7.2 components. Here's the step-by-step process:


# Remove existing PHP 7.0 packages
sudo yum remove php70w*

# Clean up orphaned dependencies
sudo package-cleanup --orphans

# Add the Webtatic repository for PHP 7.2
sudo rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

After removing PHP 7.0, install the new version with required extensions:


# Install base PHP 7.2 package
sudo yum install php72w

# Install common extensions (adjust based on your requirements)
sudo yum install php72w-common php72w-mysql php72w-mbstring php72w-gd php72w-opcache

# Verify the installation
php -v

After installation, you'll need to:


# Restore your custom php.ini settings
sudo diff /root/php.ini_backup /etc/php.ini

# Reactivate any custom modules
sudo cp /root/php.d_backup/*.ini /etc/php.d/

Before putting the server back into production:


# Create a test PHP file
echo "" > /var/www/html/phpinfo.php

# Check for any errors in the logs
tail -f /var/log/php-fpm/error.log

Always prepare a rollback plan:


# To revert, simply remove PHP 7.2 packages
sudo yum remove php72w*

# Reinstall PHP 7.0 from backups
sudo yum install php70w*