Friday, November 8, 2024

How to Set PHP 7.3 as the Default Version on CentOS 7 and Avoid PHP 8.0 Conflicts

In this blog post, we’ll address a common issue encountered when trying to set a specific PHP version as the default on CentOS 7, specifically PHP 7.3, while avoiding an unexpected upgrade to PHP 8.0. Let’s go over the necessary steps to install PHP 7.3 and its related packages as the default without PHP 8.0 dependencies interfering.

When managing software installations on CentOS, it’s common to use the yum package manager, which automatically resolves dependencies for the required software. However, this can sometimes lead to unintended outcomes, like installing an incompatible PHP version.

In this case, the problem arises because running:


$ sudo yum-config-manager --enable remi-php73 $ sudo yum install php php-pecl-mcrypt php-cli php-gd php-curl php-mysqlnd php-ldap php-zip php-fileinfo php-common php-xml php-fpm php-mbstring php-bcmath php-soap php-oci8

does not lock the system to PHP 7.3 specifically. Despite enabling the remi-php73 repository, yum may still select PHP 8.0 packages (from remi-php80) as dependencies, resulting in a conflict with the existing project dependencies.

Step-by-Step Guide to Forcing PHP 7.3 as the Default

Let’s go through each step to ensure that only PHP 7.3 packages are installed and set as the default.

Step 1: Enable the Remi Repository

The Remi repository is a third-party repository that provides various PHP versions for CentOS. If you haven’t already done so, install and enable the Remi repository:


$ sudo yum install -y epel-release yum-utils $ sudo yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm

After this, enable the remi-php73 repository specifically:


$ sudo yum-config-manager --enable remi-php73

Step 2: Install PHP 7.3 Packages Without Triggering PHP 8.0

Now that the remi-php73 repository is enabled, you’ll need to install PHP 7.3 and its related packages. Instead of using general PHP package names (like php or php-cli), specify php73- prefixes to ensure that only PHP 7.3 versions are installed. Here’s how to do this:


$ sudo yum install -y php73 php73-php-pecl-mcrypt php73-php-cli php73-php-gd php73-php-curl php73-php-mysqlnd php73-php-ldap php73-php-zip php73-php-fileinfo php73-php-common php73-php-xml php73-php-fpm php73-php-mbstring php73-php-bcmath php73-php-soap php73-php-oci8

Using the php73- prefix ensures that yum pulls only PHP 7.3 packages and avoids PHP 8.0.

Step 3: Set PHP 7.3 as the Default Version

After installing, you may still need to set PHP 7.3 as the system’s default PHP version. The update-alternatives command can manage multiple PHP versions and select the default:


$ sudo update-alternatives --install /usr/bin/php php /usr/bin/php73 73 $ sudo update-alternatives --set php /usr/bin/php73

This will ensure that calling php from the command line will use PHP 7.3.

Step 4: Verify the PHP Version

To confirm that PHP 7.3 is correctly set as the default, run:


$ php -v

You should see output similar to:


PHP 7.3.x (cli) (built: ...)

Step 5: Test PHP-FPM (Optional)

If you’re using PHP-FPM for web server integration, restart the php-fpm service and make sure it’s the PHP 7.3 version:


$ sudo systemctl enable php73-php-fpm $ sudo systemctl start php73-php-fpm $ sudo systemctl status php73-php-fpm

Troubleshooting Common Issues

If you encounter any issues during installation, here are some common solutions:

  1. Dependency Conflicts: Ensure no PHP 8.0 or other PHP packages are already installed. If they are, remove them with:


    $ sudo yum remove php*

    Then repeat the installation steps above.

  2. Re-Enable the Remi Repository: If you switch between different PHP versions in the Remi repository, it may be necessary to disable or enable the desired version with:


    $ sudo yum-config-manager --disable remi-php80 $ sudo yum-config-manager --enable remi-php73
  3. PHP-FPM Service Name: Depending on the installed PHP version, the service name for PHP-FPM might vary (php-fpm, php73-php-fpm). Verify the correct service name with:


    $ systemctl list-units --type=service | grep php

Conclusion

Setting PHP 7.3 as the default version on CentOS 7 while avoiding PHP 8.0 dependencies requires a few specific steps. By enabling the correct repository, installing version-specific packages, and setting the default version, you can maintain a stable PHP environment tailored to your project’s requirements. Following these steps ensures compatibility and helps you avoid unexpected version conflicts in the future.

0 comments:

Post a Comment