0

Install PHP 7.4-8.3 Centos 9 Stream

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.

Table of Contents

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.

Example output:

list php modules on centos 9 or 8 stream from remi repo

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 find apache user in php-fpm configuration on centos 9 or 8 stream

Example to:

example change to nginx from apache user for php-fpm on centos 9 or 8 stream

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.

Облачная платформа

Свежие комментарии

Подписка

Лучшие статьи

WordPress › Ошибка

На сайте возникла критическая ошибка.

Узнайте больше про решение проблем с WordPress.