How To Upgrade PHP Version on CentOS 7 with Easy Steps

Posted on

How To Upgrade PHP Version on CentOS 7 with Easy Steps

How To Upgrade PHP Version on CentOS 7 with Easy Steps

In this guide, brought to you by Orcacore, you will learn How To Upgrade PHP Version on CentOS 7. PHP (Hypertext Preprocessor) is a widely used, general-purpose scripting language especially suited for web development. It was one of the first server-side languages that could be embedded directly into HTML, making it incredibly convenient to add dynamic content to web pages without relying on external data files. Over the years, PHP has evolved significantly, with regular updates introducing new features and enhanced capabilities. Keeping your PHP version up to date is crucial for security, performance, and compatibility with the latest web technologies. This article focuses on How To Upgrade PHP Version on CentOS 7.

To follow this guide, ensure you have access to your CentOS 7 server as a non-root user with sudo privileges. If you need assistance with this, please refer to our guide on Initial Server Setup with CentOS 7.

1. Check Default CentOS 7 PHP Version

The default PHP version on CentOS 7 is 5.4. To confirm the current PHP version installed on your system, execute the following command in your terminal:

php -v

The output should resemble the following:

Default CentOS 7 PHP Version

This confirms that the default PHP 5.4 is installed. Time for How To Upgrade PHP Version on CentOS 7.

2. Install PHP Remi Repository on CentOS 7

To upgrade your PHP version beyond the default, you’ll need to install the Remi repository. Remi’s RPM repository is a free and stable YUM repository specifically designed for the PHP stack. It provides access to more recent PHP versions and related packages.

Install the Remi repository using the following command:

sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y

Next, install the yum-utils package. This package contains a collection of helpful tools and programs for managing yum repositories, installing debug packages, source packages, and extracting extended information from repositories.

sudo yum install yum-utils -y

3. Enable PHP Remi Repo on CentOS 7

Now, enable the Remi repository for your desired PHP version on CentOS 7. In this example, we’ll enable PHP 8.1.

sudo yum-config-manager --enable remi-php81

To verify that the Remi repository for PHP 8.1 is enabled, run:

sudo yum repolist

The output should include the Remi repository in the list of enabled repositories:

Enable PHP Remi Repo on CentOS 7

Note: If you want to install PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, or PHP 8.0 on CentOS 7, enable the corresponding Remi repository using the appropriate command:

# yum-config-manager --enable remi-php71
# yum-config-manager --enable remi-php72
# yum-config-manager --enable remi-php73
# yum-config-manager --enable remi-php74
# yum-config-manager --enable remi-php80

Finally, update your local package index to reflect the changes in the repository configuration:

sudo yum update -y

Check your PHP version again:

php -v
Output
PHP 8.1.10 (cli) (built: Aug 30 2022 16:09:36) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.10, Copyright (c) Zend Technologies

As you can see, PHP has been successfully upgraded to the latest version (in this case, PHP 8.1). That concludes the process of How To Upgrade PHP Version on CentOS 7 using Remi repository.

Conclusion

You have successfully learned How To Upgrade PHP Version on CentOS 7. Upgrading PHP on CentOS 7 is essential for maintaining security, improving performance, and ensuring compatibility with modern web applications. Older PHP versions are no longer supported with security updates, making your system vulnerable to exploits.

We hope you found this guide helpful. Please subscribe to us on Facebook and YouTube.

You may also like these articles:

Alternative Solutions for Upgrading PHP on CentOS 7

While the Remi repository method is a popular and reliable approach, here are two alternative solutions to consider when How To Upgrade PHP Version on CentOS 7:

1. Using Software Collections (SCL)

Software Collections (SCL) allow you to install multiple versions of the same software on a single system without conflicting with the base OS packages. This is particularly useful if you need to support applications that require different PHP versions simultaneously.

Explanation:

SCL provides a way to install and manage software in isolated environments. It achieves this by creating separate directories for each software collection and using environment modules to switch between them. This prevents conflicts between different versions of the same software.

