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
Table of Contents
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:
- 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.
- 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.
- 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.
- 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.
- Cross-Platform: PHP runs on most web servers and operating systems, including Apache, Nginx, Windows, Linux, and macOS, ensuring flexibility and broad compatibility.
- 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.
- 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.
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 Online Training: Learn Bash Shell in Linux for Beginners
Environment Specification
We are using a minimal RHEL 8 virtual machine with following specifications.
- CPU – 3.4 Ghz (2 cores)
- Memory – 2 GB
- Storage – 60 GB
- Operating System – RHEL 8.3
- Hostname – apache-01.centlinux.com
- IP Address – 192.168.116.206 /24
Install Third Party Yum Repositories
Connect with apache-01.centlinux.com as root user by using a ssh client.
Install EPEL (Extra Packages for Enterprise Linux) yum repository by using following command.
# dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
Here, we need to install multiple versions of PHP i.e. PHP 5.6 and PHP 7.4. Both of these versions are available in Remi Yum repository. Therefore, we are installing Remi yum repository on our RHEL 8 server by using dnf command.
# 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, Apache web server is provided with httpd software package. Therefore, you are required to install it to enable web services.
# dnf install -y httpd
Install Multiple versions of PHP on RHEL 8
You can easily install multiple versions of PHP from Remi yum repository. We are installing PHP 5.6, the oldest available version right now.
# dnf install -y php56 php56-php-fpm
We are also installing the PHP 7.4, the latest available version right now.
# 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(); ?>
Add both virtual host ports to SELinux http_port_t port type. So, the Apache web server can start service on these ports.
# semanage port -a -t http_port_t -p tcp 8056
# semanage port -a -t http_port_t -p tcp 8074
Allow these service ports in Linux firewall.
# 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.
Open URL http://apache-01.centlinux.com:8074 in a web browser.
If you are new to Linux and facing difficulty in working at Linux Bash prompt. We recommend that, you should read The Linux Command Line, 2nd Edition: A Complete Introduction by William Shotts.
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.
If you need professional assistance with installing and configuring multiple PHP versions on your RHEL 8 system, I offer expert services to help you achieve a smooth and efficient setup. Visit my Fiverr gig to learn more about how I can assist you in optimizing your PHP environment. Let’s ensure your server is equipped to handle diverse PHP applications effortlessly!