Site icon CentLinux

Install Multiple versions of PHP on RHEL 8

Share on Social Media

Learn how to install multiple versions of PHP on RHEL 8 with this detailed guide. Manage different PHP environments on a single server to meet diverse application requirements seamlessly. #centlinux #linux #php

What is PHP?

PHP (Hypertext Preprocessor) is a widely-used open-source scripting language designed primarily for web development. It is embedded into HTML and is especially suited for creating dynamic and interactive web pages. Here are some key features and aspects of PHP:

  1. Server-Side Scripting: PHP code is executed on the server, and the result is sent to the client’s web browser as plain HTML. This allows for the creation of dynamic web pages that can change content based on user interactions or other variables.
  2. Ease of Use: PHP is known for its simplicity and ease of learning, especially for those with a background in HTML. Its syntax is similar to C, Java, and Perl, making it accessible for many programmers.
  3. Integration: PHP can be easily integrated with various databases, such as MySQL, PostgreSQL, Oracle, and SQLite, making it a powerful tool for creating database-driven web applications.
  4. Extensive Functionality: PHP has a vast standard library and numerous extensions, allowing developers to perform a wide range of tasks, from file handling to data encryption and image processing.
  5. Cross-Platform: PHP runs on most web servers and operating systems, including Apache, Nginx, Windows, Linux, and macOS, ensuring flexibility and broad compatibility.
  6. Community and Support: Being open-source, PHP has a large and active community of developers who contribute to its development and provide extensive documentation, frameworks, and tools.
  7. Frameworks: There are several popular PHP frameworks, such as Laravel, Symfony, and CodeIgniter, that provide pre-built modules and a structured approach to developing robust and scalable applications.

PHP is a versatile language that powers many popular websites and applications, making it a fundamental technology for web developers around the world.

Install Multiple versions of PHP on RHEL 8

Problem Definition

It is a very common scenario for web hosting companies to host websites of multiple PHP versions. To implement it, they use different Apache handlers, according to their requirements. The two most commonly used implementations are DSO and PHP-FPM.

Although tradional DSO (mod_php) handler is simple to implement and the fastest way to run PHP web applications, but it has its own pros and cons like DSO was not built to install multiple versions of PHP.

FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features (mostly) useful for heavy-loaded sites. Using this handler, the system will run PHP scripts as the user that owns the domain/subdomain. Each FPM pool can have independent settings.

In this guide, you will see how to install multiple versions of PHP on a single Apache web servers by using PHP-FPM and mod_fcgid handler.

Recommended Training: Master Laravel 11 & PHP: From Beginner to Advanced from Piotr Jura

Linux Server Specification

We are utilizing a minimal installation of Red Hat Enterprise Linux (RHEL) 8 on a virtual machine, which is configured with the following specifications to ensure optimal performance and resource allocation.

Install Third Party Yum Repositories

To begin, establish an SSH connection to the server apache-01.centlinux.com as the root user using your preferred SSH client.

Once connected, proceed to install the EPEL (Extra Packages for Enterprise Linux) repository, which provides additional packages not included in the default RHEL repositories. You can do this by running the following command.

# dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

In this setup, we need to install multiple versions of PHP, specifically PHP 5.6 and PHP 7.4, as different applications may require different PHP versions. These versions are readily available through the Remi Yum repository, which provides a wide range of PHP versions and other essential packages.

To access these versions, we will first install the Remi Yum repository on our RHEL 8 server. This is done using the dnf package manager, ensuring that we can easily install and manage the required PHP versions.

# dnf install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm

Build yum cache for newly installed yum repositories.

# dnf makecache
Updating Subscription Management repositories.
Extra Packages for Enterprise Linux Modular 8 - 2.2 kB/s | 9.5 kB 00:04
Extra Packages for Enterprise Linux 8 - x86_64 1.9 kB/s | 6.0 kB 00:03
Red Hat Enterprise Linux 8 for x86_64 - BaseOS 1.4 kB/s | 4.1 kB 00:02
Red Hat Enterprise Linux 8 for x86_64 - AppStre 2.1 kB/s | 4.5 kB 00:02
Remi's Modular repository for Enterprise Linux 26 kB/s | 740 kB 00:27
Safe Remi's RPM repository for Enterprise Linux 213 kB/s | 1.7 MB 00:07
Metadata cache created.

Install Apache Web Server on RHEL 8

In Red Hat-based Linux distributions, the Apache web server is provided through the httpd software package, which contains all the necessary components to run Apache.

To enable web services on your server, it is essential to install this package. By installing httpd, you will be able to configure and manage Apache, allowing your system to serve web pages and handle HTTP requests.

# dnf install -y httpd

Install Multiple versions of PHP on RHEL 8

Installing multiple versions of PHP on your system is straightforward when using the Remi Yum repository, which offers a wide range of PHP versions for various application requirements.

