Easy Steps To Install PHP 8.2 on AlmaLinux 8 – OrcaCore

Posted on

Easy Steps To Install PHP 8.2 on AlmaLinux 8 - OrcaCore

Easy Steps To Install PHP 8.2 on AlmaLinux 8 – OrcaCore

This tutorial, brought to you by OrcaCore, will guide you through the process of installing PHP 8.2 on AlmaLinux 8. PHP 8.2 represents a significant upgrade to the PHP language, introducing a host of new features. These enhancements include readonly classes, the ability to use null, false, and true as standalone types, the deprecation of dynamic properties, and various performance improvements. Embracing PHP 8.2 can lead to more efficient and robust web applications.

Before we begin, ensure you have access to your AlmaLinux 8 server as a non-root user with sudo privileges. If you haven’t already configured this, you can refer to our guide on Initial Server Setup with AlmaLinux 8. A properly configured server environment is crucial for a smooth PHP 8.2 installation.

Set up PHP 8.2 on AlmaLinux 8

Let’s walk through the steps needed to successfully install and configure PHP 8.2.

First, update your system’s package index. This ensures you’re working with the latest package information.

sudo dnf update -y

Install EPEL Repository

The Extra Packages for Enterprise Linux (EPEL) repository provides additional packages not found in the default AlmaLinux repositories. Install it with the following command:

sudo dnf install epel-release -y

Install PHP Remi Repository

AlmaLinux’s default AppStream doesn’t include PHP 8.2. To access the latest PHP builds, we’ll use the Remi repository, a reliable source for updated PHP packages. Execute the following command to add the Remi repository:

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

Update the package index again to incorporate the new repository.

sudo dnf update -y

Remove Previous versions of PHP

If you have any previous PHP versions installed, it’s essential to remove them to avoid conflicts. Use the following commands to uninstall existing PHP packages and PHP-FPM:

sudo dnf remove php php-fpm -y

Then, remove any remaining PHP extensions:

sudo dnf remove php* -y

PHP Module List

Resetting the PHP module list ensures a clean slate for the PHP 8.2 installation. Execute this command:

sudo dnf module list reset php -y
PHP Module List AlmaLinux 8

As shown in the image above, PHP 7.2 has the (d) tag which signifies that it is the default version. The next step resets this setting and enables PHP 8.2.

Now, enable the PHP 8.2 module from the Remi repository:

sudo dnf module enable php:remi-8.2

Install PHP 8.2 on AlmaLinux 8

With the Remi repository configured and PHP 8.2 enabled, you can now install the core PHP package:

sudo dnf install php -y

For most web applications, you’ll need several common PHP extensions. Install them using this 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

If you’re involved in PHP development, you might also want to install the development package:

sudo dnf install php-devel -y

To verify that PHP 8.2 has been installed correctly, check the PHP version:

php -v
php 8.2 AlmaLinux 8

Configure PHP-FPM Service

By default, PHP-FPM (FastCGI Process Manager) on AlmaLinux 8 is configured to run under the Apache user. If you’re using Nginx as your web server, you’ll need to adjust the configuration.

Open the PHP-FPM configuration file:

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

Locate the user and group directives and change their values to nginx:

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, your 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, consult the official PHP Documentation page.

Conclusion

You have now successfully learned how to install PHP 8.2 on AlmaLinux 8. PHP 8.2 is a modern, stable, and efficient version of the language that will improve web development, security, and performance. Running PHP 8.2 on AlmaLinux 8 ensures compatibility, stability, and access to improved features for your web applications, servers, and enterprise environments.

Hopefully, you found this helpful. You might also be interested in these articles:

How To Install PostgreSQL 15 on AlmaLinux 8

How To Install Tor Browser on AlmaLinux 8

Install Google Chrome on AlmaLinux 8

Install DirectAdmin on AlmaLinux 8

Alternative Solutions for Installing PHP 8.2 on AlmaLinux 8

While the Remi repository method is widely used and generally reliable, alternative approaches exist for installing PHP 8.2 on AlmaLinux 8. Here are two different methods with explanations and examples:

1. Using Software Collections (SCL)

Software Collections (SCL) allow you to install multiple versions of the same software on a single system without conflicts. This can be useful if you need to maintain compatibility with older PHP versions alongside PHP 8.2.

Explanation:

SCLs provide a separate environment for the installed software, including its libraries and dependencies. This isolation prevents conflicts with the system’s default packages. The scl command enables and disables these environments as needed.

