How To Install PHP 8.1 on AlmaLinux 9 | Easy Steps

Posted on

How To Install PHP 8.1 on AlmaLinux 9 | Easy Steps

How To Install PHP 8.1 on AlmaLinux 9 | Easy Steps

In this comprehensive guide, we will explore How To Install PHP 8.1 on AlmaLinux 9. PHP stands as a cornerstone of web development, an open-source, server-side scripting language renowned for its versatility. From crafting dynamic websites and sophisticated web applications to building robust customer relationship management (CRM) systems, PHP’s capabilities are vast. Its widespread adoption stems from its general-purpose nature and its seamless integration with HTML, allowing developers to embed PHP code directly within HTML files, simplifying the development process and enhancing code maintainability.

This tutorial, brought to you by Orcacore, will guide you through the process of installing the latest stable release of PHP, currently version 8.1, on your AlmaLinux 9 server. We will provide a step-by-step approach to ensure a smooth and successful installation.

Before we dive in, ensure you have the necessary prerequisites. You’ll need to be logged into your AlmaLinux 9 server as a non-root user with sudo privileges. If you haven’t already configured this, you can refer to our detailed guide on Initial Server Setup with AlmaLinux 9.

1. Install PHP Remi Repository on AlmaLinux 9

The first step in How To Install PHP 8.1 on AlmaLinux 9 involves setting up the necessary repository. Begin by updating your local package index to ensure you have the latest information about available packages. Execute the following command:

sudo dnf update -y

Next, install the Extra Packages for Enterprise Linux (EPEL) repository, which provides additional packages not available in the default AlmaLinux repositories:

sudo dnf install epel-release -y

Since PHP 8.1 is not included in AlmaLinux’s default AppStream, we’ll utilize the Remi repository, known for providing up-to-date PHP builds. Install the Remi repository with this command:

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

After adding the Remi repository, update your package index again to reflect the new repository:

sudo dnf update -y

2. Remove Older PHP Version From AlmaLinux 9

Before proceeding, it’s crucial to remove any existing PHP installations to avoid conflicts. If you have older versions of PHP or PHP-FPM installed, remove them using the following commands:

sudo dnf remove php php-fpm -y

Then, remove any remaining PHP-related packages:

sudo dnf remove php* -y

3. Enable Remi PHP 8.1 Repository

To ensure you install the correct version, reset the PHP module list to its default state:

sudo dnf module list reset php -y
Enable Remi PHP 8.1 Repository

Note: While PHP 8.2 might be available for testing, this guide focuses on installing the stable PHP 8.1 version. Enable the Remi PHP 8.1 repository with the following command:

sudo dnf module enable php:remi-8.1

4. Installing PHP 8.1 on AlmaLinux 9

With the Remi repository enabled and PHP 8.1 selected, you can now install PHP 8.1 using the following command:

sudo dnf install php -y

To install commonly used PHP extensions, use the following command:

sudo dnf install php-cli php-fpm php-curl php-mysqlnd php-gd php-opcache php-zip php-intl php-common php-bcmath php-imap 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

For developers interested in the development branch, install the PHP development tools:

sudo dnf install php-devel -y

To verify your installation of PHP 8.1 on AlmaLinux 9, check the version using the command:

php -v
Installing PHP 8.1 on AlmaLinux 9

By default, on AlmaLinux 9, PHP-FPM is configured to run under the Apache user. If you are using Nginx, you need to adjust the configuration in www.conf.

Open the configuration file using your preferred text editor (e.g., vi):

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

Locate the user and group directives and modify them to reflect the Nginx user:

**user = nginx
group = nginx**

Save the changes and close the file. Restart the PHP-FPM service to apply the new configuration:

sudo systemctl restart php-fpm

For Nginx to properly process PHP files, the server block configuration needs the following location block:

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

For more detailed information about PHP, refer to the official PHP Documentation page.

Conclusion

How To Install PHP 8.1 on AlmaLinux 9 is simplified through the use of the Remi repository. This process ensures that you are running a stable and up-to-date version of PHP, along with essential extensions, making your server ready for modern web application development.

Hope you enjoy it. You may also like to read the following articles:

Installing PHP 8.3 on Debian 11

Install PHP 7.4 on Debian 12 Bookworm

Install PHP 8.3 on Fedora Linux 39

