CentOSRHEL Based

How To Install PHP on CentOS Stream 10

Install PHP on CentOS Stream 10

PHP is a widely-used open-source scripting language that is especially suited for web development. With the release of PHP 8.4, developers gain access to new features and enhancements that improve performance, security, and developer experience. This guide will walk you through the detailed steps to install PHP 8.4 on CentOS Stream 10, ensuring that you can leverage the latest capabilities of this powerful programming language.

Prerequisites

Before diving into the installation process, ensure that your system meets the following prerequisites:

  • System Requirements: A running instance of CentOS Stream 10.
  • Access to Terminal: You need to have access to a terminal or command line interface.
  • Root or Sudo Privileges: Ensure you have administrative rights to install software.

Additionally, it’s advisable to back up any existing configurations and data to prevent potential data loss during the installation process. A stable internet connection is also necessary for downloading packages.

Step 1: Update Your System

Keeping your system updated is crucial for security and compatibility. Start by updating your system’s package index and installed packages with the following command:

sudo dnf update -y

This command ensures that all existing packages are up-to-date, which can help avoid conflicts during the installation of PHP 8.4.

Step 2: Enable the EPEL and Remi Repositories

The EPEL (Extra Packages for Enterprise Linux) repository provides additional packages for CentOS, while the Remi repository specializes in PHP packages. Enabling these repositories is essential for accessing PHP 8.4 and its extensions.

What are EPEL and Remi Repositories?

EPEL is a repository maintained by the Fedora Project that offers high-quality add-on packages for Enterprise Linux distributions. The Remi repository, created by Remi Collet, contains up-to-date versions of PHP and its extensions.

Commands to Enable Repositories

Run the following commands to enable both repositories:

sudo dnf install epel-release -y
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-10.rpm -y

This will allow you to access a wide range of PHP versions and extensions directly from your package manager.

Step 3: Enable PHP 8.4 Module

CentOS Stream uses a modular approach for managing different versions of software. To install PHP 8.4, you need to enable its module first.

Understanding PHP Modules

A module allows users to switch between different versions of a software package easily. This modularity is beneficial for developers who may need specific versions for different projects.

Commands to Enable PHP 8.4

Execute the following commands to reset any existing PHP modules and enable PHP 8.4:

sudo dnf module reset php
sudo dnf module enable php:remi-8.4

This process ensures that your system is prepared to install PHP 8.4 without interference from other versions.

Step 4: Install PHP 8.4

You are now ready to install PHP 8.4 along with some commonly used extensions that enhance its functionality.

Installation Command

The following command will install PHP 8.4 along with several essential extensions:

sudo dnf install php php-cli php-fpm php-json php-common php-mysqlnd php-zip php-gd php-mbstring php-curl php-xml php-pecl-bcmath -y

This command installs the core PHP package along with various extensions that are often required for web applications, such as database connectivity and image processing capabilities.

Step 5: Verify Installation

After installation, it’s important to verify that PHP is correctly installed and functioning as expected.

Check Installed Version

You can check the installed version of PHP by running:

php -v

The output should display information about PHP version 8.4, confirming that the installation was successful.

Step 6: Configure PHP-FPM

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation that provides significant improvements over traditional CGI methods.

What is PHP-FPM?

This service manages pools of processes that handle requests from web servers efficiently, improving performance especially under high load conditions.

Commands to Start and Enable PHP-FPM

You can start the PHP-FPM service and ensure it runs on system boot with these commands:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm
sudo systemctl status php-fpm

The last command checks the status of the service, allowing you to confirm that it is running properly.

Step 7: Configure Web Server

Your web server needs to be configured to work with PHP-FPM effectively. Below are instructions for both Apache and Nginx users.

Integrating with Apache

    • If you haven’t installed Apache yet, do so using:
sudo dnf install httpd -y
    • Edit the Apache configuration file (usually located at /etc/httpd/conf/httpd.conf) to include the following lines:
AddType application/x-httpd-php .php
    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch
    • You may also need to configure Apache to communicate with PHP-FPM by adding a ProxyPass directive or similar configuration based on your setup.
    • Finally, restart Apache:
sudo systemctl restart httpd

Integrating with Nginx

    • If you haven’t installed Nginx yet, do so using:
sudo dnf install nginx -y
    • Edit your Nginx server block configuration file (usually located in /etc/nginx/conf.d/ or /etc/nginx/sites-available/) and include these lines within your server block:
location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/www.sock; # Adjust if necessary
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    • This configuration tells Nginx how to handle requests for .php files using PHP-FPM.
    • Restart Nginx after making changes:
sudo systemctl restart nginx

Troubleshooting Common Issues

If you encounter issues during or after installation, consider these common problems and their solutions:

  • Error: “PHP not found”: Ensure that your PATH variable includes the directory where PHP is installed or check if it was installed correctly by running `php -v`.
  • Error: “Failed to start php-fpm.service”: Check logs at `/var/log/php-fpm/error.log` for error messages indicating what went wrong during startup.
  • Error: “502 Bad Gateway: This could indicate an issue with Nginx not connecting properly to PHP-FPM; verify your configuration settings in Nginx as outlined above.
  • Error: “Permission denied” errors in logs: Ensure proper permissions are set on your web root directory and files being accessed by the web server user (e.g., `www-data` for Nginx).

Congratulations! You have successfully installed PHP. Thanks for using this tutorial for installing PHP scripting language on CentOS Stream 10 system. For additional help or useful information, we recommend you check the official PHP website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button