How to Install PHP 8.1 on AlmaLinux 9

Introduction

Before we begin talking about how to install PHP 8.1 on AlmaLinux 9, let's briefly understand – What is PHP 8.1?

PHP 8.1 is a significant update to the popular programming language used for building dynamic websites and web applications. It offers several new features and enhancements aimed at improving developer experience and application performance.

PHP 8.1 focuses on improving developer productivity, performance, and security. With features like enums for defining a set of named constants, the deprecation of some outdated functionality, and enhancements to the type system, PHP 8.1 offers more flexibility and efficiency in coding. Developers can expect better performance and easier maintenance of their code with this version.

In this tutorial, you will install PHP 8.1 on AlmaLinux 9. We will also address a few FAQs on how to install PHP 8.1 on AlmaLinux 9.

Advantages of PHP 8.1

  1. Improved Performance: PHP 8.1 comes with optimizations that enhance the speed and efficiency of web applications, resulting in quicker load times and better overall performance.
  2. Enhanced Developer Productivity: Features like enums and named arguments simplify code writing, making development faster and more organized. Developers can write cleaner, more maintainable code with less effort.
  3. Enhanced Type System: PHP 8.1 introduces stricter type annotations and improved type checking, helping developers catch errors early in the development process and ensuring more robust code.
  4. Deprecation Policy: Outdated features are deprecated in PHP 8.1, encouraging the adoption of modern coding practices and ensuring better code quality and future compatibility.
  5. Security Improvements: PHP 8.1 includes security enhancements that help protect web applications from common vulnerabilities, making it a more secure choice for development projects.

Import Remi PHP RPM for PHP 8.1 on AlmaLinux

Update AlmaLinux System

Update AlmaLinux first to make sure all system packages are up-to-date. This action improves your server's security and stability.

sudo dnf upgrade --refresh

This command updates all installed packages to the most recent versions and refreshes your package database.

Enable Repositories and Add Remi PHP RPM Repository to AlmaLinux

For AlmaLinux 9

Enable CRB Repository

The CodeReady Linux Builder (CRB) repository offers extra libraries and development tools that are necessary for installing PHP.

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

Include the EPEL repository, which provides a wide range of extra Enterprise Linux packages.

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

Install the Remi PHP repository to get the most recent versions of PHP.

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

Install PHP 8.1 on AlmaLinux

Set PHP 8.1 as the Default Module

Set PHP 8.1 as the system's default PHP version on your AlmaLinux system to start the installation. To enable PHP 8.1 from the Remi repository, run the command below:

sudo dnf module enable php:remi-8.1 -y

With this step, PHP 8.1 is designated as your system's primary PHP version. It guarantees that this version will be the default for all PHP package installations and updates.

Enabling PHP 8.1 from Remi Repo in Terminal on AlmaLinux

Install PHP 8.1 Using the DNF Command

Once the PHP 8.1 module has been enabled, install it. The command to install PHP 8.1 is as follows:

sudo dnf install php

During installation, you'll be asked to import Remi's RPM GPG keys. Make sure you use a "Y" to confirm. This step prepares your AlmaLinux system for use by installing PHP 8.1.

PHP 8.1 Installation Prompt in AlmaLinux Terminal

Use this command instead if you already have a PHP installation (such as PHP 8.0) through Remi and would like to update to PHP 8.1:

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

These commands make PHP 8.1 your system's default version. They make sure that this version will be the default for all PHP package installations and updates.

Install PHP 8.1 Development Tools (Optional)

Installing specific development packages is advised for developers who require additional tools and packages for PHP development.

Install the necessary PHP 8.1 development tools with this command:

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

These packages include php-pcov for code coverage, php-xdebug for debugging, and php-devel for developing PHP extensions. By adding these to your PHP development environment, you can improve PHP 8.1 performance analysis and debugging with extensive tools.

Installing PHP 8.1 Development Tools in Terminal

Verify PHP 8.1 Installation

