Search

Step-by-Step Guide: How to Install PHP on RHEL 9 Easily

Are you looking to install PHP on RHEL 9? Look no further! In this step-by-step guide, we will walk you through the process of installing PHP 8.2 on RHEL 9, enabling you to power your web development projects with the popular server-side scripting language.

Installing PHP on RHEL 9 has never been easier. By following our comprehensive guide, you'll have PHP up and running in no time, allowing you to create dynamic and interactive websites. So, let's dive in and get started on your PHP installation journey!

Prerequisites for Installing PHP on RHEL 9

Before you begin the installation process for PHP on RHEL 9, you need to ensure that you have a few prerequisites in place. These prerequisites are essential to ensure a smooth and successful installation of PHP on your RHEL 9 system.

1. RHEL 9 or Compatible Linux Distribution

Make sure that you have RHEL 9 or a compatible Linux distribution, such as Rocky Linux 9 or Alma Linux 9, installed on your server. This is necessary because PHP installation on RHEL 9 requires a supported Linux environment.

2. Sudo User with Admin Rights

To install PHP on RHEL 9, you need a sudo user with administrative rights. This user will have the necessary permissions to execute commands and make system changes during the installation process.

3. Internet Connectivity

Ensure that your server has internet connectivity so that you can download the necessary packages and dependencies during the installation. This is important because the PHP installation process will require access to online repositories.

By ensuring that you have these prerequisites in place, you will be ready to proceed with the installation of PHP on RHEL 9. These prerequisites ensure that you have the necessary environment and permissions to carry out a successful installation.

Step 1: Update System Packages

To begin the installation of PHP on RHEL 9, the first step is to update the system packages. This ensures that you have the latest updates and fixes, which can help resolve any installation issues. To do this, log into your server instance and execute the following command:

sudo dnf update -y

This command will update all installed packages to the latest versions. Once the update is complete, it is recommended to restart the server instance to apply any necessary changes:

sudo reboot

Updating the system packages is an essential step before proceeding with the installation of PHP on RHEL 9, as it helps ensure a smooth and problem-free installation process.

Common Troubleshooting:

If you encounter any issues during the update process, such as dependency errors or failed updates, you can try the following troubleshooting steps:

  1. Check your internet connectivity to ensure you have a stable connection.
  2. Ensure that you have the necessary repositories enabled for package installation.
  3. Clean the DNF cache by running the command sudo dnf clean all and then attempt to update the system packages again.
  4. If the issue persists, you can search for specific error messages online or consult the RHEL 9 documentation for further assistance.

By updating the system packages, you are taking an important step towards ensuring a successful installation of PHP on RHEL 9.

Step 2: Add the Remi Repository

In order to install PHP 8.2 on RHEL 9, we need to add the Remi repository, which provides the latest PHP versions for RHEL-based Linux distributions. Adding the Remi repository is a crucial step to ensure you have access to the most up-to-date PHP packages.

  1. To get started, we first need to install the EPEL repository as a prerequisite. Run the following command to install it:
  2. After installing the EPEL repository, verify the installation by running:
  3. Now, we can proceed to add the Remi repository. Execute the following command:
  4. Verify the installation of the Remi repository by running:

By following these steps, you have successfully added the Remi repository, which will allow you to install the latest version of PHP on RHEL 9.

Why Add the Remi Repository?

"The Remi repository is a valuable resource for RHEL users as it provides a wide range of PHP versions and extensions that are not available in the default RHEL repositories. By adding the Remi repository, you gain access to the latest PHP releases and can take advantage of the newest features and improvements."

Step 3: Install PHP 8.2 on RHEL 9

Now that the repositories are installed, we can proceed with installing PHP 8.2 on RHEL 9. Reset the default PHP module using the command:

sudo dnf module reset php -y

Next, enable the PHP Remi 8.2 module:

sudo dnf module install php:remi-8.2

When prompted, import the GPG key. Finally, install PHP 8.2 and its dependency packages:

sudo dnf -y install php

To verify the installation, use the command:

php --version

This will display the PHP version, confirming that PHP 8.2 is successfully installed on RHEL 9.

Installing PHP Modules

To extend the functionality of PHP, you can install additional PHP modules. To install a single module, use the following syntax:

sudo dnf install php-extension_name

If you need to install multiple modules, you can use the shortened form:

sudo dnf install php-{extension1,extension2}

For example, to install a set of PHP extensions, execute the following command:

sudo dnf install php-{zip,json,pear,mysqlnd, xml,fpm,curl,opcache,intl,cgi}

Accept the prompts to confirm the installation. You can check the installed modules using:

php -modules

This will provide you with a list of the installed PHP modules.

Installing PHP Modules

To extend the functionality of PHP on RHEL 9, you have the option to install additional PHP modules. These modules provide additional features and capabilities that can enhance your web development projects. Installing PHP modules is a straightforward process that can be done using the terminal.

If you need to install a single PHP module, you can use the following command:
<em>sudo dnf install php-extension_name</em>

For example, if you want to install the zip, json, pear, mysqlnd, xml, fpm, curl, opcache, intl, cgi modules, you can use the following command:
<em>sudo dnf install php-{zip,json,pear,mysqlnd,xml,fpm,curl,opcache,intl,cgi}</em>

Installing Multiple PHP Modules:

  1. Open the terminal on your RHEL 9 system.
  2. Run the appropriate command to install the desired PHP modules. Use the <em>sudo dnf install php-{extension1,extension2}</em> syntax to install multiple modules at once.
  3. Follow the prompts and confirm the installation when prompted.
  4. After the installation is complete, you can verify the installed modules by running the command <em>php -m</em>. This will display a list of all installed PHP modules.

