How to Install PHP on Rocky Linux

PHP is a widely used scripting language for web development, powering content management systems like WordPress, e-commerce platforms like Magento, and frameworks like Laravel. Rocky Linux provides PHP through its AppStream repository, offering multiple versions through the DNF module system. By the end of this guide, you will have a working PHP installation configured for either Apache or Nginx, with common extensions ready for web application deployment.

PHP is available from the default AppStream repositories on Rocky Linux 8, 9, and 10. The installation method differs between releases: Rocky Linux 10 dropped modularity and provides PHP 8.3 directly, while Rocky 8 and 9 require enabling a module stream before installation. No third-party repositories are needed.

Choose Your PHP Version

Rocky Linux provides different PHP versions depending on your release. The following table summarizes what each version offers through the default AppStream repository:

Rocky VersionPHP Versions AvailableInstallation MethodDefault PHP
Rocky Linux 108.3Direct DNF install8.3
Rocky Linux 98.1, 8.2, 8.3DNF module streamsNone (must enable)
Rocky Linux 87.2, 7.3, 7.4, 8.0, 8.2DNF module streams7.2

For most users, choose the newest PHP version available for your Rocky Linux release. On version 10, you get PHP 8.3 automatically. If you run version 9, enable PHP 8.3 for the latest features and security updates. For version 8 users, PHP 8.2 is the newest option, though PHP 8.3 requires upgrading to Rocky 9 or 10.

Update Your System

Before installing PHP, update your Rocky Linux system to ensure all packages and security patches are current:

sudo dnf upgrade --refresh

This command refreshes the repository metadata and upgrades all installed packages. Depending on your system, this may take a few minutes.

Install PHP on Rocky Linux 10

Since Rocky Linux 10 dropped the module system, PHP installation is straightforward. PHP 8.3 is available directly from the AppStream repository without needing to enable any module streams first.

Install PHP for Apache

If you plan to use PHP with the Apache web server, install the base PHP package along with the command-line interface:

sudo dnf install php php-cli -y

This command installs PHP configured to work with Apache’s mod_php module. As a result, Apache processes PHP files directly within its own process.

Install PHP for Nginx

Nginx requires PHP-FPM (FastCGI Process Manager) because Nginx cannot process PHP directly. Install PHP-FPM along with the CLI tools:

sudo dnf install php-fpm php-cli -y

Unlike Apache, PHP-FPM runs as a separate service that Nginx communicates with through a Unix socket. This architecture provides better performance under high load compared to mod_php.

Verify PHP Installation

After installation completes, verify that PHP is working correctly by checking the version:

php -v

You should see output similar to the following:

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

This output confirms PHP 8.3 is installed and includes the Zend OPcache extension for improved performance.

Install PHP on Rocky Linux 9

Rocky Linux 9 uses the DNF module system to provide multiple PHP versions. Before installing PHP, you must enable the module stream for your desired version.

List Available PHP Versions

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

dnf module list php

In the output below, you can see the available module streams:

Rocky Linux 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 [d], devel, minimal PHP scripting language

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

Rocky Linux 9 provides PHP 8.1, 8.2, and 8.3. Since no stream is enabled by default, you must explicitly enable one before installation.

Enable Your Preferred PHP Version

Enable the PHP version you want to install. For the latest features and security updates, enable PHP 8.3:

sudo dnf module enable php:8.3 -y

Alternatively, if your application requires a specific version, enable that stream instead:

# For PHP 8.2
sudo dnf module enable php:8.2 -y

# For PHP 8.1
sudo dnf module enable php:8.1 -y

Install PHP Packages

Once the module stream is enabled, install PHP for your web server. For Apache:

sudo dnf install php php-cli -y

For Nginx with PHP-FPM:

sudo dnf install php-fpm php-cli -y

Then, verify the installation by checking the PHP version as shown in the Rocky Linux 10 section above.

Install PHP on Rocky Linux 8

Rocky Linux 8 also uses the DNF module system, but with a different set of available PHP versions. The default stream is PHP 7.2, which has reached end-of-life. For production use, enable a newer version.

List Available PHP Versions

Check the available PHP module streams:

dnf module list php
Rocky Linux 8 - AppStream
Name Stream  Profiles                   Summary
php  7.2 [d] common [d], devel, minimal PHP scripting language
php  7.3     common [d], devel, minimal PHP scripting language
php  7.4     common [d], devel, minimal PHP scripting language
php  8.0     common [d], devel, minimal PHP scripting language
php  8.2     common [d], devel, minimal PHP scripting language

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

