Digity Republic

Tech News, Reviews, Deals, and How-To's

How to Use PHP-FPM and Apache to Run Multiple PHP Versions on a Single Server Using Ubuntu 20.04

Use PHP-FPM

image of -cloud.

Introduction

Use PHP-FPM: To manage numerous domains on a single instance, the Apache web server makes use of virtual hosts. Similar to this, PHP-FPM manages many PHP versions on a single instance using a daemon. Multiple PHP web applications running various versions of PHP can host simultaneously on the same server using Apache and PHP-FPM. This is helpful since some server stacks, like a routine setup LAMP stack, can only manage one PHP version, even though various apps may need multiple versions. Hosting each program on its own instance is more expensive; combining Apache with PHP-FPM is less expensive.

For heavily trafficked websites, PHP-FPM additionally provides configuration options for stderr and stdout logging, emergency restarts, and adaptive process spawning.

One of the greatest stacks for hosting PHP applications, particularly in terms of performance, is Apache with PHP-FPM. In addition to enabling simultaneous use of several PHP versions, PHP-FPM offers a wide range of additional capabilities including adaptive process spawning, which is beneficial for heavily trafficked websites.

You will set up two PHP sites on a single instance in this tutorial. Each website will have its own domain and utilize a different PHP version. The first will launch PHP 7.2 at the site1.your domain. The second will deploy PHP 7.3 to the site2.your domain.

Prerequisites: Use PHP-FPM

Follow the instructions for Initial Server Setup with Ubuntu 20.04 to set up one Ubuntu 20.04 server with at least 1GB of RAM, a sudo non-root user, and a firewall.
the steps for installing and configuring an Apache web server On Ubuntu 20.04, how to set up the Apache web server
a domain name that is set up to refer to the server running Ubuntu 20.04. Follow How To Point to DigitalOcean Nameservers From Common Domain Registrars to discover how to point domains to DigitalOcean Droplets. We will utilize two subdomains in this tutorial, each of which will have an A record in our DNS settings: sites with your domains, site1, and site2.

1. Installing PHP 7.2 and 7.3 with PHP-FPM is the first step.

You will now install PHP versions 7.2 and 7.3, PHP-FPM, and a number of extra extensions after completing the requirements. However, you must first add the Ondrej PHP repository to your system in order to achieve this.

Software-properties-common may be installed by using the apt-get command:

sudo apt-get install software-properties-common -y

You will add the Ondrej/PHP PPA (Personal Package Archive) repository using the apt-add-repository command-line tool, which is part of the software-properties-common package.

The Ondrej/Php repository has now been added to your system. In addition to having more recent versions of PHP than the official Ubuntu repository, the Ondrej/Php PPA also enables you to install different PHP versions on the same machine:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update -y

Then, use the following instructions to install libapache2-mod-php7.2, libapache2-mod-fcgid, php7.2, php7.2-fpm, and php7.2-mysql:

sudo apt-get install php7.2 php7.2-fpm php7.2-mysql libapache2-mod-php7.2 libapache2-mod-fcgid -y
  • The meta package php7.2 using to run PHP programs.
  • The Fast Process Manager interpreter, which operates as a daemon and accepts Fast/CGI requests, provides php7.2-fpm.
  • PHP is linked to the MySQL database via php7.2-mysql.
  • For the Apache web server, the PHP module is provided by libapache2-mod-php7.2.
  • A mod fcgid is present in libapache2-mod-fcgid and launches several CGI application instances to manage multiple concurrent requests.

Now carry out the same procedure using PHP 7.3. Install libapache2-mod-php7.3, php7.3, php7.3-fpm, and php7.3-mysql.

sudo apt-get install php7.3 php7.3-fpm php7.3-mysql libapache2-mod-php7.3 -y

Start the php7.2-fpm service once both PHP versions have been installed:

sudo systemctl start php7.2-fpm
sudo systemctl status php7.2-fpm

The following output will appear:

