Tutorials > How to run multiple versions of PHP with Apache on AlmaLinux 8

How to run multiple versions of PHP with Apache on AlmaLinux 8

Published on: 09 March 2023

AlmaLinux Hosting PHP

Introduction

The Apache web server uses Virtual Hosts for managing multiple domains on a single instance, as shown in the tutorial on How to configure Virtual Hosts with Apache on AlmaLinux 8.

Thanks to Apache and PHP-FPM, multiple PHP applications can be hosted, each with its own version, and simultaneously managed by a single server.

The application also provides some configurations for reporting output and errors, performing emergency restarts and deploying processes. This allows you to improve the performance of your web space.

Pre-Requisites

The requirements to install PHP-FPM, in your web space, are as follows:

  • AlmaLinux 8 Server with at least 1GB of RAM available
  • Apache web server, correctly set up and configured
  • A domain name that refers to your AlmaLinux 8 server.

Installing PHP versions 7.3 and 7.4 via PHP-FPM

First, install multiple versions of PHP on your server. In the example, version 7.3 and 7.4 have been used.

By adding the remirepo repository, the latest updated versions of PHP can be downloaded on your AlmaLinux 8 system.

Adding the repository:

$ sudo dnf install http://rpms.remirepo.net/enterprise/remi-release-8.rpm

To find out about the latest available versions of PHP, use the command:

$ sudo dnf module list php

An output like this should be shown:

PHP Versions List

Now, proceed by deactivating the PHP module and activating the Remi PHP 7.3 module instead.

To perform the step just explained, use the commands:

$ sudo dnf module reset php
$ sudo dnf module enable php:remi-7.3

Now, install php73 and php73-php-fpm. These components have two different functions:

  • php73 is a metapackage that can be used to run PHP applications
  • php73-php-fpm is an important process-related component that serves to receive FAST/CGI requests.

To install the two components, use the command:

$ sudo dnf install php73 php73-php-fpm -y

The same commands will be launched for the next version, 7.4:

$ sudo dnf module reset php
$ sudo dnf module enable php:remi-7.4
$ sudo dnf install php74 php74-php-fpm -y

After the installation of both versions, enable the php73-php-fpm service to run on startup :

$ sudo systemctl start php73-php-fpm
$ sudo systemctl enable php73-php-fpm

Now check the status of the service using the command:

$ sudo systemctl status php73-php-fpm

In case the service has been successfully activated, an output similar to the following should be shown:

PHP-FPM service status

Do the same for version 7.4, repeating the commands:

$ sudo systemctl start php74-php-fpm
$ sudo systemctl enable php74-php-fpm

Check, as before, the status of the service via:

$ sudo systemctl status php74-php-fpm
PHP-FPM service status

Configuring directories for both sites

Now fill your web space with a root directory and an index page, in order to have a first structure for your sites.

Starting from the folders, use the classic mkdir command of the AlmaLinux terminal:

$ sudo mkdir /var/www/dominio1

$ sudo mkdir /var/www/dominio2

Since Apache uses the apache user and apache group, it is important to set the right permissions:

$ sudo chown -R apache:apache /var/www/dominio1
$ sudo chown -R apache:apache /var/www/dominio2
$ sudo chmod -R 755 /var/www/dominio1
$ sudo chmod -R 755 /var/www/dominio2

While the chown alters the users and groups that will get directory ownership, the chmod command changes the privileges for those directories.

Next, create a file called info.php, which will guide you to a page that will display the PHP version information.

To get started, create this file in the first domain:

$ sudo vi /var/www/dominio1/info.php

Once the command has been executed, the file will be created and opened in a text editor of the terminal, where the phpinfo() function has to be inserted to dedicate that page to the display of information on PHP.

Now, rather than rerunning the whole process, simply copy the file in the second domain's directory:

$ sudo cp /var/www/dominio1/info.php /var/www/dominio2/info.php

Once this is done, the root directory of your domains will have been correctly configured and, connecting to the information page for the PHP version, will also be possible.

Configuring Apache for both sites

Now, it’s time to configure Apache by creating files with the .conf extension. These files must be hosted in the directories /etc/httpd/conf.d/

First, create a new file for domain 1, the one that will require PHP version 7.3:

$ sudo vi /etc/httpd/conf.d/dominio1.conf

Within the file, add this content, which will show various information on the configuration of the Virtual Host:

 <VirtualHost *:80>
 ServerAdmin admin@dominio1
 ServerName dominio1
 DocumentRoot /var/www/dominio1
 DirectoryIndex info.php
 ErrorLog /var/log/httpd/dominio1-error.log
 CustomLog /var/log/httpd/dominio1-access.log combined

 <IfModule !mod_php7.c>
 <FilesMatch \.(php|phar)$>
 SetHandler "proxy:unix:/var/opt/remi/php73/run/php-fpm/www.sock|fcgi://localhost"
 </FilesMatch>
 </IfModule>

</VirtualHost>

Let's analyze some aspects of the semantics of this configuration:

  • DocumentRoot represents the root folder of your site
  • ServerAdmin represents the admin email that will be made available to the webadmin
  • SetHandler points to the socket file for PHP 7.3.

Repeat the process for the second domain, substituting the names and php version from php73 to php74 as appropriate.

$ sudo vi /etc/httpd/conf.d/dominio2.conf

 <VirtualHost *:80>
 ServerAdmin admin@dominio2
 ServerName dominio2
 DocumentRoot /var/www/dominio2
 DirectoryIndex info.php
 ErrorLog /var/log/httpd/dominio2-error.log
 CustomLog /var/log/httpd/dominio2-access.log combined

 <IfModule !mod_php7.c>
 <FilesMatch \.(php|phar)$>
 SetHandler "proxy:unix:/var/opt/remi/php74/run/php-fpm/www.sock|fcgi://localhost"
 </FilesMatch>
 </IfModule>

</VirtualHost>

Once the configuration files have been saved, test if there are any syntax errors within the configurations.

$ sudo apachectl configtest

If everything is correctly configured, some outputs should be shown.

Finally, to actually apply the changes, restart Apache by:

$ sudo systemctl restart httpd

Testing websites

To end this tutorial, test the actual visualization of the two websites created.

Due to the nature of the page, two pages containing PHP version information installed on the domains should be present.

Make sure Apache is running, then, type your site's url in your web browser in the form http://yourdomain

By entering the address into the web browser, reach the Info page for both domains, where various information about the PHP deployed on the relevant domain will be shown.

A page similar to the following is usually shown:

PHP info

For security reasons, removing the two info.php pages created earlier is recommended as they contain sensitive information about your server, which could be vulnerable to a possible attack by malicious users.

To do so, run the two commands:

$ sudo rm -rf /var/www/dominio1/info.php
$ sudo rm -rf /var/www/dominio2/info.php

Conclusions

At the end of the tutorial, you will be able to use Virtual Host and PHP-FPM to deploy multiple websites and multiple different versions of PHP on the same server with AlmaLinux 8.

At this step, the only limitation to deploying as many websites as you want is the resource management of your Apache service.