Set default version of Php in CentOS 7

Set default version of Php in CentOS 7

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Managing PHP Version Defaults in CentOS 7

When you install multiple PHP versions side-by-side on a CentOS 7 system, typically using the REMI repository structure where binaries reside under /opt/remi/, the shell does not automatically know which one to invoke. If you run php -v and see output corresponding to PHP 5.6 (Zend Engine v2.6.0), but your local configuration requires PHP 7.2, the issue lies in how the system resolves the php executable versus the specific versioned paths.

The core problem is that /usr/bin/php acts as a symlink or wrapper pointing to the default installation (usually PHP 5.6 in older REMI setups), while your newer versions live in /opt/remi/php72/. To make PHP 7.2 the active version without globally breaking existing scripts, you must explicitly update the system path resolution.

Updating the System Symlink

The most direct method to change the default version is to modify the symbolic link at /usr/bin/php. This approach ensures that any new shell session or cron job calling php immediately picks up the 7.2 binaries unless a specific alias overrides it.

First, ensure you are root or have sudo privileges. You need to remove the existing symlink and create a new one pointing to the PHP 7.2 binary:

# Remove the current default symlink if it exists
sudo rm /usr/bin/php

# Create a new symlink pointing to the PHP 7.2 binary
sudo ln -s /opt/remi/php72/root/usr/bin/php /usr/bin/php

After executing these commands, verify the change:

php -v

The output should now reflect Zend Engine 3.x and the specific version of PHP 7.2 you installed. This method is effective for system-wide defaults but carries a risk: if a legacy application depends on PHP 5.6, it will break unless you are meticulous about version management.

Using Environment Variables for Specific Directories

If your workflow involves switching projects that require different PHP versions frequently, relying solely on a global symlink can be fragile. A more granular approach is to use environment variables like PATH or the PHPRC variable.

You can prepend the REMI PHP 7.2 directory to your PATH in your shell profile (e.g., ~/.bashrc):

export PATH="/opt/remi/php72/root/usr/bin:$PATH"

However, this only affects new terminal sessions. For a persistent system-wide change without touching the main /usr/bin/php, you can also utilize the PHPRC environment variable to tell PHP where to find its configuration files specific to version 7.2:

export PHPRC="/opt/remi/php72/root/etc"

This is particularly useful if you have separate php.ini files for each version and want to ensure the correct configuration loads without altering the binary path directly. Note that modifying PHPRC does not change which executable is called; it only changes the runtime environment. If your goal is strictly to make php -v report 7.2, updating the symlink remains the most robust solution for the binary itself.

Handling Apache and CLI Separation

A common pitfall in CentOS environments is conflating the Command Line Interface (CLI) with the Apache HTTP Server module. The commands above only affect the CLI (php, phpize, php-fpm). If your web server is using PHP 7.2, you must ensure the Apache module also points to the correct version.

In CentOS with REMI, this is usually handled by managing the mod_php modules in /opt/remi. You might need to run:

sudo yum reinstall php72-php-mod

Or ensure that the Apache configuration includes the specific 7.2 module path rather than the generic one. If you are running PHP-FPM, you must also update the php-fpm symlink and the corresponding pool configuration files (e.g., /etc/pool/php72.conf) to point to the correct socket and binary paths.

Conclusion

Switching the default PHP version on CentOS 7 is fundamentally about controlling which executable gets called. By updating the /usr/bin/php symlink or adjusting your environment variables, you can seamlessly transition from a legacy PHP 5.6 setup to PHP 7.2. Just remember that global changes require caution when managing mixed-version projects. For complex setups involving multiple domains with different requirements, consider isolating versions using Docker containers or maintaining strict versioning in your deployment scripts to avoid accidental breakages.

Our network