How to Install PHP Rocky Linux 9 or 8

This guide will demonstrate how to install PHP on Rocky Linux 9 or 8 using the command-line terminal and Remi’s RPM PHP repository, ensuring access to the latest version and future upgrades.

PHP is a widely-used scripting language, pivotal in web development for its versatility and efficiency. It’s the backbone of many content management systems (CMS), including the popular WordPress, enabling dynamic website functionality. PHP’s compatibility with various databases and its capacity for handling complex operations make it a preferred choice for developers.

Below are key features that underscore PHP’s significance:

  • Ease of Use: PHP’s syntax is clear and intuitive, making it approachable for beginners yet powerful for advanced users.
  • Flexibility: It seamlessly integrates with various databases and is compatible with multiple operating systems.
  • Performance: With PHP-FPM (FastCGI Process Manager), PHP delivers enhanced performance for server-side scripting, which is crucial for CMS-based websites with Nginx.
  • Scalability: PHP can handle a wide range of web applications, from small websites to large-scale, high-traffic sites.
  • Community Support: A vast community of developers contributes to continuous improvements and extensive documentation.

The following sections delve into the technical aspects of installing PHP on Rocky Linux, providing a straightforward, step-by-step approach for developers and system administrators.

Import Remi PHP RPM Repo on Rocky Linux

Update Rocky Linux Before PHP Installation

Begin by updating your Rocky Linux system to prepare for PHP installation. Open the terminal and enter:

sudo dnf upgrade --refresh

Import PHP Remi Repository on Rocky Linux

EPEL Repository Setup

EPEL (Extra Packages for Enterprise Linux) is crucial for additional software packages in Rocky Linux. Below are two options to install EPEL on Rocky Linux for the latest EL9 release or EL8.

Import EPEL for Rocky Linux 9

Enable CRB

CRB provides access to more development tools. Use:

sudo dnf config-manager --set-enabled crb
Install EPEL Repositories

To get the latest EPEL releases for Rocky Linux 9:

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

Incorporate the Remi repository designed for EL 9:

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

Import EPEL for Rocky Linux 8

Enable PowerTools Repository

PowerTools is essential for PHP development tools.

sudo dnf config-manager --set-enabled powertools
Enable EPEL for Rocky Linux 8

Install EPEL repositories for Rocky Linux 8:

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
Import Remi EL 8 Repository

With EPEL installed, import the Remi RPM containing PHP for EL 8:

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

Enable PHP (8.x) from Remi Repository on Rocky Linux

Verifying PHP Versions

Start by confirming the available PHP versions in the Remi repository. This ensures you have access to the desired PHP version.

Execute in the terminal:

dnf module list php

If prompted, import the GPG key for Remi’s repository by typing (Y) to proceed.

Selecting Your PHP Version

Enabling PHP 8.3

To install PHP 8.3, a stable and recent version, use:

Screenshot showing how to list PHP modules for PHP 8.3, 8.2, 8.1, 8.0 or 7.4 on Rocky Linux 9 or 8Pin
Example of listing available PHP modules on Rocky Linux 9 or 8 for various PHP versions.

Selecting Your PHP Version

Enable PHP 8.3 on Rocky Linux

To install PHP 8.3, a stable and recent version, use:

sudo dnf module enable php:remi-8.3 -y

Enable PHP 8.2 on Rocky Linux

For PHP 8.2, suitable for applications requiring this specific version:

sudo dnf module enable php:remi-8.2 -y

Enable PHP 8.1 on Rocky Linux

To enable PHP 8.1, ideal for compatibility with certain applications:

sudo dnf module enable php:remi-8.1 -y

Install PHP 8.3, 8.2, and 8.1 on Rocky Linux

Installing PHP for Apache and Nginx

After enabling the Remi PHP repository, you can install PHP tailored to your web server needs, either Apache or Nginx.

Apache (httpd) PHP Installation

sudo dnf install php php-cli -y

Nginx PHP Installation

sudo dnf install php-fpm php-cli -y

Verifying PHP Installation

Confirm PHP installation by running:

php -v

PHP Extensions Installation

Install essential PHP extensions based on your project needs. Avoid installing all extensions to maintain optimal performance and security.

Standard Installation

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

Simplified Installation

For a compact installation command:

sudo dnf install php-{cli,fpm,curl,mysqlnd,gd,opcache,zip,intl,common,bcmath,imagick,xmlrpc,json,readline,memcached,redis,mbstring,apcu,xml,dom,memcache}