Checking that PHP 8.1 is installed correctly on your system is crucial after the installation. To verify the PHP version that is installed, run:

php --version

The PHP version is shown by this command, verifying that PHP 8.1 was installed successfully.

PHP 8.1 Version Verification in Terminal

Install PHP 8.1 Extensions on AlmaLinux

List Installed PHP 8.1 Modules

It's crucial to determine which modules are already installed in your PHP 8.1 installation on AlmaLinux before adding new extensions. To see a list of these modules, run:

php -m

A detailed list of all installed modules is produced by this command. However, you can format the output into a single line for simpler visualization:

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

The module names are now easier to read in a single line thanks to this formatting, which substitutes spaces for newline characters.

Clean Display of PHP 8.1 Modules in Terminal

As an alternative, use this command to create a small multi-column view:

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

Using the -d option to specify the delimiters (two tabs followed by a newline), the paste command reorganizes the output into a neater, multi-column layout. These commands show all installed modules for PHP 8.1, making it easier to determine which extensions are still required.

Install Additional PHP 8.1 Extensions

Start by looking for the necessary module in order to add additional PHP 8.1 extension modules that aren't part of the default configuration. As an example:

sudo dnf search php8.1-{extension name}

Change {extension name} to the exact extension you are looking for, such as php8.1-redis.

Take a look at these frequently used extensions for a variety of applications. As necessary, make changes to the list:

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

To keep your PHP environment optimized, carefully choose just the extensions that are essential for your setup and remove any that become unnecessary.

Configure PHP-FPM for PHP 8.1 with Nginx on AlmaLinux

Adjusting PHP-FPM Configuration for Nginx

Nginx cannot work with PHP-FPM in AlmaLinux because the default configuration sets the user to "Apache." Modifying this setting is necessary in order to integrate Nginx.

Access PHP-FPM Configuration

To change the way PHP-FPM communicates with Nginx, start by opening the PHP-FPM configuration file:

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

With this command, the text editor Nano opens the PHP-FPM configuration file.

Update User and Group Settings

Look for the user and group settings in the www.conf file. By default, they are set to "Apache." Set these settings to 'Nginx' for Nginx to work properly:

; 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

By aligning PHP-FPM with Nginx, this modification improves your web server setup's compatibility and performance.

Adjusting PHP-FPM User and Group for Nginx

Save Changes and Exit Editor

Once the changes are made, use CTRL+O to save the file and CTRL+X to exit Nano. This step guarantees that the changes you make are saved.

Restart PHP-FPM Service

Restart the PHP-FPM service in order to take effect of the new user and group settings:

sudo systemctl restart php-fpm

You can optimize PHP-FPM for Nginx on your AlmaLinux system by restarting PHP-FPM to activate the updated configuration.

FAQs to Install PHP 8.1 on AlmaLinux 9

What are the prerequisites for installing PHP 8.1 on AlmaLinux 9?

You will need an internet connection and administrative privileges (root or sudo access) on your system.

What are the different ways to install PHP 8.1 on AlmaLinux 9?

The most common methods are using the official AlmaLinux repositories (limited version options) or a third-party repository like Remi.

How can I check if PHP 8.1 is already installed on my system?

Open a terminal and type php -v. If PHP 8.1 is installed, it will display the version information.

What are some common issues that can occur during installation?

Permission errors, network connectivity issues, and conflicts with existing software can be potential hurdles.

Can I install PHP 8.1 alongside other PHP versions on AlmaLinux 9?

Yes, you can have multiple PHP versions coexisting on AlmaLinux 9, allowing you to choose the version for each project.

Can I revert to a previous PHP version if needed after installing PHP 8.1 on AlmaLinux 9?

Yes, you can switch back to a previous PHP version if compatibility issues arise with your applications.

Is it necessary to restart the system after installing PHP 8.1 on AlmaLinux 9?

While not always mandatory, a system reboot after installation may help apply any necessary changes effectively.

Conclusion

We hope this tutorial helped you understand how to install PHP 8.1 on AlmaLinux 9.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.