Steps:

  1. Install the SCL repository:

    sudo yum install centos-release-scl -y
  2. Install the desired PHP version via SCL: For example, to install PHP 7.3:

    sudo yum install rh-php73 -y
  3. Enable the SCL environment:

    scl enable rh-php73 bash

    This command starts a new shell session with PHP 7.3 enabled. To use PHP 7.3 in a script, you can wrap the script execution in the scl enable command.

Code Example:

Let’s say you have a PHP script info.php:

<?php
phpinfo();
?>

To run this script with PHP 7.3 using SCL, you can use the following command:

scl enable rh-php73 "php info.php"

This will execute the info.php script using the PHP 7.3 interpreter within the SCL environment. After the command completes, you are back to your base PHP version.

Advantages:

  • Allows for multiple PHP versions to coexist on the same server.
  • Reduces the risk of breaking existing applications during the upgrade process.

Disadvantages:

  • Requires a different approach to running PHP scripts, as you need to enable the SCL environment each time.
  • Can be more complex to manage than the Remi repository method.

2. Building PHP from Source

Building PHP from source code gives you the ultimate control over the installation process. You can customize compile-time options and ensure that PHP is built with the specific features and extensions you need.

Explanation:

Building from source involves downloading the PHP source code, configuring the build environment, compiling the code, and installing the resulting binaries. This method requires more technical expertise but offers greater flexibility.

Steps:

  1. Install Development Tools and Libraries:

    sudo yum groupinstall "Development Tools" -y
    sudo yum install libxml2-devel bzip2-devel curl-devel libpng-devel freetype-devel openssl-devel -y
  2. Download the PHP Source Code:

    Download the source code from php.net. Choose the version you want to install (e.g., PHP 8.2). Use wget to download it directly to your server.

    wget https://www.php.net/distributions/php-8.2.0.tar.gz
    tar -xvf php-8.2.0.tar.gz
    cd php-8.2.0
  3. Configure the Build:

    The ./configure script prepares the source code for compilation. You can specify various options to enable or disable features. A typical configuration command might look like this:

    ./configure --with-apxs2=/usr/sbin/apxs --with-mysqli --with-pdo-mysql --with-openssl --with-curl --enable-mbstring --with-gd --with-jpeg --with-png
    • --with-apxs2=/usr/sbin/apxs: Enables Apache module support. Replace with the correct path to your apxs binary.
    • --with-mysqli, --with-pdo-mysql: Enables MySQL support.
    • --with-openssl, --with-curl: Enables SSL and cURL support.
    • --enable-mbstring: Enables multi-byte string support.
    • --with-gd, --with-jpeg, --with-png: Enables GD library for image manipulation.
  4. Compile and Install:

    make
    sudo make install

    The make command compiles the source code. sudo make install installs the compiled binaries to the system.

  5. Configure Apache (if applicable):

    If you configured PHP as an Apache module, you may need to manually configure Apache to load the new PHP module. This typically involves adding a LoadModule directive to your Apache configuration file.

Code Example:

After installation, you might need to manually add PHP to your Apache configuration. Edit your Apache configuration file (e.g., /etc/httpd/conf/httpd.conf or /etc/apache2/apache2.conf) and add the following line (adjust the path to libphp8.so if needed):

LoadModule php_module modules/libphp8.so

Then, restart Apache:

sudo systemctl restart httpd

Advantages:

  • Provides maximum control over the PHP installation.
  • Allows you to customize compile-time options.
  • Useful for installing PHP versions that are not available in standard repositories.

Disadvantages:

  • Requires more technical expertise.
  • Can be time-consuming.
  • You are responsible for managing updates and security patches manually.

Choosing the right method for How To Upgrade PHP Version on CentOS 7 depends on your specific needs and technical expertise. The Remi repository is generally the easiest and most convenient option, while SCL offers a way to manage multiple PHP versions, and building from source provides the greatest flexibility.

Leave a Reply

Your email address will not be published. Required fields are marked *