PHP 8.3 is not available on Rocky Linux 8 through the default repositories. If you need PHP 8.3, consider upgrading to Rocky Linux 9 or 10.

Enable PHP 8.2

For the newest PHP version available on Rocky 8, enable the 8.2 stream:

sudo dnf module enable php:8.2 -y

Install PHP Packages

After enabling the module, install PHP for your web server. For Apache:

sudo dnf install php php-cli -y

For Nginx with PHP-FPM:

sudo dnf install php-fpm php-cli -y

Then, verify the installation to confirm PHP 8.2 is active:

php -v
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 PHP Extensions

The base PHP installation already includes essential modules like cURL, JSON, OpenSSL, and XML. However, most web applications require additional extensions for database connectivity, image processing, and caching. Therefore, install extensions based on your application’s requirements rather than installing everything.

Common Extensions for Web Applications

The following command installs commonly needed extensions for WordPress, Laravel, and similar PHP applications:

sudo dnf install php-gd php-intl php-bcmath php-soap php-ldap php-mysqlnd php-pgsql php-pecl-apcu php-pecl-zip php-mbstring php-pdo php-xml

Here is what each extension provides:

  • php-gd: Image manipulation for thumbnails, watermarks, and captchas
  • php-intl: Internationalization support for multi-language applications
  • php-bcmath: Arbitrary precision mathematics for financial calculations
  • php-soap: SOAP protocol support for web services
  • php-ldap: LDAP directory service connectivity
  • php-mysqlnd: Native MySQL/MariaDB database driver
  • php-pgsql: PostgreSQL database connectivity
  • php-pecl-apcu: User-level object caching for improved performance
  • php-pecl-zip: ZIP archive creation and extraction
  • php-mbstring: Multi-byte string handling for UTF-8 and international characters
  • php-pdo: Database abstraction layer for portable database code
  • php-xml: XML parsing and DOM manipulation

Redis Extension for Caching

If you use Redis for session storage or object caching, install the Redis extension. This package is available on Rocky Linux 9 and 10, but not included in the default Rocky Linux 8 repositories:

# Rocky Linux 9 and 10
sudo dnf install php-pecl-redis6

View Loaded Modules

After installing extensions, verify which modules are loaded:

php -m

This command lists all enabled PHP modules. To filter for specific modules, use grep:

php -m | grep -i mysql
mysqli
mysqlnd
pdo_mysql

PHP Development Tools

For PHP extension development or debugging, install the development package and Xdebug:

sudo dnf install php-devel php-pecl-xdebug3

These packages are intended for development environments only. Therefore, avoid installing Xdebug on production servers as it significantly impacts performance.

Configure PHP-FPM for Nginx

If you installed PHP-FPM for use with Nginx, you need to adjust the pool configuration. By default, PHP-FPM runs as the Apache user, which does not work correctly with Nginx.

Edit the PHP-FPM Pool Configuration

Open the default pool configuration file:

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

Then, find the user and group directives near the top of the file:

user = apache
group = apache

Then, change both values to nginx:

user = nginx
group = nginx

Save the file by pressing Ctrl+O, then Enter, and exit with Ctrl+X.

Start and Enable PHP-FPM

Start the PHP-FPM service and enable it to start automatically at boot:

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

Then, verify the service is running:

sudo systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; preset: disabled)
     Active: active (running) since Sat 2026-01-10 05:10:00 UTC; 5s ago
   Main PID: 1234 (php-fpm)
     Status: "Ready to handle connections"
      Tasks: 6 (limit: 23567)
     Memory: 12.5M
        CPU: 45ms
     CGroup: /system.slice/php-fpm.service
             ├─1234 "php-fpm: master process (/etc/php-fpm.conf)"
             └─1235 "php-fpm: pool www"

Configure Nginx Server Block for PHP

Add the following location block to your Nginx server configuration to process PHP files:

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

After editing your Nginx configuration, test the syntax before reloading:

sudo nginx -t
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

Switch PHP Versions

On Rocky Linux 8 and 9, you can switch between PHP versions by resetting and re-enabling a different module stream. This is useful when upgrading your application or testing compatibility.

Reset and Switch Module Stream

First, remove the currently installed PHP packages:

sudo dnf remove php php-cli php-fpm php-common

Next, reset the PHP module to clear the enabled stream:

sudo dnf module reset php

Finally, enable the new PHP version and reinstall:

sudo dnf module enable php:8.3 -y
sudo dnf install php php-cli php-fpm php-common -y

Once complete, verify the new version is active:

php -v

Remove PHP