PHP 8.3 Installation on AlmaLinux 9

Alternative Solutions for Installing PHP 8.1 on AlmaLinux 9

While the Remi repository method is highly recommended for its ease and reliability, let’s explore two alternative approaches to How To Install PHP 8.1 on AlmaLinux 9.

1. Using Software Collections (SCL)

Software Collections (SCL) allows you to install multiple versions of the same software on a single system without conflicts. This can be particularly useful if you need to maintain compatibility with older applications that require older PHP versions while running newer applications on PHP 8.1. However, SCL is often behind the Remi Repo in terms of the newest PHP versions.

Explanation:

SCL installs software in /opt/rh/, keeping it separate from the base system. You then use the scl enable command to activate the desired version in your current session.

Steps:

  1. Install SCL repository:

    sudo dnf install centos-release-scl -y
  2. Install PHP 8.1 from SCL (if available, check the available collections first):

    sudo dnf install rh-php81 -y

    Note: The package name might differ slightly depending on the exact SCL offering.

  3. Enable PHP 8.1 in your current session:

    scl enable rh-php81 bash
  4. Verify the PHP version:

    php -v

Code Example:

Assuming rh-php81 is the correct package name in the SCL:

sudo dnf install centos-release-scl -y
sudo dnf install rh-php81 -y
scl enable rh-php81 bash
php -v

Caveats: You need to enable the SCL environment in each session or add the scl enable command to your .bashrc file to make it permanent for your user.

2. Manual Compilation from Source

This method provides the most control over the installation process. It allows you to customize the build options and install PHP in a location of your choice. However, it’s also the most complex and time-consuming. This is not generally recommended for production environments unless very specific needs are driving this decision.

Explanation:

Compiling from source involves downloading the PHP source code, configuring the build environment, compiling the code, and then installing the compiled binaries.

Steps:

  1. Install development tools and dependencies:

    sudo dnf groupinstall "Development Tools" -y
    sudo dnf install libxml2-devel bzip2-devel curl-devel libpng-devel libjpeg-devel freetype-devel gmp-devel openssl-devel -y
  2. Download the PHP 8.1 source code:

    Visit php.net and find the download link for the PHP 8.1 source code (e.g., php-8.1.x.tar.gz).

    wget https://www.php.net/distributions/php-8.1.x.tar.gz
    tar -xzf php-8.1.x.tar.gz
    cd php-8.1.x
  3. Configure the build:

    This step determines which features and extensions will be included in the build. Customize the configure options to suit your needs.

    ./configure --prefix=/usr/local/php81 --with-config-file-path=/usr/local/php81/etc --with-mysqli --with-pdo-mysql --with-curl --with-gd --with-openssl --enable-mbstring
  4. Compile and install:

    make
    sudo make install
  5. Configure PHP:

    Create a php.ini file in /usr/local/php81/etc and customize it.

  6. Add PHP to your PATH:

    Edit your .bashrc file and add the following line:

    export PATH="/usr/local/php81/bin:$PATH"

    Source your .bashrc file:

    source ~/.bashrc
  7. Verify the installation:

    php -v

Code Example:

This example shows the basic commands for compiling and installing from source. Remember to adjust the configure options to match your specific requirements.

sudo dnf groupinstall "Development Tools" -y
sudo dnf install libxml2-devel bzip2-devel curl-devel libpng-devel libjpeg-devel freetype-devel gmp-devel openssl-devel -y
wget https://www.php.net/distributions/php-8.1.x.tar.gz #REPLACE php-8.1.x with correct version
tar -xzf php-8.1.x.tar.gz
cd php-8.1.x
./configure --prefix=/usr/local/php81 --with-config-file-path=/usr/local/php81/etc --with-mysqli --with-pdo-mysql --with-curl --with-gd --with-openssl --enable-mbstring
make
sudo make install

Caveats:

  • This method requires a good understanding of the compilation process.
  • You are responsible for managing dependencies and security updates.
  • It can be time-consuming and error-prone.

In conclusion, while the Remi repository offers the most straightforward approach to How To Install PHP 8.1 on AlmaLinux 9, SCL and manual compilation provide alternative solutions for specific scenarios. Choose the method that best suits your needs and technical expertise.

Leave a Reply

Your email address will not be published. Required fields are marked *