How to Install PHP on CentOS Stream 9 or 8

This guide will explain how to install PHP on CentOS Stream 9 or 8 using the command-line terminal. It will utilize the Remi PHP RPM repository for accessing the most recent version and for future updates on your system.

PHP, a powerful and versatile scripting language, plays a crucial role in web development, especially on Linux systems like CentOS Stream. In this guide, we will demonstrate how to install PHP on CentOS Stream 9 or 8, catering to a wide range of audiences, including users, developers, system administrators, and website owners. PHP’s adaptability makes it an ideal choice for various web-based applications, and its compatibility with CentOS Stream enhances performance and reliability.

Key Features of PHP on CentOS Stream:

  • Flexibility: PHP supports a wide range of databases and web servers, offering versatility in web development.
  • Performance: Optimized for speed, PHP ensures efficient execution of scripts on CentOS Stream.
  • Security: Regular updates and a vast community contribute to a robust security framework.
  • Ease of Use: With straightforward syntax, PHP is accessible for beginners while offering advanced features for experienced developers.
  • Open Source: Free to use and modify, PHP aligns with the open-source nature of CentOS Stream.

The process of installing PHP on CentOS Stream is streamlined and user-friendly, ensuring that even those new to Linux can successfully integrate PHP into their web environments. This guide will provide step-by-step instructions, ensuring a smooth setup process.

Import Remi PHP RPM on CentOS

Step 1: 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.

Step 2: Importing Remi PHP RPM Repository on CentOS Stream

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.

Importing Remi PHP Repository for CentOS 9 Stream

For CentOS 9 Stream users, 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

Importing Remi PHP Repository for CentOS 8 Stream

For CentOS 8 Stream, start by enabling the PowerTools repository. This repository is crucial as it provides development and debugging tools needed for various packages.

Enable PowerTools with:

sudo dnf config-manager --set-enabled powertools

Then, install the EPEL repository 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, import the Remi PHP repository for EL8:

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

These steps ensure that your CentOS Stream system has access to the latest PHP versions through the Remi repository. Each command is tailored to the specific version of CentOS Stream you are using, ensuring compatibility.

Enabling PHP Remi Repository on CentOS Stream

Step 1: Listing Available PHP Modules

Before proceeding with the installation of PHP, it’s essential to identify the available PHP versions in the Remi repository. This step helps in making an informed decision about which PHP 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.

Screenshot of PHP modules from Remi repo on CentOS Stream.
Visual representation of PHP modules available from the Remi repository on CentOS Stream.

Step 2: 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.

Step 3: Activating Your Desired PHP Version

After confirming the available PHP versions, you can proceed to enable the specific version of PHP that fits your needs. It’s advisable to select a stable and well-supported version of PHP for your projects. PHP versions 8.1 and 8.2 are generally recommended for their balance of features and stability. PHP 8.3 is also an option, though it might be relatively new and less tested in some environments.

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
sudo dnf module enable php:remi-8.0 -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, 8.1, or 8.0 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

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:

Screenshot locating Apache user in PHP-FPM config on CentOS Stream 9 or 8.
Discovering the Apache user within the PHP-FPM configuration on CentOS Stream 9 or 8.
Screenshot of switching to Nginx user from Apache in PHP-FPM on CentOS Stream 9 or 8.
Modifying the PHP-FPM configuration to utilize the Nginx user instead of Apache on CentOS Stream 9 or 8.

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 on CentOS Stream

Configuring Nginx Server Block for PHP Processing

To enable PHP processing in Nginx on CentOS Stream, it’s essential to modify the server block within the Nginx configuration file. 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 is ready to be implemented.

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

So, we’ve just wrapped up installing PHP on CentOS Stream, tweaking it to play nice with both Apache and Nginx. We made sure PHP-FPM and Nginx are like best buddies and tweaked those server blocks and PHP extensions to create a pretty solid setup for your PHP apps. Here’s a heads-up: always keep your system and PHP up-to-date to stay on top of security and keep things running smoothly.

Remember, it’s a good idea to give your PHP modules and settings a check-up now and then to keep your server in top shape. With this setup, you’re all set to host some awesome dynamic websites and apps, striking a nice balance between being flexible and reliable.

Leave a Comment


Your Mastodon Instance
Share to...