If you need to remove PHP from your system, follow these steps to clean up all related packages and reset the module state.

Remove PHP Packages

Start by removing the core PHP packages and any extensions you installed:

sudo dnf remove php php-cli php-fpm php-common

Clean Up Unused Dependencies

Next, remove orphaned dependencies that were installed alongside PHP:

sudo dnf autoremove

Reset PHP Module

On Rocky Linux 8 and 9, reset the PHP module to remove the stream association:

sudo dnf module reset php

This step is not needed on Rocky Linux 10 since it does not use the module system for PHP.

Troubleshooting

Module Stream Conflicts

If you see an error about conflicting module streams when trying to install PHP, reset the module first:

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

PHP-FPM Fails to Start

If PHP-FPM fails to start, check the configuration syntax:

php-fpm -t

Additionally, review the systemd journal for detailed error messages:

journalctl -xeu php-fpm

Common issues include permission problems on the socket file or syntax errors in pool configuration files.

Missing Extensions

If your application reports missing PHP extensions, search for the package name:

dnf search php- | grep -i extension-name

Some extensions use the php-pecl- prefix. For example, the Redis extension is php-pecl-redis6 on Rocky Linux 9 and 10.

Conclusion

You now have PHP installed on Rocky Linux using the default AppStream repositories. Rocky Linux 10 users have PHP 8.3 with direct installation, while Rocky 8 and 9 users can select their preferred version through DNF modules. With PHP-FPM configured for Nginx or the base installation working with Apache, you can deploy PHP applications, install additional extensions as needed, and switch versions when your requirements change.

2 thoughts on “How to Install PHP on Rocky Linux”

  1. Im getting the following errors on rockyos 9.5

    Error:
    Problem 1: package php-pecl-memcached-3.3.0-1.el9.x86_64 from epel requires php(api) = 20200930-64, but none of the providers can be installed
    – package php-pecl-memcached-3.3.0-1.el9.x86_64 from epel requires php(zend-abi) = 20200930-64, but none of the providers can be installed
    – conflicting requests
    – package php-common-8.0.30-10.el9.remi.x86_64 from remi-modular is filtered out by modular filtering
    – package php-common-8.0.30-11.el9.remi.x86_64 from remi-modular is filtered out by modular filtering
    – package php-common-8.0.30-1.el9_2.x86_64 from appstream is filtered out by modular filtering
    Problem 2: package php-pecl-memcache-8.2-1.el9.x86_64 from epel requires php(api) = 20200930-64, but none of the providers can be installed
    – package php-pecl-memcache-8.2-1.el9.x86_64 from epel requires php(zend-abi) = 20200930-64, but none of the providers can be installed
    – conflicting requests
    – package php-common-8.0.30-10.el9.remi.x86_64 from remi-modular is filtered out by modular filtering
    – package php-common-8.0.30-11.el9.remi.x86_64 from remi-modular is filtered out by modular filtering
    – package php-common-8.0.30-1.el9_2.x86_64 from appstream is filtered out by modular filtering
    (try to add ‘–skip-broken’ to skip uninstallable packages or ‘–nobest’ to use not only best candidate packages)

    Reply
    • Hi Allen,

      Thank you for reporting the issue! The errors you’re encountering typically occur due to conflicts in PHP version dependencies or filtering in the enabled repositories. Here’s how you can resolve this:

      1. Check Your PHP Module and Repositories:
        First, ensure that the desired PHP version is enabled. Run:
        dnf module list php

        If the required version is not enabled, enable it. For example, for PHP 8.1:

        sudo dnf module enable php:remi-8.1 -y
      2. Disable Modular Filtering:
        The error suggests modular filtering is blocking some packages. You can temporarily disable this by appending the --setopt=modularity_filter=false option:
        sudo dnf install php-pecl-memcached --setopt=modularity_filter=false
      3. Use the ‘–nobest’ Option:
        This allows DNF to consider packages that are not the best candidate but can resolve the dependency issue:
        sudo dnf install php-pecl-memcached --nobest
      4. Ensure Dependencies Are Installed:
        Some missing dependencies might need to be installed manually. Run:
        sudo dnf install php-common php-zend-abi php-api

        Replace these with the exact dependencies as indicated in the error messages.

      5. Skip Broken Packages:
        To bypass any unresolvable packages, you can add the --skip-broken flag:
        sudo dnf install php-pecl-memcached --skip-broken

      These steps should help resolve the conflict. If you still face issues, let me know the exact output, and I’ll assist you further!

      Reply

Leave a Comment

Let us know you are human: