How to Install PHP 8.2, 8.1, 8.0, 7.4 on CentOS Stream EL9 or EL8


PHP (Hypertext Preprocessor) is a widely-used, open-source programming language particularly suited for web development. It can be easily integrated into the CentOS stream system, providing many benefits. Some of the key features of PHP include:

  • Server-side scripting: PHP is a server-side language, meaning the code is executed on the server before the result is sent to the client. This allows for dynamic web pages and improved security.
  • Cross-platform compatibility: PHP can run on various operating systems, including Windows, Mac, and Linux. This makes it a versatile choice for web development.
  • Large community: PHP has a large and active community of developers, which means a wealth of resources and support is available.
  • Wide range of built-in functions: PHP includes many built-in functions that can perform common web development tasks, such as connecting databases, sending emails, and creating image thumbnails.
  • Integrated support for many databases: PHP supports many popular databases, including MySQL, PostgreSQL, and Oracle. This makes it easy to work with databases in your PHP projects.
  • Integration with other technologies: PHP can be easily integrated with other technologies, such as JavaScript and AJAX, to create interactive and dynamic web applications.
  • Extensive collection of libraries and frameworks: PHP has a large group of libraries and frameworks that can be used to simplify and speed up web development. Some popular PHP frameworks include Laravel, CodeIgniter, and Zend Framework.
  • Easy to learn: PHP is relatively easy to understand, making it an excellent choice for beginners and those with limited programming experience. The syntax is similar to other popular programming languages such as C and Java, and many tutorials and resources are available to help you get started.
  • Cost-effective: PHP is open-source software that is free to use and distribute. This makes it a cost-effective solution for web development, especially for small businesses or individuals.
  • High performance: PHP has been optimized to provide high performance and speed, making it suitable for large and complex web applications. Additionally, PHP 7 and later versions introduced new features such as Just-In-Time (JIT) compilation that further improves the performance of the language.

The latest versions of CentOS Stream, including 9 and 8, now support installing multiple versions of PHP using the Remi PHP repository. This allows you to easily integrate PHP, an open-source programming language widely used for web development, into your system. The available versions include 8.2, 8.1, 8.0, and 7.4, which can be easily integrated with various servers, databases, and technologies. This guide will show you how to properly install these different versions of PHP on your CentOS Stream system.

Step 1: Update CentOS Stream

Before installing any new software, updating the system is always a good idea. To do this, open a terminal and run the following commands:

sudo dnf upgrade --refresh

Step 2: Import PHP Remi Repository

To access the Remi PHP repository, it is necessary first to install the EPEL (Extra Packages for Enterprise Linux) repository. EPEL is a valuable resource, particularly for new users of distributions like CentOS Stream, which is built on RHEL and provides a vast array of commonly used software packages for Enterprise Linux.

Make sure to import the appropriate repository for your version of CentOS Stream.

Import Remi PHP Repository for CentOS 9 Stream

While this is optional for EL9, it is recommended to enable the CRB.

sudo dnf config-manager --set-enabled crb

With the CRB enabled, execute the following command to install both versions of EPEL for EL9.

sudo dnf install \
    https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm \
    https://dl.fedoraproject.org/pub/epel/epel-next-release-latest-9.noarch.rpm

Use the command below to import the EL 9 Remi repository.

sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-9.rpm -y

Import Remi PHP Repository for CentOS 8 Stream

To start, use the command below to enable EPEL for EL8.

sudo dnf install \
    https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm \
    https://dl.fedoraproject.org/pub/epel/epel-next-release-latest-8.noarch.rpm

Now, use this command to import the Remi EL 8 repository that contains PHP.

sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm -y

Step 3: Enable PHP Remi Repository

To ensure you can view all versions of the REMI PHP repository, list the PHP modules first and then adjust your selection as needed once it is confirmed.

dnf module list php

If prompted, enter (Y) to import the GPG key for Remi’s repository to proceed.

See also
How to Install Foliate on Fedora Linux

Example output:

After that, activate the version of PHP you want to install.

Enable PHP 8.2 on CentOS Stream.

sudo dnf module enable php:remi-8.2 -y

Enable PHP 8.1 on CentOS Stream.

sudo dnf module enable php:remi-8.1 -y

Enable PHP 8.0 on CentOS Stream.

sudo dnf module enable php:remi-8.0 -y

Enable PHP 7.4 on CentOS Stream.

sudo dnf module enable php:remi-7.4 -y

Step 4: Install PHP 8.2, 8.1, 8.0 or 7.4

Now that the Remi PHP repository is enabled, you can install the PHP. Below are some options to choose between Apache and Nginx, but you can customize these further if you know what extensions to install for your CMS or software development.

Apache (httpd) PHP:

sudo dnf install php php-cli -y

Nginx PHP:

sudo dnf install php-fpm php-cli -y

After the installation is complete, execute the following command to verify that PHP has been installed correctly.

php -v

Optionally, run the command below to obtain the most frequently used extensions for your selected version of PHP. Be sure to remove any extensions you know are not needed.

sudo dnf install php-cli php-fpm php-curl php-mysqlnd php-gd php-opcache php-zip php-intl php-common php-bcmath php-imagick php-xmlrpc php-json php-readline php-memcached php-redis php-mbstring php-apcu php-xml php-dom php-redis php-memcached php-memcache

You can execute the command below at any time to view the currently loaded modules.

php -m

It is recommended to regularly monitor and remove any unnecessary modules, as many installed modules can negatively impact system performance.

If you want to install the development branch, you can use the below command.

sudo dnf install php-devel

To add additional development tools, such as debugging tools, use the command given below.

sudo dnf install php-xdebug php-pcov

It’s worth noting that installing this version will introduce multiple dependencies, and it’s not suggested unless you have a specific need for it in your PHP development or any specific requirement.

Step 5: Set Up Nginx user for PHP-FPM

When installing PHP-FPM on distributions like Debian/Ubuntu, the “www-data” user is often used. However, this is not the default for installations on the RHEL family. By default, the PHP-FPM service is configured to run under the “Apache” user, which is not suitable for use with Nginx. Therefore, adjustments need to be made.

First, open the configuration file (www.conf) with the following command.

sudo nano /etc/php-fpm.d/www.conf

Then, substitute the (Apache) user and group with the (Nginx) user and group.

Example from:

Example to:

To save, press (CTRL+O), then exit (CTRL+X).

Now, you can restart your PHP-FPM service.

sudo systemctl restart php-fpm

Example Nginx PHP-FPM Server Block Code

For Nginx to process PHP files, the server block needs a specific configuration, as demonstrated in the example below. This example applies to all server{} blocks that handle PHP files and necessitates the inclusion of “location ~ \.php$.”

Example ONLY:

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index   index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

To confirm that the changes made to the code above did not cause any errors, execute the following command to test Nginx.

sudo nginx -t

Example output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

To finish the PHP-FPM setup, the Nginx service needs to be restarted.

sudo systemctl restart nginx

Conclusion

This guide covered installing PHP on CentOS Stream by activating the Remi PHP repository, making the chosen version of PHP the default, installing popular extensions, and configuring the Nginx server block to handle PHP files. It’s crucial to monitor dependencies and eliminate unnecessary modules. Also, it’s essential to test Nginx for errors before restarting the service in the future, particularly in a live environment.

Leave a Comment

Share to...