Now, let’s break down what each of these PHP extensions does:

  • php-cli: Command Line Interface for PHP, helpful in running PHP scripts in the shell.
  • php-fpm: FastCGI Process Manager, useful for handling and serving PHP requests.
  • php-curl: Allows you to connect and communicate with different types of servers and protocols.
  • php-mysqlnd: MySQL Native Driver, used for MySQL database connections.
  • php-gd: Graphics Draw, useful for creating and manipulating image files.
  • php-opcache: Opcode Cache, used to speed up PHP execution.
  • php-zip: Allows you to work with zip archives.
  • php-intl: Internationalization extension for working with international character sets and locales.
  • php-common: Common files and settings used by multiple PHP modules.
  • php-bcmath: Binary Calculator allows arbitrary precision mathematics.
  • php-imagick: Provides methods to create and modify images using the ImageMagick library.
  • php-xmlrpc: Enables creating an XML-RPC server and makes sending XML-RPC requests easy.
  • php-json: JSON (JavaScript Object Notation) extension for encoding and decoding JSON data.
  • php-readline: Allows for reading a line of data from the terminal.
  • php-memcached: Provides fast, distributed memory object caching.
  • php-redis: PHP extension for interfacing with Redis.
  • php-mbstring: Multi-Byte String for handling non-ASCII strings.
  • php-apcu: APC User Cache, a userland caching extension based on APC.
  • php-xml: XML Parser for parsing XML documents.
  • php-dom: Document Object Model, to manipulate XML documents in an object-oriented way.
  • php-memcache: Provides handy procedural and object-oriented interface to memcached.
  • php-soap: Used for writing SOAP Servers and Clients.
  • php-ldap: Lightweight Directory Access Protocol used for accessing and managing directory information.
  • php-pdo: PHP Data Objects, a database access layer providing a uniform access method to multiple databases.

You can use the following command to view modules loaded at any time.

php -m

Regular monitoring and removal of unnecessary modules are recommended for performance efficiency.

Installing PHP Development Tools on Rocky Linux

For development purposes, install the PHP development branch and debugging tools:

PHP Development Branch

sudo dnf install php-devel

PHP Debugging Tools

sudo dnf install php-xdebug php-pcov

It’s important to note that installing this version will bring in multiple dependencies, and it’s not recommended to do so unless you have a specific need for it in your PHP development or any special requirement.

Configure PHP-FPM Nginx User on Rocky Linux

Adjusting PHP-FPM for Nginx

RHEL-based distributions like Rocky Linux default to using the “Apache” user for PHP-FPM, which is not suitable for Nginx setups. It’s essential to modify this for optimal functioning with Nginx.

Editing PHP-FPM Configuration

Open Configuration File

Access the www.conf file for editing:

sudo nano /etc/php-fpm.d/www.conf
Modify User and Group Settings

Change the Apache user and group to Nginx within the file.

Screenshot illustrating how to find the Apache username in the PHP-FPM configuration file on Rocky Linux 9 or 8 for PHP 8.3, 8.2, 8.1, 8.0 or 7.4Pin
Locating the Apache username in the PHP-FPM configuration file for different PHP versions on Rocky Linux.

Post modification:

Screenshot showing how to change the username to Nginx in PHP-FPM on Rocky Linux 9 or 8 for PHP 8.3, 8.2, 8.1, 8.0 or 7.4Pin
Step-by-step example of changing the PHP-FPM username to Nginx on Rocky Linux 9 or 8.

Save your changes by pressing (CTRL+O), then exit with (CTRL+X).

Restart PHP-FPM Service

After modifying the configuration, restart PHP-FPM to apply changes:

sudo systemctl restart php-fpm

Example Nginx PHP-FPM Server Block Code on Rocky Linux

Configuring Nginx for PHP Processing

To enable Nginx to handle PHP files, the server block must be configured correctly. The following example demonstrates the necessary setup within the server{} block for processing PHP files.

Nginx PHP-FPM 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;
    }

Validating Nginx Configuration

After updating the configuration, validate it to ensure there are no syntax errors.

sudo nginx -t

Expected output should confirm the configuration is correct:

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

Restarting Nginx Service

To apply the changes, restart the Nginx service:

sudo systemctl restart nginx

This restart is important for the changes to take effect and for Nginx to start processing PHP files with the new configuration.

Conclusion

Alright, that wraps up our journey! We’ve successfully set up PHP on Rocky Linux, tweaking it for Apache or Nginx and fine-tuning PHP-FPM for optimal performance. Remember, regularly checking your PHP modules and configurations keeps things running smoothly. And don’t forget to restart your services after any changes.

Leave a Comment


Your Mastodon Instance
Share to...