How to Install PHP on CentOS Stream 9

PHP is a widely-used server-side scripting language designed for web development but also used as a general-purpose programming language. It’s particularly favored for creating dynamic and interactive websites. Installing the latest versions of PHP, such as 8.3, 8.2, or 8.1, ensures that you have access to the latest features, performance improvements, and security patches. On CentOS Stream 9, you can easily install these versions using the Remi RPM repository, which is well-known for providing up-to-date PHP packages.

This guide will walk you through the process of enabling the Remi repository and installing the latest PHP builds on CentOS Stream 9.

Import Remi PHP RPM on CentOS

Updating CentOS Stream Before PHP Installation

It’s crucial to start by updating your CentOS Stream system to ensure all existing packages are up to date. This step is essential for system stability and security.

Open your terminal and execute the following command to update your system:

sudo dnf upgrade --refresh

This command refreshes your package database and upgrades all the installed packages to their latest available versions.

Importing Remi PHP RPM Repository

The Remi PHP repository is a third-party repository that offers the latest PHP versions. Before adding the Remi repository, you must install the EPEL repository, which provides extra packages for Enterprise Linux.

First, enabling the CodeReady Linux Builder (CRB) repository is recommended, though optional. It provides additional developer tools and libraries. Enable CRB with:

sudo dnf config-manager --set-enabled crb

After enabling CRB, install the EPEL repository for EL9 using:

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

Finally, import the Remi PHP repository for EL9:

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

Enabling PHP Remi Repository on CentOS Stream

Listing Available PHP Modules

Before installing PHP, it’s essential to identify the available PHP versions in the Remi repository. This step helps you make an informed decision about which version to install.

Use the following command to list all PHP modules:

dnf module list php

This command displays all the PHP versions available in the Remi repository. It’s a crucial step to ensure you choose a version that best suits your requirements.

Importing GPG Key for Remi’s Repository

When prompted, you must confirm the import of the GPG key for Remi’s repository. This step is vital for security reasons, as it verifies the authenticity of the packages you are about to install. Enter ‘Y’ when asked to proceed with the import.

Activating Your Desired PHP Version

After confirming the available PHP versions, you can enable the specific version that fits your needs. It is advisable to select a stable and well-supported version of PHP for your projects.

To enable a specific PHP version from the Remi repository, use the following commands:

sudo dnf module enable php:remi-8.3 -y
sudo dnf module enable php:remi-8.2 -y
sudo dnf module enable php:remi-8.1 -y

Each command activates a different version of PHP from the Remi repository. Choose the command that corresponds to the version you wish to install. The ‘-y’ flag in these commands indicates automatic confirmation of the installation, streamlining the process.

Install PHP 8.3, 8.2 or 8.1 on CentOS Stream

Choosing the Web Server for PHP Installation

Selecting the appropriate web server is a key step in setting up PHP. CentOS Stream supports both Apache and Nginx, each requiring different PHP packages.

Apache (httpd) PHP Installation

For those using Apache as their web server, the following command installs PHP along with the PHP Command Line Interface (CLI), which is essential for running PHP scripts from the command line:

sudo dnf install php php-cli -y

Nginx PHP Installation

If you’re using Nginx, you’ll need to install PHP FastCGI Process Manager (FPM) alongside the PHP CLI. PHP-FPM is an alternative PHP FastCGI implementation that’s highly efficient for sites with heavy traffic:

sudo dnf install php-fpm php-cli -y

Verifying PHP Installation

After installation, it’s important to confirm that PHP is correctly installed. Run the following command to check the installed PHP version:

php -v

This command displays the current PHP version, verifying successful installation.

Installing Common PHP Extensions

To enhance PHP functionality, you might need to install additional extensions. The command below installs commonly used PHP extensions, which are vital for various CMS platforms and development needs:

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

It’s advisable to review and omit any extensions that are not necessary for your setup.

Viewing Loaded PHP Modules

To inspect the currently loaded PHP modules, execute:

php -m

Regularly monitoring and pruning unnecessary modules is recommended to maintain optimal system performance.

Installing PHP Development Branch

For specific development requirements, you might need the PHP development branch. Install it with:

