How to Install PHP 8.1 on AlmaLinux 9 or 8

This guide will demonstrate how to install PHP 8.1 on AlmaLinux 9 or 8 using the Remi RPM repository, whether you’re aiming for a fresh installation or upgrading to the latest version of PHP.

PHP 8.1, a significant update in the PHP series, was officially released in November 2021. This version brings a host of improvements and new features that enhance the overall performance and security of PHP applications. PHP, known for its flexibility and efficiency in web development, continues to evolve, offering developers a robust platform for creating dynamic web content.

Key Features of PHP 8.1:

  • Enumerations (Enums): Adds support for enum classes, allowing for more structured coding.
  • Readonly Properties: Increases data integrity by preventing accidental modifications.
  • Fibers: Introduces a lightweight method of managing concurrency.
  • New in Initializers: Enables the use of new expressions in property, constant, and parameter default values.
  • Performance Improvements: Enhancements in the JIT compiler lead to faster execution times.
  • Array Unpacking with String Keys: Provides more flexibility in array manipulation.
  • Deprecations and Type Changes: Continues the trend of tightening type systems for more reliable code.

The following sections of this guide will provide a step-by-step process for installing PHP 8.1 on AlmaLinux, leveraging the Remi repository for the most up-to-date version. We will cover prerequisites, installation steps, and post-installation checks to ensure a successful setup.

Import Remi PHP RPM for PHP 8.1 on AlmaLinux

Update AlmaLinux System

Begin by updating AlmaLinux to ensure all system packages are current. This step enhances the stability and security of your server.

sudo dnf upgrade --refresh

This command refreshes your package database and upgrades all installed packages to their latest versions.

Enable Repositories and Add Remi PHP RPM Repository to AlmaLinux

For AlmaLinux 9

Enable CRB Repository

The CodeReady Linux Builder (CRB) repository provides additional development tools and libraries, essential for PHP installation.

sudo dnf config-manager --set-enabled crb
Install EPEL Repository for EL9

Add the EPEL repository, offering a comprehensive range of additional packages for Enterprise Linux.

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
Add Remi PHP Repository for EL9

To access the latest PHP versions, install the Remi PHP repository.

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

For AlmaLinux 8

Enable PowerTools Repository

The PowerTools repository contains necessary development and debugging tools for PHP installation.

sudo dnf config-manager --set-enabled powertools
Install EPEL Repository for EL8

Incorporate the EPEL repository to extend the range of available packages on Enterprise Linux.

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
Integrate Remi PHP Repository for EL8

Finalize your setup by adding the Remi PHP repository, ensuring you have access to the latest PHP versions.

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

Install PHP 8.1 on AlmaLinux

Set PHP 8.1 as the Default Module

Initiate the installation by configuring PHP 8.1 as the default PHP version on your AlmaLinux system. Execute the command below to enable PHP 8.1 from the Remi repository:

sudo dnf module enable php:remi-8.1 -y

This step marks PHP 8.1 as the primary PHP version for your system. It ensures that all installations or updates of PHP packages will default to this version.

Enabling PHP 8.1 from Remi Repo in Terminal on AlmaLinux
Terminal Screenshot of Enabling PHP 8.1 on AlmaLinux

Install PHP 8.1 Using the DNF Command

After enabling the PHP 8.1 module, proceed with its installation. Enter the following command to install PHP 8.1:

sudo dnf install php

You’ll be prompted to import Remi’s RPM GPG keys during the installation. Ensure you confirm with a “Y”. This step installs PHP 8.1 on your AlmaLinux system, making it ready for use.

PHP 8.1 Installation Prompt in AlmaLinux Terminal
Confirmation Prompt for PHP 8.1 Installation

If you have an existing PHP installation (e.g., PHP 8.0) via Remi and wish to upgrade to PHP 8.1, use this command instead:

sudo dnf module switch-to php:remi-8.1/common

These commands set PHP 8.1 as the primary version for your system. They ensure that all installations or updates of PHP packages will default to this version.

Install PHP 8.1 Development Tools (Optional)

