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

In this article, brought to you by Orcacore, we’ll guide you through the process of installing PHP 7.4 on Rocky Linux 8. PHP is a widely used open-source, general-purpose scripting language that is especially suited for web development. Its popularity stems from its ability to create dynamic and interactive websites.

PHP functions as a server-side scripting language. This means the PHP code is executed on the web hosting server, not in the user’s browser. When a visitor requests a page from your website, their browser sends a request to your server. The server then executes the PHP code, generates the corresponding HTML page, and sends it back to the visitor’s browser. The visitor only sees the rendered HTML and has no access to the underlying PHP code, ensuring security and efficiency.

Before proceeding with this guide on PHP 7.4 on Rocky Linux 8, ensure you have access to a Rocky Linux 8 server and are logged in as a non-root user with sudo privileges. If you haven’t already configured this, refer to our guide on Initial Server Setup with Rocky Linux 8 for detailed instructions.

Set up PHP 7.4 Rocky Linux 8

Let’s begin the installation process.

Remove Old Versions of PHP

First, it’s crucial to update your local package index to ensure you have the latest package information:

sudo dnf update -y

Next, remove any existing PHP or PHP-FPM installations to avoid conflicts. Run the following command to remove the core packages:

sudo dnf remove php php-fpm -y

To ensure a clean slate, remove any remaining PHP-related package extensions using:

sudo dnf remove php* -y

List Available PHP Modules

Reset the PHP module list to its default state using the command below. This is an important step to ensure a smooth transition to PHP 7.4:

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

The image above illustrates the output of the dnf module list php command. Note the (d) tag, which indicates the default PHP version. This needs to be reset before enabling PHP 7.4.

Enable and Install PHP 7.4

Now, enable the PHP 7.4 module on your Rocky Linux 8 system:

sudo dnf module enable php:7.4 -y

With PHP 7.4 enabled, install the core PHP package:

sudo dnf install php -y

For a fully functional PHP environment, install commonly used extensions. This command installs extensions for CLI, FPM, cURL, MySQL, GD, and more:

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 interested in PHP development, install the development package:

sudo dnf install php-devel -y

Verify your 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’re using Nginx, you need to adjust the configuration.

Open the www.conf file:

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

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

user = nginx
group = nginx

Save and close the file.

Reload the PHP-FPM service to apply the changes:

sudo systemctl restart php-fpm

Configure your Nginx server block to process PHP files by adding 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 comprehensive information, refer to the PHP Documentation page.

Alternative Installation Methods for PHP 7.4 on Rocky Linux 8

While the dnf module approach is standard, alternative methods exist for installing PHP 7.4 on Rocky Linux 8. Here are two options:

1. Using Remi Repository:

The Remi repository provides more recent versions of PHP than the base Rocky Linux repositories. While we are installing PHP 7.4, the Remi repository often offers more flexibility in choosing the specific version. It is important to note that using third-party repositories requires caution. Ensure that you trust the source before adding it to your system.

  • Add the Remi repository:
sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
  • Enable the PHP 7.4 Remi repository:
sudo dnf module enable php:remi-7.4 -y
  • Install PHP and extensions (same as before):
sudo dnf install php 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
  • Explanation: This method utilizes a third-party repository known for providing updated PHP versions. Enabling the Remi repository allows you to install PHP 7.4 from a source other than the standard Rocky Linux repositories. This is useful if the standard repositories are outdated or if you need specific configurations offered by the Remi repository. This method is more advanced and might introduce dependencies or conflicts if not handled carefully.

2. Building from Source:

This is the most complex method but offers the greatest control over the PHP installation.

  • Install development tools:
sudo dnf groupinstall "Development Tools" -y
sudo dnf install bison re2c libxml2-devel openssl-devel bzip2-devel libcurl-devel libpng-devel libjpeg-turbo-devel freetype-devel gmp-devel net-snmp-devel libzip-devel -y
  • Download PHP 7.4 source code: Download the source code from the official PHP website (php.net) and place it in /usr/src/.
cd /usr/src/
sudo wget https://www.php.net/distributions/php-7.4.33.tar.gz # Replace with latest 7.4.x version
sudo tar -xvzf php-7.4.33.tar.gz
cd php-7.4.33
  • Configure, compile, and install:
./configure --enable-fpm --with-mysqli --with-pdo-mysql --with-curl --with-gd --with-jpeg --with-png --with-freetype --with-openssl --with-zlib --enable-mbstring --enable-exif
make
sudo make install
  • Configure PHP-FPM: You’ll need to manually create the php.ini file and configure PHP-FPM. This is a more involved process.

  • Explanation: Building from source involves downloading the PHP source code, configuring it with specific options, compiling it, and then installing the compiled binaries. This method provides maximum flexibility, allowing you to customize every aspect of the PHP installation. However, it’s also the most complex and time-consuming option. It requires a solid understanding of compiling software and managing dependencies.

These alternative methods offer varying degrees of complexity and control. The Remi repository provides a relatively straightforward way to access newer PHP versions, while building from source allows for complete customization. The best method depends on your specific needs and technical expertise.

Conclusion

You have now successfully learned how to Install PHP 7.4 on Rocky Linux 8 using multiple methods. While PHP 7.4 reached its end-of-life in November 2022, it remains a common choice in many existing web applications. Keep in mind that using outdated software can present security risks, so plan to migrate to a newer version of PHP when feasible.

We hope this guide has been helpful. You may also find these articles useful:

Remember to choose the installation method for PHP 7.4 on Rocky Linux 8 that best fits your specific needs and technical skills. Installing PHP 7.4 on Rocky Linux 8 is a crucial step for many web development projects.

Leave a Reply

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