How to Install PHP on CentOS Stream

PHP is a server-side scripting language designed for web development that powers millions of websites worldwide, from personal blogs to enterprise applications. Whether you need to run WordPress, Laravel, or custom PHP applications, CentOS Stream provides PHP directly through its AppStream repository with multiple version options. By the end of this guide, you will have a working PHP installation configured for either Apache or Nginx, with common extensions installed and verified.

Version Notice: CentOS Stream 9 uses the DNF module system to offer PHP 8.1, 8.2, and 8.3. CentOS Stream 10 provides PHP 8.3 directly without modules. This guide covers both versions with version-specific commands where needed.

Choose Your PHP Version

CentOS Stream provides different PHP versions depending on your release. The following table summarizes what each version offers and helps you decide which to install.

CentOS Stream VersionAvailable PHP VersionsDefault VersionBest For
CentOS Stream 10PHP 8.38.3.xNew installations, latest features and security
CentOS Stream 9PHP 8.1, 8.2, 8.38.1.x (module default)Production servers needing version flexibility

For most users, PHP 8.3 is recommended as it offers the latest performance improvements, security patches, and language features. Only select an older version if your application specifically requires it for compatibility reasons.

Update Your System Before Installation

Before installing PHP, update your CentOS Stream system to ensure all existing packages are current. This step prevents dependency conflicts and ensures you receive the latest security patches.

sudo dnf upgrade --refresh

Once complete, this command refreshes the package metadata and upgrades all installed packages to their latest available versions.

Install PHP on CentOS Stream 10

CentOS Stream 10 provides PHP 8.3 directly through the AppStream repository without requiring module configuration. As a result, installation is straightforward using standard DNF commands.

Install PHP for Apache on CentOS Stream 10

If you are using Apache HTTPD on CentOS Stream, install PHP with the CLI component using the following command:

sudo dnf install php php-cli -y

As a result, this installs the PHP module for Apache along with the command-line interface, which is useful for running PHP scripts from the terminal.

Install PHP-FPM for Nginx on CentOS Stream 10

For Nginx deployments, you need PHP-FPM (FastCGI Process Manager) instead of the Apache module. PHP-FPM handles PHP requests efficiently and is the standard approach for Nginx:

sudo dnf install php-fpm php-cli -y

Verify PHP Installation on CentOS Stream 10

Once installation completes, verify that PHP is correctly installed by checking the version:

php -v

You should see output similar to the following, confirming a successful installation:

PHP 8.3.26 (cli) (built: Sep 23 2025 17:57:26) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.3.26, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.26, Copyright (c), by Zend Technologies

Install PHP on CentOS Stream 9

Unlike CentOS Stream 10, CentOS Stream 9 uses the DNF module system to provide multiple PHP versions. Before installing PHP, you must first enable the desired version stream. As a result, this approach allows you to switch between versions if needed.

List Available PHP Modules on CentOS Stream 9

First, check which PHP versions are available in the AppStream repository:

dnf module list php

You should see output showing available PHP module streams:

CentOS Stream 9 - AppStream
Name Stream Profiles                   Summary               
php  8.1    common [d], devel, minimal PHP scripting language
php  8.2    common [d], devel, minimal PHP scripting language
php  8.3    common, devel, minimal     PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

Enable Your Desired PHP Version

Next, enable the PHP version you want to install. Choose one of the following commands based on your requirements:

For PHP 8.3 (Recommended):

sudo dnf module enable php:8.3 -y

Alternatively, PHP 8.2:

sudo dnf module enable php:8.2 -y

Or PHP 8.1:

sudo dnf module enable php:8.1 -y

The -y flag automatically confirms the operation. After the command completes, you will see output similar to:

Enabling module streams:
 php                              8.3

Transaction Summary
================================================================================

Complete!

With the module stream enabled, you can now proceed with installation.

Install PHP for Apache on CentOS Stream 9

Now that the module is enabled, install PHP for Apache using:

sudo dnf install php php-cli -y

Install PHP-FPM for Nginx on CentOS Stream 9

Alternatively, for Nginx deployments, install PHP-FPM instead:

sudo dnf install php-fpm php-cli -y

Verify PHP Installation on CentOS Stream 9

Confirm the installation by checking the PHP version:

php -v

You should see output similar to the following (version varies based on your selection):

PHP 8.2.28 (cli) (built: Mar 11 2025 17:58:12) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.2.28, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.28, Copyright (c), by Zend Technologies

Install Common PHP Extensions

Most PHP applications require additional extensions beyond the base installation. The following command installs commonly needed extensions for CMS platforms like WordPress and frameworks like Laravel:

sudo dnf install php-mysqlnd php-gd php-opcache php-intl php-bcmath php-xml php-mbstring php-pdo php-pecl-apcu php-pecl-zip -y

Here is what each extension provides:

  • php-mysqlnd — MySQL native driver for database connections
  • php-gd — Graphics library for image manipulation
  • php-opcache — Bytecode caching for improved performance
  • php-intl — Internationalization functions
  • php-bcmath — Arbitrary precision mathematics
  • php-xml — XML parsing and manipulation
  • php-mbstring — Multi-byte string handling
  • php-pdo — Database abstraction layer
  • php-pecl-apcu — User cache for storing data in memory
  • php-pecl-zip — ZIP archive handling