sudo dnf install php-devel

Note: This installation introduces multiple dependencies. Only proceed if there’s a specific need in your PHP development environment.

Adding PHP Development Tools

For advanced development features, including debugging, install additional tools using:

sudo dnf install php-xdebug php-pcov

These tools are essential for debugging and code coverage analysis in PHP, but be cautious of the additional dependencies they introduce.

Configuring Nginx User for PHP-FPM on CentOS Stream

Editing PHP-FPM Configuration for Nginx

In CentOS Stream, the default user for PHP-FPM is set to ‘Apache’, which is not compatible with Nginx setups. To optimize PHP-FPM for Nginx, a configuration change is necessary.

Accessing PHP-FPM Configuration

Start by opening the PHP-FPM configuration file. This file contains settings specific to how PHP-FPM interacts with your web server. Use the following command to edit the www.conf file:

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

This command launches the Nano text editor with the PHP-FPM configuration file.

Modifying User and Group Settings

In the configuration file, locate the lines specifying the user and group. By default, these are set to ‘Apache’. Change both the user and group to ‘Nginx’ to align the PHP-FPM service with Nginx:

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

Here is a working visual examples:

Saving and Exiting the Configuration File

After making the changes, save the file by pressing CTRL+O and then exit with CTRL+X. This action saves the modifications and closes the Nano editor.

Restarting PHP-FPM Service

To apply the changes, restart the PHP-FPM service using:

sudo systemctl restart php-fpm

This command ensures that PHP-FPM starts running under the Nginx user and group, ensuring compatibility and enhanced security for your Nginx web server setup on CentOS Stream.

Example Nginx PHP-FPM Server Block Code

Configuring Nginx Server Block for PHP Processing

To enable PHP processing in Nginx on CentOS Stream, the server block within the Nginx configuration file must be modified. The following example demonstrates the necessary configuration for handling PHP files. This setup requires specifying the PHP location block.

    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;
    }

This code ensures that Nginx correctly handles PHP files, redirecting them to the PHP-FPM processor. Here is a quick breakdown:

  • location ~ .php$: This directive tells Nginx to apply the following rules to any file ending in .php. The tilde ~ indicates that this is a regular expression match.
  • try_files $uri =404: This line checks if the PHP file exists at the specified URI. If it doesn’t, Nginx returns a 404 error. This is a security measure to prevent unauthorized script execution.
  • fastcgi_pass unix:/run/php-fpm/www.sock;: This directive specifies the socket where the PHP-FPM service is listening. In this case, Nginx passes the PHP requests to the PHP-FPM process through the /run/php-fpm/www.sock socket.
  • fastcgi_index index.php;: This sets index.php as the default script to be executed when a directory is accessed.
  • fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;: This line sets the SCRIPT_FILENAME parameter, which is essential for PHP-FPM to find the script file on the filesystem. It combines the document root with the script name.
  • include fastcgi_params;: This includes the default FastCGI parameters provided by Nginx. These are standard settings required for PHP-FPM to function correctly.

This configuration ensures that Nginx correctly handles PHP requests by passing them to the PHP-FPM service for processing, a crucial step for running PHP-based websites and applications on a Nginx server.

Verifying Nginx Configuration

After updating the server block, verify the Nginx configuration for any syntax errors with the following command:

sudo nginx -t

Example output confirming a successful syntax check:

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

This step ensures that your Nginx configuration is free from syntax errors and ready to implement.

Restarting Nginx Service

To apply the PHP-FPM configuration changes, restart the Nginx service:

sudo systemctl restart nginx

This restart makes the changes effective, allowing Nginx to process PHP files using the updated server block configuration.

Conclusion

By installing PHP through the Remi RPM repository on CentOS Stream 9, you ensure that your system is equipped with the latest stable versions of PHP, including 8.3, 8.2, and 8.1. This setup provides you with enhanced performance, access to the latest features, and improved security for your web applications. Regularly updating PHP through the Remi repository will keep your system secure and up-to-date with the latest developments in PHP. Enjoy the flexibility and power that PHP offers for building and maintaining dynamic web applications.

Leave a Comment