Easy Way To Install PHP 7.4 on AlmaLinux 8 – OrcaCore

Posted on

Easy Way To Install PHP 7.4 on AlmaLinux 8 - OrcaCore

Easy Way To Install PHP 7.4 on AlmaLinux 8 – OrcaCore

In this guide on the Orcacore website, we want to teach you How To Install PHP 7.4 on AlmaLinux 8. PHP is a type of open-source general-purpose scripting language fit for server-side programming. It is a popular choice in web development for creating dynamic pages and applications.

The acronym used to stand for Personal Home Page. But, now, PHP is known as Hypertext Preprocessor.

Note that PHP 7.4 is an outdated version, and it is recommended to use the stable versions. To upgrade the PHP version, you can check this guide on upgrading PHP Version on AlmaLinux 8.

To install PHP 7.4, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on the Initial Server Setup with AlmaLinux 8.

Now follow the steps below to complete this guide.

Step 1. Remove Old Versions of PHP

First, you need to update your local package index with the following command:

sudo dnf update -y

Then, you need to remove the PHP and PHP-FPM previous versions if you have them installed on your server. To do this, run the following command:

sudo dnf remove php php-fpm -y

Then, remove the rest of the package extensions with the command below:

sudo dnf remove php* -y

Step 2. List Available PHP Versions on AlmaLinux 8

To reset the PHP module list on AlmaLinux 8, you can use the following command:

sudo dnf module list reset php -y

In your output, you should see:

reset the PHP module list on AlmaLinux 8

As you can see above, the (d) tag is next to PHP 7.2, which you will need to reset and change to install PHP 7.4.

Step 3. Enable PHP 7.4 on AlmaLinux 8

Now you need to enable PHP 7.4 on AlmaLinux 8 with the command below:

sudo dnf module enable php:7.4

At this point, you have enabled PHP 7.4 to be the default version on your system. You can install PHP 7.4 with the following command:

sudo dnf install php -y

If you would like to install the most commonly used extensions for PHP 7.4, 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

Also, you can use the following command for anyone interested in installing the development branch:

sudo dnf install php-devel -y

To verify your PHP 7.4 installation on AlmaLinux 8, you can check its version:

php -v
Enable PHP 7.4 on AlmaLinux 8

Step 4. (Optional) Run PHP-FPM as Nginx User

By default, on AlmaLinux 8, the PHP-FPM service is designed to be run as the Apache user. If you are using Nginx, you need to make configuration changes at (www.conf).

You can open the file with your favorite text editor. Here we use vi:

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

Find the user and group directives and change them to Nginx as shown below:

user = nginx
group = nginx

When you are done, save and close the file.

Reload the PHP-FPM service to apply the changes:

sudo systemctl restart php-fpm

The Nginx server block needs the following example below for Nginx to process the PHP files.

Below is an example for all server {} blocks that process PHP files that need the location ~ .php$ added.

    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 information, you can visit the PHP Documentation page.

Conclusion

At this point, you learned to install PHP 7.4 on AlmaLinux 8. You just need to remove the old version of PHP and enable the PHP version you want to install. From there, you can configure your PHP-FPM service to run as a Nginx user or Apache.

Hope you enjoy it. You may also like these articles:

  • How To Install MySQL on AlmaLinux 8
  • Install and Configure Zabbix on AlmaLinux 8
  • How To Set up 7-Zip on AlmaLinux 8

Alternative Solutions for Installing PHP 7.4 on AlmaLinux 8

While the above method using dnf module is a straightforward approach, here are two alternative methods you can use to install PHP 7.4 on AlmaLinux 8:

1. Using Remi Repository

Remi is a popular third-party repository that provides more recent versions of PHP and related packages than the base AlmaLinux repositories. It is especially useful when installing older PHP versions that are no longer directly supported by the default repositories.

Explanation:

The Remi repository contains numerous PHP versions, including PHP 7.4, that are not available in the standard AlmaLinux repositories. By enabling the Remi repository and selecting the PHP 7.4 stream, you can use the dnf package manager to install PHP 7.4 and its associated extensions.

Steps:

  1. Install Remi Repository:
    First, install the Remi repository configuration package:

    sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
  2. Enable PHP 7.4 Remi Module:
    Next, enable the PHP 7.4 module from the Remi repository. You will also need to enable the remi repository itself.

    sudo dnf module enable php:remi-7.4 -y
  3. Install PHP 7.4 and Extensions:
    Finally, install PHP 7.4 along with the desired extensions:

    sudo dnf install php php-fpm php-mysqlnd php-opcache php-gd php-curl php-mbstring php-xml -y

    You can adjust the list of extensions to match your specific requirements.

  4. Verify the Installation:

    php -v

Example Code (Complete Installation):

sudo dnf update -y
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
sudo dnf module enable php:remi-7.4 -y
sudo dnf install php php-fpm php-mysqlnd php-opcache php-gd php-curl php-mbstring php-xml -y
php -v

2. 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 very useful if you need to run different applications that require different PHP versions.

Explanation:

SCL provides a way to install PHP 7.4 in a separate directory structure, preventing it from interfering with the system’s default PHP version or other PHP versions installed via SCL. This approach is beneficial for applications that require a specific PHP version and cannot be easily upgraded.

Steps:

  1. Install SCL Repository:
    Install the SCL release package:

    sudo dnf install centos-release-scl -y
  2. Install PHP 7.4 from SCL:
    Install PHP 7.4 using the SCL package:

    sudo dnf install rh-php74 -y
  3. Install PHP 7.4 Extensions (SCL):
    Install the extensions using the SCL naming convention:

    sudo dnf install rh-php74-php-mysqlnd rh-php74-php-opcache rh-php74-php-gd rh-php74-php-curl rh-php74-php-mbstring rh-php74-php-xml -y
  4. Enable PHP 7.4 SCL:
    To use PHP 7.4 installed via SCL, you need to enable the SCL environment. This is done using the scl command. This will be needed for each new shell instance where you want to use this PHP installation.

    scl enable rh-php74 bash
  5. Verify Installation:

    php -v

    Note: When you run php -v after enabling SCL, it will point to the SCL version of PHP. If you exit the shell and open a new one, php -v will revert to the default PHP version installed (if any).

Example Code (Complete Installation):

sudo dnf update -y
sudo dnf install centos-release-scl -y
sudo dnf install rh-php74 -y
sudo dnf install rh-php74-php-mysqlnd rh-php74-php-opcache rh-php74-php-gd rh-php74-php-curl rh-php74-php-mbstring rh-php74-php-xml -y
scl enable rh-php74 bash
php -v

These alternative methods provide flexibility in managing PHP versions on AlmaLinux 8, especially when dealing with specific application requirements or needing to run multiple PHP versions concurrently. Each method has its advantages and disadvantages, so choose the one that best suits your specific needs.

Leave a Reply

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