Review this list and remove any extensions your application does not require. Installing unnecessary extensions consumes memory and may introduce security considerations.

View Loaded PHP Modules

To verify which extensions are currently loaded, use the following command:

php -m

As a result, this command displays all active PHP modules. The output includes both core modules and any extensions you installed:

[PHP Modules]
bcmath
Core
curl
date
dom
gd
hash
intl
json
mbstring
mysqlnd
opcache
pdo_mysql
xml
zlib

[Zend Modules]
Zend OPcache

If a required extension is missing from the list, install it using sudo dnf install php-<extension>.

Install PHP Development Tools

For development environments that require debugging or code coverage analysis, install the following tools:

sudo dnf install php-devel php-pecl-xdebug3 -y

Specifically, the php-devel package provides headers and tools for building PHP extensions, while php-pecl-xdebug3 enables step debugging and profiling. However, only install these on development systems, as they add overhead unsuitable for production.

Configure PHP-FPM for Nginx

If you installed PHP-FPM for use with Nginx, you need to adjust the configuration to use the correct user and group. By default, PHP-FPM runs as the apache user, which does not match Nginx’s configuration.

Edit PHP-FPM Pool Configuration

Open the PHP-FPM pool configuration file:

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

Locate the user and group directives near the top of the file and change them from apache to nginx:

; Unix user/group of processes
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

; Set socket ownership (important for Nginx access)
listen.owner = nginx
listen.group = nginx

Save the file by pressing CTRL+O, then exit with CTRL+X.

Start and Enable PHP-FPM Service

After modifying the configuration, start PHP-FPM and enable it to start automatically on boot:

sudo systemctl enable --now php-fpm

Next, verify the service is running:

sudo systemctl status php-fpm --no-pager

You should see output showing the service is active:

● php-fpm.service - The PHP FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; preset: disabled)
     Active: active (running) since Fri 2025-12-12 11:30:00 UTC; 5s ago
   Main PID: 12345 (php-fpm)
     Status: "Ready to handle connections"

Configure Nginx Server Block for PHP

To process PHP files, add the following location block to your Nginx server configuration:

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

In effect, this configuration tells Nginx to pass PHP requests to the PHP-FPM socket. Here is what each directive accomplishes:

  • location ~ \.php$ — Matches any request ending in .php
  • try_files $uri =404 — Returns 404 if the requested PHP file does not exist, preventing unauthorized script execution
  • fastcgi_pass — Specifies the PHP-FPM socket location
  • fastcgi_param SCRIPT_FILENAME — Tells PHP-FPM where to find the script file

Test and Reload Nginx

After editing the Nginx configuration, test for syntax errors:

sudo nginx -t

You should see output confirming valid configuration:

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

If the test passes, reload Nginx to apply the changes:

sudo systemctl reload nginx

Troubleshoot PHP Installation Issues

Module Stream Already Enabled Error

If you try to enable a different PHP version after one is already enabled, you may see an error. To switch PHP versions on CentOS Stream 9, first reset the module, then enable the new version:

sudo dnf module reset php -y
sudo dnf module enable php:8.3 -y
sudo dnf distro-sync -y

Finally, the distro-sync command ensures all PHP packages align with the newly enabled stream.

PHP-FPM Fails to Start

If PHP-FPM fails to start, check the journal for error details:

sudo journalctl -xeu php-fpm --no-pager | tail -20

Common causes include incorrect user/group settings in /etc/php-fpm.d/www.conf or permission issues on the socket directory. Verify the nginx user exists:

id nginx

In that case, you may need to install Nginx first or adjust PHP-FPM to use a different user.

Missing PHP Extensions

When your application reports missing extensions, search for available packages:

dnf search php- | grep -i "extension-name"

Replace extension-name with the extension you need. Some extensions may require EPEL repository for additional packages not included in the base AppStream.

Remove PHP from CentOS Stream

To remove PHP from your system, use the following commands based on your CentOS Stream version.

Warning: The php* wildcard pattern removes all PHP-related packages. Before proceeding, ensure you have backed up any custom PHP configurations or application data that depends on these packages.

Remove PHP on CentOS Stream 10

To remove all PHP packages and their dependencies, run the following commands:

sudo dnf remove php* -y
sudo dnf autoremove -y

Remove PHP on CentOS Stream 9

In addition to removing packages, reset the module stream:

sudo dnf remove php* -y
sudo dnf module reset php -y
sudo dnf autoremove -y

Additionally, the autoremove command removes orphaned dependencies that were installed alongside PHP but are no longer needed.

Verify PHP Removal

Finally, confirm that PHP is no longer installed:

php -v

You should see the following output confirming removal:

bash: php: command not found

Conclusion

You now have PHP installed and configured on CentOS Stream using the official AppStream repository. The module system on CentOS Stream 9 provides flexibility to choose between PHP 8.1, 8.2, and 8.3, while CentOS Stream 10 delivers PHP 8.3 directly. With common extensions installed and PHP-FPM configured for Nginx, your system is ready to host PHP applications. For production deployments, consider configuring PHP’s memory limits and timeout values in /etc/php.ini based on your application’s requirements.

Leave a Comment