Easy Steps To Install PHP 7.4 on Rocky Linux 8

Posted on

Easy Steps To Install PHP 7.4 on Rocky Linux 8

Easy Steps To Install PHP 7.4 on Rocky Linux 8

This article from Orcacore will guide you through the process of installing PHP 7.4 on Rocky Linux 8. PHP is a widely used, open-source scripting language, especially suited for web development. It is a powerful tool for creating dynamic and interactive websites.

PHP operates as a server-side scripting language. This means the PHP code is executed on the web server, rather than the user’s browser. When a user requests a webpage, the server processes the PHP code, generating HTML output that is then sent to the user’s browser for display. The user only sees the rendered HTML, keeping the underlying PHP code secure on the server.

Before proceeding with this guide to Install PHP 7.4 on Rocky Linux 8, ensure you have a user account with sudo privileges on your Rocky Linux 8 server. If you haven’t already, you can follow our guide on Initial Server Setup with Rocky Linux 8 to create such an account.

Set up PHP 7.4 Rocky Linux 8

First, update your local package index to ensure you have the latest information about available packages:

sudo dnf update -y

Remove Old Versions of PHP

If you have any previous versions of PHP or PHP-FPM installed, remove them to avoid conflicts:

sudo dnf remove php php-fpm -y

Remove any remaining PHP extensions:

sudo dnf remove php* -y

List Available PHP Modules

Reset the PHP module list to its default state. This is important for ensuring a clean installation:

sudo dnf module list reset php -y
List Available PHP Modules Rocky Linux 8

As you can see in the image, PHP 7.2 is marked with (d), indicating it’s the default. We need to change this to install PHP 7.4 on Rocky Linux 8.

Enable and Install PHP 7.4

Enable the PHP 7.4 module:

sudo dnf module enable php:7.4 -y

Now, install PHP 7.4:

sudo dnf install php -y

To install commonly used PHP extensions, run 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, the development branch can be installed using:

sudo dnf install php-devel -y

Verify the installation by checking the PHP version:

php -v
php 7.4 Rocky Linux 8

Configure PHP-FPM To Run as Nginx User

By default, PHP-FPM is configured to run as the Apache user. If you are using Nginx, you need to modify the configuration.

Open the www.conf file using a text editor like vi:

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

Locate the user and group directives and change them to nginx:

user = nginx
group = nginx

Save the changes and close the file.

Reload the PHP-FPM service to apply the new configuration:

sudo systemctl restart php-fpm

The Nginx server block needs the following configuration to properly process PHP files:

    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 PHP Documentation page.

Conclusion

You have successfully learned how to Install PHP 7.4 on Rocky Linux 8. Although PHP 7.4 reached its end of life on November 28, 2022, it remains in widespread use.

Hope you found this guide helpful. You may also find these articles interesting:

Set up Anaconda on Rocky Linux 8

Install DotNet on Rocky Linux 8

How to count lines in a file in Linux?

fasd quick access to files and directories

How do I change the version of phpMyAdmin?

Download and Install Virtualmin GPL on Linux

Set up Lighttpd Web Server Rocky Linux 8

CSF Firewall Rocky Linux 8

Alternative Solutions for Installing PHP 7.4 on Rocky Linux 8

While the DNF package manager is a standard and reliable method, alternative approaches exist for installing PHP 7.4 on Rocky Linux 8. Here are two distinct methods:

1. Using Remi Repository:

Remi Collet’s repository provides newer versions of PHP and related packages for CentOS and RHEL-based systems, including Rocky Linux. This method is particularly useful if you need more control over the specific version and extensions.

  • Explanation: The Remi repository offers a wider selection of PHP versions and extensions than the default Rocky Linux repositories. It also often includes more up-to-date packages. However, it requires adding a third-party repository to your system, so it’s crucial to ensure the repository is trustworthy and well-maintained.

  • Steps:

    1. Install Remi Repository:

      sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
    2. Enable PHP 7.4 Remi Module:

      sudo dnf module enable php:remi-7.4 -y
    3. Install PHP and Extensions:

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

      You can adjust the extension list based on your needs.

    4. Verify Installation:

      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 is achieved by creating isolated environments for each software version. This is another alternative to Install PHP 7.4 on Rocky Linux 8.

  • Explanation: SCL provides a way to run different versions of PHP simultaneously, which is useful for testing or maintaining legacy applications. Each PHP version operates within its own environment, preventing conflicts with the system’s default PHP installation.

  • Steps:

    1. Install SCL Repository:

      sudo dnf install centos-release-scl -y
    2. Install PHP 7.4 SCL: (This assumes a PHP 7.4 SCL is available. Availability can vary). This example uses rh-php74 as a placeholder for the SCL name. You’ll need to verify the correct name.

      sudo dnf install rh-php74 -y
    3. Enable the SCL:

      Before using PHP 7.4 from the SCL, you need to enable it for your current shell session:

      scl enable rh-php74 bash
    4. Verify Installation:

      php -v

      Note that you need to enable the SCL in each new terminal session where you want to use the SCL-installed PHP.

    5. Running PHP-FPM as a Service: Running PHP-FPM from an SCL typically requires adjusting the service configuration. This is beyond the scope of a simple example, but generally involves creating a custom systemd service file that executes scl enable before starting PHP-FPM.

These alternative methods offer flexibility and control over your PHP installation, depending on your specific requirements and use case. Remember to choose the method that best suits your needs and ensure proper configuration for optimal performance and security. Installing PHP 7.4 on Rocky Linux 8 can be done in many ways, so choose the one that suits you.

Leave a Reply

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