Output
● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2020-06-05 11:25:07 UTC; 1min 38s ago
       Docs: man:php-fpm7.2(8)
   Main PID: 13703 (php-fpm7.2)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2353)
     Memory: 6.2M
     CGroup: /system.slice/php7.2-fpm.service
             ├─13703 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
             ├─13719 php-fpm: pool www
             └─13720 php-fpm: pool www

Jun 05 11:25:07 ubuntu systemd[1]: Starting The PHP 7.2 FastCGI Process Manager...
Jun 05 11:25:07 ubuntu systemd[1]: Started The PHP 7.2 FastCGI Process Manager.

After doing so once again, launch the php7.3-fpm service:

sudo systemctl start php7.3-fpm

Next, check the php7.3-fpm service’s status:

sudo systemctl status php7.3-fpm

The following output will appear:

Output
● php7.3-fpm.service - The PHP 7.3 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.3-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2020-06-05 11:26:33 UTC; 56s ago
       Docs: man:php-fpm7.3(8)
    Process: 23470 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.3/fpm/pool.d/www.conf 73 (code=ex>
   Main PID: 23452 (php-fpm7.3)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 2353)
     Memory: 7.1M
     CGroup: /system.slice/php7.3-fpm.service
             ├─23452 php-fpm: master process (/etc/php/7.3/fpm/php-fpm.conf)
             ├─23468 php-fpm: pool www
             └─23469 php-fpm: pool www

Jun 05 11:26:33 ubuntu systemd[1]: Starting The PHP 7.3 FastCGI Process Manager...
Jun 05 11:26:33 ubuntu systemd[1]: Started The PHP 7.3 FastCGI Process Manager.

Your Apache2 service has to have the following modules enabled in order to support different PHP versions:

  • actions use to run CGI scripts based on the request method or media type.
  • A high-performance substitute for mod CGI is fcgid, which launches enough instances of the CGI program to accommodate concurrent requests.
  • The alias allows for URL redirection and mapping of different host filesystem components in the document tree.
  • PHP-FPM may receive requests from Apache thanks to proxy fcgi.

To apply your modifications, restart the Apache service right away:

systemctl restart apache2 with sudo

Two PHP versions currently installing on your server. The next step is to build a directory structure for each website you intend to launch.

Use PHP-FPM: 2. Making Directory Structures for Both Websites in Step 2


For each of your two websites, you will make an index page and a document root directory in this section.

Create document root folders for sites with the domains of your choice: site1. your domain and site2.your domain.

sudo mkdir /var/www/site1.your_domain
sudo mkdir /var/www/site2.your_domain

The www-data user and www-data group are the default users and groups for Apache. Execute the following commands to check that your website root folders have the appropriate ownership and permissions:

sudo chown -R www-data:www-data /var/www/site1.your_domain
sudo chown -R www-data:www-data /var/www/site2.your_domain
sudo chmod -R 755 /var/www/site1.your_domain
sudo chmod -R 755 /var/www/site2.your_domain
sudo nano /var/www/site1.your_domain/info.php
<?php phpinfo(); ?>
sudo cp /var/www/site1.your_domain/info.php /var/www/site2.your_domain/info.php

Now that each website needs document root folders to provide data to visitors, your web server should have them. The next step is setting up Apache to support two distinct PHP versions.

3. Setting Up Apache for Both Websites in Step 3

You will build two virtual host configuration files in this step. Your two websites are able to operate simultaneously with two distinct PHP versions thanks to this.

It is important to construct a virtual host file with the appropriate directives in order for Apache to deliver this content. You will generate two new configuration files inside the directory /etc/apache2/sites-available/ rather than changing the one found at /etc/apache2/sites-available/000-default.conf.

For the website site1.your domain first builds a new virtual host configuration file. Here, you’ll instruct Apache to use php7.2 to render the content:

sudo nano /etc/apache2/sites-available/site1.your_domain.conf

Include the following material. Verify that the server name, PHP version, and website directory path correspond to your configuration:


<VirtualHost *:80>
     ServerAdmin [email protected]_domain
     ServerName site1.your_domain
     DocumentRoot /var/www/site1.your_domain
     DirectoryIndex info.php

     <Directory /var/www/site1.your_domain>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>

    <FilesMatch \.php$>
        # From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
         SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
    </FilesMatch>

     ErrorLog ${APACHE_LOG_DIR}/site1.your_domain_error.log
     CustomLog ${APACHE_LOG_DIR}/site1.your_domain_access.log combined
</VirtualHost>

You changed the ServerAdmin email address so that your domain site administrator may access it and the DocumentRoot to your new directory in this file. Additionally, you added a SetHandler directive to enable PHP to function as a FastCGI process server, and you changed ServerName, which specifies the base domain for this virtual host configuration.

Save the document, then exit.

For website site2.your domain, make a new virtual host configuration file next. To install php7.3, you must specify the following subdomain:

sudo nano /etc/apache2/sites-available/site2.your_domain.conf

Include the following material. Once more, confirm that your specific information matches the website directory path, server name, and PHP version:

<VirtualHost *:80>
     ServerAdmin [email protected]_domain
     ServerName site2.your_domain
     DocumentRoot /var/www/site2.your_domain
     DirectoryIndex info.php

     <Directory /var/www/site2.your_domain>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>

    <FilesMatch \.php$>
        # 2.4.10+ can proxy to unix socket
         SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
    </FilesMatch>

     ErrorLog ${APACHE_LOG_DIR}/site2.your_domain_error.log
     CustomLog ${APACHE_LOG_DIR}/site2.your_domain_access.log combined
</VirtualHost>

When you’re done, save and shut the file. Then, look for syntax mistakes in the Apache configuration file:

sudo apachectl configtest

You’ll see the following output:

Output
Syntax OK
sudo a2ensite site1.your_domain
sudo a2ensite site2.your_domain
sudo a2dissite 000-default.conf
sudo systemctl restart apache2

You will test each site after setting up Apache to serve it to ensure that the appropriate PHP versions are installing

4. Testing both websites is step four.

You have now set up two websites to use two distinct PHP versions. Check the outcomes now.

Observe the titles. The first page states that PHP version 7.2 was used on site1.your domain. The second shows that PHP version 7.3 was using site2.your domain.

Remove the info.php files after testing your websites. They provide a security issue because they give unauthorized users access to private information about your server. Make use of the following commands to delete both files:

sudo rm -rf /var/www/site1.your_domain/info.php
sudo rm -rf /var/www/site2.your_domain/info.php

You now manage two websites using two distinct PHP versions on a single Ubuntu 20.04 server. But PHP-FPM is not just for this one application.

Conclusion: Use PHP-FPM

Now that virtual hosts and PHP-FPM have been merged, a single server may serve a number of websites and PHP versions. The processing power of your instance is the only real restriction on the number of PHP sites and PHP versions that your Apache server can support.

From here, you could look into PHP-more FPM’s complex capabilities. Such as its adaptive spawning mechanism or how it can log stdout and stderr, or you could start protecting your websites. You may achieve this by using our instructions on how to protect your websites using Let’s Encrypt’s free TLS/SSL certificates.

Frequently Asked Questions:

How can I use Linux to run several PHP versions?

Introduce a PHP repository

PHP 7.4 is preinstalled by default in Ubuntu 20.04. Therefore, in order to install the various PHP versions, you must add the PHP repository to your system. You can install several PHP versions when the repository is updating.

Can I run many instances of PHP simultaneously?

Multiple PHP web applications running various versions of PHP can host simultaneously on the same server using Apache and PHP-FPM.

Can you install several versions of PHP?

You might need to utilize one version of PHP while running numerous PHP versions on various domains. when utilizing various applications on the same account. For instance, you may utilize PHP 5.2 in a subfolder and PHP 5.4 for the site’s public HTML.

Editor’s Recommendations:

Do You Need More Help?

Visit our forum, describe your question briefly and get answers