For developers needing extra tools and packages for PHP development, it’s advisable to install specific development packages.

Use this command to install essential development tools for PHP 8.1:

sudo dnf install php-devel php-xdebug php-pcov

These packages include php-devel for PHP extension development, php-xdebug for debugging, and php-pcov for code coverage. Installing these enhances your PHP development environment, providing comprehensive tools for effective debugging and performance analysis in PHP 8.1.

Installing PHP 8.1 Development Tools in Terminal
Terminal Screenshot of Installing PHP Development Tools

Verify PHP 8.1 Installation

After the installation, verifying that PHP 8.1 is correctly installed on your system is essential. To check the installed PHP version, execute:

php --version

This command displays the PHP version, confirming the successful installation of PHP 8.1.

Verifying PHP 8.1 Installation in AlmaLinux Terminal
PHP 8.1 Version Verification in Terminal

Install PHP 8.1 Extensions on AlmaLinux

List Installed PHP 8.1 Modules

Before installing new extensions, it’s important to identify the modules already present in your PHP 8.1 installation on AlmaLinux. To list these modules, execute:

php -m

This command outputs a comprehensive list of installed modules. However, for easier visualization, you can format the output into a single line:

php -m | tr '\n' ' '; echo

This formatting replaces newline characters with spaces, making the module names easier to read in a single line.

Displaying PHP 8.1 Modules in AlmaLinux Terminal
Clean Display of PHP 8.1 Modules in Terminal

Alternatively, for a compact multi-column view, use this command:

php -m | paste -s -d '\t\t\n'; echo

The paste command rearranges the output into a tidier, multi-column layout, with the -d option specifying the delimiters (two tabs followed by a newline). These commands display all modules installed with PHP 8.1, aiding in identifying which additional extensions are needed.

Install Additional PHP 8.1 Extensions

To incorporate extra PHP 8.1 extension modules not included in the default setup, begin by searching for the required module. For example:

sudo dnf search php8.1-{extension name}

Replace {extension name} with the specific extension you seek, like php8.1-redis.

For a broad range of applications, consider these commonly used extensions. Adjust the list as needed:

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

Carefully select only the extensions vital for your setup and remove any that become redundant to maintain an optimized PHP environment.

Configure PHP-FPM for PHP 8.1 with Nginx on AlmaLinux

Adjusting PHP-FPM Configuration for Nginx

The default configuration of PHP-FPM in AlmaLinux sets the user to ‘Apache’, which is incompatible with Nginx. Changing this setting is essential for Nginx integration.

Access PHP-FPM Configuration

Begin by opening the PHP-FPM configuration file to modify how PHP-FPM interacts with Nginx:

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

This command launches the PHP-FPM configuration file in Nano, a text editor.

Update User and Group Settings

Within the www.conf file, find the user and group settings set to ‘Apache’ by default. For proper functioning with Nginx, update these settings to ‘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

This modification aligns PHP-FPM with Nginx, enhancing the compatibility and performance of your web server setup.

Configuring PHP-FPM for Nginx and PHP 8.1 on AlmaLinux
Adjusting PHP-FPM User and Group for Nginx

Save Changes and Exit Editor

After making the changes, save the file by pressing CTRL+O and exit Nano with CTRL+X. This step ensures your alterations are stored.

Restart PHP-FPM Service

To apply the new user and group settings, restart the PHP-FPM service:

sudo systemctl restart php-fpm

Restarting PHP-FPM activates the updated configuration, optimizing PHP-FPM for Nginx on your AlmaLinux system.

Conclusion

Well, that wraps it up! We’ve journeyed through setting up PHP 8.1 on AlmaLinux, from setting it as the default module to ensuring all the necessary development tools are in place. Along the way, we’ve tackled upgrading from older PHP versions and configuring PHP-FPM for Nginx compatibility. Remember, keeping your PHP version up-to-date is key for security and performance. And hey, don’t hesitate to explore and add new PHP extensions as your projects evolve.

1 thought on “How to Install PHP 8.1 on AlmaLinux 9 or 8”

Leave a Comment


Your Mastodon Instance
Share to...