Note: It is recommended to only install the modules that you require for your specific project. Installing unnecessary modules can increase the size and complexity of your PHP installation.

By installing the necessary PHP modules, you can customize and expand the capabilities of your PHP development environment on RHEL 9. Whether you need additional functionality for database connectivity, encryption, or other specific requirements, installing PHP modules is a simple and efficient way to enhance your web development projects.

Integrating PHP with Apache Web Server

To fully utilize the power of PHP on your RHEL 9 server, you can integrate it with the Apache web server. This integration allows Apache to parse PHP code and interpret PHP files, enabling dynamic web content. Here's how you can do it:

Step 1: Install Apache

The first step is to install the Apache web server. Open your terminal and run the following command:

sudo dnf install httpd -y

This command will install Apache on your RHEL 9 server.

Step 2: Enable and Start Apache

Once Apache is installed, you need to enable and start the Apache service. Run the following commands:

sudo systemctl enable httpd

sudo systemctl start httpd

These commands will enable and start the Apache service so that it automatically starts whenever your server boots up.

Step 3: Test the Integration

To confirm that PHP is properly integrated with Apache, you can create a sample PHP file. Run the following command to open the file in the nano editor:

sudo nano /var/www/html/info.php

In the file, add the following PHP code:

<?php phpinfo(); ?>

Save the file and exit the editor. Now, you can test the integration by accessing the URL http://your-server-ip/info.php in your web browser. If everything is set up correctly, you should see a page displaying detailed information about your PHP installation.

By integrating PHP with Apache on your RHEL 9 server, you can harness the full potential of PHP for your web applications. Apache's robustness and PHP's versatility make them an excellent combination for creating dynamic and interactive websites.

Integrating PHP with Nginx Web Server

If you prefer to use Nginx as your web server, you can easily integrate PHP with Nginx using PHP-FPM (FastCGI Process Manager). By following the steps below, you'll be able to seamlessly set up PHP on RHEL 9 with Nginx.

Step 1: Install Nginx and PHP-FPM

To begin, install Nginx and PHP-FPM on your RHEL 9 server by running the command: sudo dnf install nginx php-fpm -y. This will ensure that you have both Nginx and PHP-FPM installed simultaneously.

Step 2: Configure Nginx

Stop the Apache service if it's running using the command: sudo systemctl stop httpd. Then, enable and start Nginx and PHP-FPM with the following commands: sudo systemctl enable --now nginx php-fpm. This will ensure that Nginx and PHP-FPM are running and ready to be configured.

Next, you'll need to edit the www.conf file with the command: sudo vim /etc/php-fpm.d/www.conf. In this file, you'll configure Nginx to forward requests to PHP-FPM.

Step 3: Create a Sample PHP File

Create a sample info.php file in the Nginx web root directory using the command: sudo nano /usr/share/nginx/html/info.php. Inside this file, add the PHP code: <?php phpinfo(); ?>. This code will display information about the PHP configuration on your server.

Finally, restart Nginx and PHP-FPM for the changes to take effect by running the command: sudo systemctl restart nginx php-fpm. You can confirm that the configuration is successful by accessing the URL "http://server-ip/info.php" in your web browser.

By integrating PHP with Nginx using PHP-FPM, you can take advantage of the performance benefits of Nginx while enjoying the flexibility and power of PHP for your web applications.

PHP and Linux: A Powerful Combination

When it comes to web development, the combination of PHP and Linux offers a powerful and reliable platform. PHP, as a widely used scripting language, provides developers with the tools they need to create dynamic and performant applications. Linux, on the other hand, is a secure and stable operating system that ensures the smooth running of web applications.

One of the key benefits of using PHP with Linux is the scalability it offers. As your web application grows, Linux provides the necessary infrastructure to handle increased traffic and data processing. Additionally, both PHP and Linux are open source technologies with active development communities, making them easily accessible and well-documented. This means you can find a wealth of resources and support to help you troubleshoot issues or optimize your PHP code on a Linux environment.

"The combination of PHP and Linux provides a reliable environment for deploying performant and secure web applications."

Another advantage of PHP and Linux is their compatibility with popular web servers such as Apache and Nginx. Whether you choose one or the other, PHP seamlessly integrates with both, allowing you to leverage the features and performance benefits of these web servers. With Apache, you can use the mod_php module to parse PHP code and interpret PHP files, while Nginx can be integrated with PHP using PHP-FPM, providing a fast and efficient way to process PHP requests.

Summary:

  • PHP and Linux form a powerful combination for web development.
  • Linux provides a secure and stable environment for running web applications.
  • PHP offers developers the flexibility and performance needed to create dynamic web applications.
  • Both PHP and Linux are open source technologies with active development communities.
  • PHP is compatible with popular web servers like Apache and Nginx, giving you options for deployment.

Conclusion

In conclusion, the process of installing PHP on RHEL 9 is simple and straightforward. By following the step-by-step guide outlined in this article, you can easily install PHP 8.2 on RHEL 9 and integrate it with both Apache and Nginx web servers. PHP and Linux together form a powerful combination for web development, providing developers with the necessary tools to create dynamic and high-performing applications.

Whether you choose Apache or Nginx as your web server, PHP is compatible and can be easily integrated with both. This flexibility allows you to choose the server that best suits your needs and preferences. With PHP and Linux, you have a reliable and scalable platform for building web applications.

PHP and Linux are both open-source technologies with active development communities, ensuring continuous improvement and support. The combination of PHP's versatility and Linux's stability creates an environment that is ideal for deploying secure and performant web applications. With PHP on RHEL 9, you can unlock the full potential of your web development projects.

Sys Admin

Sys Admin

I am a Red Hat Certified Engineer (RHCE) and working as an IT Professional since 2012...