Steps:

  1. Install the SCL repository:

    sudo dnf install centos-release-scl -y
  2. Enable the PHP 8.2 SCL:

    Unfortunately, at the time of writing, a dedicated PHP 8.2 SCL might not be directly available for AlmaLinux 8. However, you can check the available SCLs using:

    sudo dnf search scl | grep php

    If a PHP 8.2 SCL becomes available (e.g., rh-php82), you would install it using:

    sudo dnf install rh-php82 -y
  3. Accessing PHP 8.2:

    After installation, you won’t be able to access PHP 8.2 directly using the php command. You need to enable the SCL environment first:

    scl enable rh-php82 bash

    Now, the php command will point to the PHP 8.2 version within the SCL environment. You can verify this with php -v.

  4. Using PHP-FPM with SCL:

    If using PHP-FPM, you’ll need to configure it to use the SCL environment. The location of the PHP-FPM executable and configuration files will be within the SCL directory (e.g., /opt/rh/rh-php82/root/usr/sbin/php-fpm).

    You would need to modify your Nginx or Apache configuration to point to the correct PHP-FPM socket within the SCL environment. For example:

        location ~ .php$ {
            try_files $uri =404;
            fastcgi_pass unix:/opt/rh/rh-php82/root/run/php-fpm/www.sock;  #Example, adjust path
            fastcgi_index   index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

Code Example (hypothetical):

Assuming a rh-php82 SCL, the modified Nginx configuration snippet above demonstrates how to point Nginx to the PHP-FPM socket within the SCL environment. Remember to adjust the path based on your specific SCL installation.

Pros: Allows multiple PHP versions to coexist.

Cons: More complex configuration, requires managing SCL environments. Availability of specific PHP versions as SCLs can be limited.

2. Compiling from Source

A more advanced approach is to compile PHP 8.2 directly from the source code. This offers maximum flexibility and control over the installation process.

Explanation:

Compiling from source involves downloading the PHP 8.2 source code, configuring it with the desired options and extensions, and then building and installing the software. This allows you to customize the installation to your specific needs and system configuration.

Steps:

  1. Install Development Tools:

    You will need essential development tools to compile the code.

    sudo dnf groupinstall "Development Tools" -y
    sudo dnf install libxml2-devel bzip2-devel curl-devel libpng-devel libjpeg-turbo-devel freetype-devel gmp-devel openssl-devel -y
  2. Download PHP 8.2 Source Code:

    Download the source code from the official PHP website (php.net). Use wget or curl:

    wget https://www.php.net/distributions/php-8.2.x.tar.gz  # Replace x with the latest minor version
    tar -xvzf php-8.2.x.tar.gz
    cd php-8.2.x
  3. Configure the Build:

    Use the ./configure script to prepare the build process. Specify the desired options and extensions. This is a crucial step where you define which modules and features will be included in your PHP build.

    ./configure --prefix=/usr/local/php82 --with-config-file-path=/usr/local/php82/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli --with-pdo-mysql --with-curl --with-gd --with-jpeg --with-png --with-freetype --with-xsl --enable-mbstring --enable-opcache

    Explanation of Configure Options:

    • --prefix=/usr/local/php82: Specifies the installation directory.
    • --with-config-file-path=/usr/local/php82/etc: Sets the location for the php.ini file.
    • --enable-fpm: Enables PHP-FPM.
    • --with-fpm-user=nginx --with-fpm-group=nginx: Configures PHP-FPM to run under the Nginx user and group.
    • --with-mysqli --with-pdo-mysql: Enables MySQL extensions.
    • --with-curl --with-gd --with-jpeg --with-png --with-freetype: Enables support for curl and graphics libraries.
    • --enable-mbstring: Enables multibyte string support.
    • --enable-opcache: Enables the OpCache for performance optimization.
  4. Compile and Install:

    After configuring, compile and install the PHP binaries:

    make
    sudo make install
  5. Configure PHP-FPM:

    Copy the default PHP-FPM configuration file and adjust it to your needs:

    cp php.ini-development /usr/local/php82/etc/php.ini
    cp /usr/local/php82/etc/php-fpm.conf.default /usr/local/php82/etc/php-fpm.conf
    cp /usr/local/php82/etc/php-fpm.d/www.conf.default /usr/local/php82/etc/php-fpm.d/www.conf
    
    vi /usr/local/php82/etc/php-fpm.d/www.conf  # Ensure user and group are set to nginx
  6. Create a Systemd Service File:

    Create a systemd service file to manage the PHP-FPM process:

    sudo vi /etc/systemd/system/php82-fpm.service

    Service File Content:

    [Unit]
    Description=PHP 8.2 FPM service
    After=network.target
    
    [Service]
    Type=simple
    PIDFile=/usr/local/php82/var/run/php-fpm.pid
    ExecStart=/usr/local/php82/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php82/etc/php-fpm.conf
    ExecReload=/bin/kill -USR2 $MAINPID
    ExecStop=/bin/kill -SIGINT $MAINPID
    
    [Install]
    WantedBy=multi-user.target
  7. Start and Enable the Service:

    sudo systemctl enable php82-fpm
    sudo systemctl start php82-fpm
  8. Configure Nginx:

    Configure your Nginx server blocks to use the PHP-FPM socket. The socket path will be determined by the PHP-FPM configuration (usually /tmp/php-fpm.sock or /run/php-fpm/www.sock, but adjust based on your settings). The Nginx configuration snippet is the same as in the SCL example, but you might need to adjust the fastcgi_pass path.

Code Example:

The example above illustrates the critical steps of configuring and building PHP 8.2 from source. Remember to adjust paths and options to match your environment. Also, the created php82-fpm.service enables the management of the PHP-FPM instance.

Pros: Maximum control, allows customization, potentially better performance for specific workloads.

Cons: Complex, time-consuming, requires manual dependency management. More prone to errors if not done carefully.

In conclusion, while the Remi repository offers a straightforward installation method, SCLs and compiling from source provide alternative routes for installing PHP 8.2 on AlmaLinux 8, each with its own set of advantages and disadvantages. Choose the method that best aligns with your specific needs and technical expertise.

Leave a Reply

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