In this case, we will be installing PHP 5.6, which is currently the oldest available version in the repository. Despite being an older version, PHP 5.6 may still be required for compatibility with certain legacy applications or systems.

# dnf install -y php56 php56-php-fpm

In addition to PHP 5.6, we are also installing PHP 7.4, which is currently the latest stable version available in the Remi Yum repository. PHP 7.4 brings several performance improvements, security updates, and new features, making it a preferred choice for modern applications and ensuring better compatibility with newer software frameworks.

# dnf install -y php74 php74-php-fpm

You are required to create script wrappers, to call the required version of PHP based on user request.

Execute following commands to create PHP script wrappers.

# cat > /var/www/cgi-bin/php56.fcgi << EOF
> #!/bin/bash
> exec /bin/php56-cgi
> EOF

# cat > /var/www/cgi-bin/php74.fcgi << EOF
> #!/bin/bash
> exec /bin/php74-cgi
> EOF

Add execution permissions on these script files.

# chmod 755 /var/www/cgi-bin/php*

Now add Apache virtual host configurations to run multiple versions of PHP websites on different ports. You can also run Virtual Hosts on different Domain names, if you have configured a authoritative DNS server for your network.

Create a configuration for PHP 5.6 based virtual host.

# vi /etc/httpd/conf.d/php56.conf

Add following directives in this file.

Listen 8056

<VirtualHost *:8056>
     ServerAdmin root@localhost.com
     ServerName 56.test
     DocumentRoot /var/www/html
     DirectoryIndex index.php index.html
<FilesMatch ".php$">
  <If "-f %{REQUEST_FILENAME}">
      SetHandler "proxy:unix:/var/opt/remi/php56/run/php-fpm/www.sock|fcgi://localhost"
  </If>
</FilesMatch>
     ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
     AddHandler php56-fcgi .php
     Action php56-fcgi /cgi-bin/php56.fcgi
 <Directory  "/var/www/html">
    AllowOverride All
  </Directory>
</VirtualHost>

Similarly, Add configurations for PHP 7.4 base virtual host.

# vi /etc/httpd/conf.d/php74.conf

Add following directives therein.

Listen 8074

<VirtualHost *:8074>
     ServerAdmin root@localhost.com
     ServerName 74.test
     DocumentRoot /var/www/html
     DirectoryIndex index.php index.html
<FilesMatch ".php$">
  <If "-f %{REQUEST_FILENAME}">
      SetHandler "proxy:unix:/var/opt/remi/php74/run/php-fpm/www.sock|fcgi://localhost"
  </If>
</FilesMatch>
     ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
     AddHandler php74-fcgi .php
     Action php74-fcgi /cgi-bin/php74.fcgi
 <Directory  "/var/www/html">
    AllowOverride All
  </Directory>
</VirtualHost>

Create a default index for your website.

# vi /var/www/html/index.php

And add following PHP code in this file.

<?php
phpinfo();
?>

To ensure that the Apache web server can properly start and serve content on both virtual host ports, you need to add these ports to the SELinux http_port_t port type. This step is necessary because SELinux enforces security policies that restrict which ports can be used by web services.

By adding the ports to this specific port type, you’re allowing Apache to bind and listen on these ports without encountering security restrictions, thus enabling the web server to start and function correctly on the configured ports.

# semanage port -a -t http_port_t -p tcp 8056
# semanage port -a -t http_port_t -p tcp 8074

To ensure that the Apache web server can communicate through the specified ports, it is essential to allow these service ports in the Linux firewall. By configuring the firewall rules to permit traffic on the designated ports, you ensure that incoming requests can reach the web server, enabling proper communication with clients.

This step is crucial for ensuring that the firewall does not block access to your web services, allowing them to function seamlessly in a production environment.

# firewall-cmd --permanent --add-port={8056,8074}/tcp
success
# firewall-cmd --reload
success

Enable and start the Apache and PHP-FPM services.

# systemctl enable --now httpd.service php56-php-fpm.service php74-php-fpm.service
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service â /usr/lib/systemd/system/httpd.service.
Created symlink /etc/systemd/system/multi-user.target.wants/php56-php-fpm.service â /usr/lib/systemd/system/php56-php-fpm.service.
Created symlink /etc/systemd/system/multi-user.target.wants/php74-php-fpm.service â /usr/lib/systemd/system/php74-php-fpm.service.

Open URL http://apache-01.centlinux.com:8056 in a web browser.

PHPInfo 5.6

Open URL http://apache-01.centlinux.com:8074 in a web browser.

PHPInfo 7.4

Final Thoughts

Installing multiple versions of PHP on RHEL 8 allows you to manage various PHP environments on a single server, catering to different application requirements efficiently. This setup ensures compatibility and flexibility, enabling seamless development and deployment.

Your Linux servers deserve expert care! I provide reliable management and optimization services tailored to your needs. Discover how I can help on Fiverr!

Let’s ensure your server is equipped to handle diverse PHP applications effortlessly!

Exit mobile version