How to install newer PHP versions on our Debian 9 (Stretch) LAMP server in PHP-FPM mode (page 2)

botond published 2019/09/28, Sat - 23:40 time

Content

  1. page: Installing the sury.org repository and installing various PHP versions
  2. page: Combined use of different PHP-FPM versions on a single server

 

2. page content

 

Continuation

The description on the first page We have added a deb.sury.org repository, from which we installed all available PHP version. On this page, we run these versions of PHP on the test server, which run the PHP code of our web pages in parallel but independently of each other.

 

 

Basic settings

First, we quickly make the necessary settings to run websites through PHP-FPM. First we set the Apache-him.

Configuring Apache

Turn on the Apache modules recommended during PHP installation, and then restart Apache:

a2enmod proxy_fcgi setenvif
systemctl restart apache2

In a previous description already we have set up PHP-FPM on Debian 8 LAMP, we will still do similar tasks, but there are many different ones here PHP-FPM version.

 

Easy to use: Use one version of PHP-FPM

If you’re already here, we’ll also take a look at the easy-to-use mode, where you just need to enable the appropriate PHP version of FPM (global PHP-FPM usage).

The task

Suppose you want to run one or more websites on the server, but it is enough for us to use only one version of PHP - the one previously installed - and it does not matter that the php codes of each website are on behalf of the same (www-data) user to run because, for example, the websites are owned by one, so there is no security risk. This PHP version should be say 7.3, which is the current latest active release branch.

The solution

If you want to use only one PHP-FPM version at a time, then the simple thing is to turn on the appropriate PHP version FPM configuration. You can find them here:

ls -al /etc/apache2/conf-available

In this directory you will find the available Apache configurations, of which phpx.y-fpm.conf we deal with:

php5.6-fpm.conf
php7.0-fpm.conf
php7.1-fpm.conf
php7.2-fpm.conf
php7.3-fpm.conf
php7.4-fpm.conf

Let's look at one, say,

nano php7.3-fpm.conf
# Redirect to local php-fpm if mod_php is not available
<IfModule !mod_php7.c>
<IfModule proxy_fcgi_module>
    # Enable http authorization headers
    <IfModule setenvif_module>
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
    </IfModule>

    <FilesMatch ".+\.ph(ar|p|tml)$">
        SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
    </FilesMatch>
    <FilesMatch ".+\.phps$">
        # Deny access to raw php sources by default
        # To re-enable it's recommended to enable access to the files
        # only in specific virtual host or directory
        Require all denied
    </FilesMatch>
    # Deny access to files without filename (e.g. '.php')
    <FilesMatch "^\.ph(ar|p|ps|tml)$">
        Require all denied
    </FilesMatch>
</IfModule>
</IfModule>

It starts here with a comment describing that this config performs a redirect if mod_php is not available on the system. And in the second line of the config, a condition takes care of this, which allows the code block below to run only if mod_php7 is not available.

To use this, you first need to disable mod_php, which will no longer be available on the server. That's okay, because mod_php is a very outdated server API, which we better avoid today. So turn it off:

a2dismod php7.0
Here we have to disable a different module for different Debian versions, for example "php9" for Debian 7.0, "php10" for Debian 7.3 and "php8" for Debian 5.

Then enable the above php7.3 configuration:

a2enconf php7.3-fpm

Then let's restart Apache:

systemctl reload apache2

Of course, the previous ones could have been described in 3 command lines, in which we turn the elements on and off, but I believe that if we look behind things, we can see the connections much easier to understand how the system works and remember the course of these settings sooner, and why. Secondly, the config file opened above will also come in handy due to the use of its different parts.

Then create a phpinfo query file:

/var/www/html/phpinfo.php

Then add the following:

<?php
    phpinfo();
?>

Finally, load it into your browser or server IP address, or if directed our domain name eg. in our hosts file, then with:

PHP Test - Version and PHP-FPM Server API

Here you can see mainly the 7.3 version of our working PHP environment and the page running with PHP-FPM. Scrolling down we also see the Linux user running the page:

PHP Test - User running the site

which in this case is www-data.

So if you create more web pages that we don't do virtualhosztIn particular custom settings, each page will start with the same PHP environment.

 

 

Operate multiple web pages with separate PHP versions

In this section, we operate multiple websites with different PHP versions and different users, so the pages are completely separate and do not pose a security risk to each other.

The task

Suppose - keeping our previous settings - we need two more websites, one of which is an old website that contains old PHP code, so it can only work with PHP version 5. Let's have that name oregweboldal.local. And we need another site where we want to make the PHP 7.x code of our other website compatible with the new PHP 7.4 system, and that requires a test environment with PHP 7.4. Let it be called kiserletioldal.local. Both web pages must run with PHP-FPM and their own users, so that the default 7.3 PHP system that we have set up so far remains functional in its current web root.

Of course, we can create separate websites for each installed PHP system, I just don't want to go into the description unnecessarily long by making many similar settings, and this lifelike setup is a good illustration of how different generations of PHP run simultaneously.

The solution

Create users and web root directories

First, create two users for the web pages that will run the PHP code on their behalf:

adduser oregweboldal
adduser kiserletioldal

Give both of them a password and answer the questions asked (just press enter for these questions for simplicity).

Then create the web root directories for the pages, which will be fine under / var / www (where we also have the original Apache virtual host with the "html" directory):

mkdir /var/www/oregweboldal
mkdir /var/www/kiserletioldal

This fits nicely the three web roots in a main directory. We are now transferring ownership of these directories to the two new users:

chown oregweboldal:oregweboldal /var/www/oregweboldal/
chown kiserletioldal:kiserletioldal /var/www/kiserletioldal/

After that, our / var / www directory looks like this:

drwxr-xr-x 2 root           root           4,0K jún   19 22:32 html
drwxr-xr-x 2 kiserletioldal kiserletioldal 4,0K szept 28 18:27 kiserletioldal
drwxr-xr-x 2 oregweboldal   oregweboldal   4,0K szept 28 18:27 oregweboldal

Creating PHP-FPM Pools

Create PHP-FPM Pools for Websites with the Right Version:

old Website

To operate this page, we designed PHP5, so enter the PHP-FPM pool directory, make a copy of your default www pool, and open it for editing:

cd /etc/php/5.6/fpm/pool.d
cp www.conf oregweboldal.conf
nano oregweboldal.conf

Configure a few things in the config file as follows:

[oregweboldal]

[...]

user = oregweboldal
group = oregweboldal

[...]

listen = /run/php/php5.6-oregweboldal-fpm.sock

[...]

The first option in square brackets is the name of our pool, and after setting the user and group, we will set up our future socket file in the listen queue, which will be created after restarting PHP-FPM.

No other setup here, save the file, restart this version of PHP-FPM, and check it again:

systemctl restart php5.6-fpm
systemctl status php5.6-fpm

Here we see that PHP5.6 FPM works, and besides the basic www pool we have our newly created pool with its own 2 process:

 php5.6-fpm.service - The PHP 5.6 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php5.6-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-09-28 18:52:49 CEST; 26s ago
     Docs: man:php-fpm5.6(8)
 Main PID: 6260 (php-fpm5.6)
   Status: "Processes active: 0, idle: 4, Requests: 0, slow: 0, Traffic: 0req/sec"
    Tasks: 5 (limit: 4915)
   CGroup: /system.slice/php5.6-fpm.service
           ├─6260 php-fpm: master process (/etc/php/5.6/fpm/php-fpm.conf)
           ├─6261 php-fpm: pool oregweboldal
           ├─6262 php-fpm: pool oregweboldal
           ├─6263 php-fpm: pool www
           └─6264 php-fpm: pool www

szept 28 18:52:49 debian9 systemd[1]: Starting The PHP 5.6 FastCGI Process Manager...
szept 28 18:52:49 debian9 systemd[1]: Started The PHP 5.6 FastCGI Process Manager.

From now on, we can reference our socket file set in the listen queue and then our Apache virtual host.

Experiment page

We do the same for this page, just here with 7.4 PHP:

cd /etc/php/7.4/fpm/pool.d
cp www.conf kiserletioldal.conf
nano kiserletioldal.conf

Here, too, set the appropriate values ​​as above:

[kiserletioldal]

[...]

user = kiserletioldal
group = kiserletioldal

[...]

listen = /run/php/php7.4-kiserletioldal-fpm.sock

[...]

Save and then restart the appropriate FPM and verify:

systemctl restart php7.4-fpm
systemctl status php7.4-fpm
 php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-09-28 19:09:37 CEST; 4s ago
     Docs: man:php-fpm7.4(8)
 Main PID: 6643 (php-fpm7.4)
   Status: "Ready to handle connections"
    Tasks: 5 (limit: 4915)
   CGroup: /system.slice/php7.4-fpm.service
           ├─6643 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
           ├─6644 php-fpm: pool kiserletioldal
           ├─6645 php-fpm: pool kiserletioldal
           ├─6646 php-fpm: pool www
           └─6647 php-fpm: pool www

szept 28 19:09:37 debian9 systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
szept 28 19:09:37 debian9 systemd[1]: Started The PHP 7.4 FastCGI Process Manager.

Here, too, our new pool started off nicely.

 

 

Creating Apache Virtual Hosts

In order to access our web pages from the outside world, we need to create virtual hosts. We've done this before, we create it the same way now, so we won't go into the details of the settings here.

old Website

Go to the Apache directory of available web site configurations:

cd /etc/apache2/sites-available

Then create a new file with oregweboldal.local for our site:

nano oregweboldal.local.conf

and put the following configuration:

<VirtualHost *:80>
	ServerName oregweboldal.local
	ServerAlias www.oregweboldal.local

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/oregweboldal

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

	<IfModule mod_proxy_fcgi.c>
		<Directory /var/www/oregweboldal>
			DirectoryIndex index.php index.html
			<FilesMatch "\.(php)$">
				SetHandler "proxy:unix:/run/php/php5.6-oregweboldal-fpm.sock|fcgi://localhost"
			</FilesMatch>		
		</Directory>
	</IfModule>
</VirtualHost>

Save and enable the web page, and then restart Apache:

a2ensite oregweboldal.local.conf
systemctl restart apache2
Experiment page

Here we do the same:

cd /etc/apache2/sites-available
nano kiserletioldal.local.conf

Let's put the right settings here too:

<VirtualHost *:80>
	ServerName kiserletioldal.local
	ServerAlias www.kiserletioldal.local

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/kiserletioldal

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

	<IfModule mod_proxy_fcgi.c>
		<Directory /var/www/kiserletioldal>
			DirectoryIndex index.php index.html
			<FilesMatch "\.(php)$">
				SetHandler "proxy:unix:/run/php/php7.4-kiserletioldal-fpm.sock|fcgi://localhost"
			</FilesMatch>		
		</Directory>
	</IfModule>
</VirtualHost>

Then you can enable the website and restart Apache:

a2ensite kiserletioldal.local.conf
systemctl restart apache2

Set up a hosts file

If you have multiple virtual hosts on a server, that is, a web page, Apache a server-based domain namewhich virtual host configuration to execute. If you do not have the correct domain names, you will need to set the IP address - domain name relationships in the operating system hosts file on the client page.

  • If you are browsing Linux, open the / Etc / hosts file
  • or if you want to access our server 's web pages from Windows, open it in Windows c: \ Windows \ System32 \ drivers \ etc \ hosts file

then do the following to access the web pages you just created:

192.168.1.120   oregweboldal.local
192.168.1.120   www.oregweboldal.local

192.168.1.120   kiserletioldal.local
192.168.1.120   www.kiserletioldal.local

Of course, replace the IP address with the address of our own server.

Placing phpinfo files and testing pages

So far, we're done with everything, with only pages left to test. The easiest way to do this is to run the standard phpinfo () PHP function.

old Website

Create the standard phpinfo.php file in the web root of the page:

nano /var/www/oregweboldal/phpinfo.php

Let's include the standard content:

<?php
    phpinfo();
?>

Save and then load this file on the client machine in our browser under the web address of the page:

Website Testing: Old Website - Run Phpinfo 1.

The phpinfo page comes in, where you can see the 5.6.40 PHP version and the PHP-FPM operation above. Scroll down to see the user running the file and its environment settings:

Website Testing: Old Website - Run Phpinfo 2.

Experiment page

Proceed in a similar way here:

nano /var/www/kiserletioldal/phpinfo.php
<?php
    phpinfo();
?>

Then also load this page:

Website Testing: Experiment Page - Run Phpinfo 1.

Here, too, we have the planned output: PHP Version 7.4.0beta4 + PHP + FPM. Then below:

Website Testing: Experiment Page - Run Phpinfo 2.

Here you can see the "experiment page" of the user running the script and its environment parameters.

Also, if we reload the setup test at the beginning of this tutorial, it will still be accessible by the server's default virtual host, where PHP7.3 will still work.

 

 

Use multiple versions of PHP in different subdirectories of a single web page

And finally, an even rarer case is when you have a website under one domain name and within that you want to use multiple versions of PHP. It may seem strange at first, but there may be situations where we need such a solution. For example, if you don’t want to buy multiple domain names, but only make multiple websites or web applications available with a single existing one that require separate PHP - all from multiple subdirectories of a single web host.

The task

This section is also built on the experimental page of the previous chapter to avoid further unnecessary settings.

So the current task is to create a subdirectory within the experimental page, in which the PHP system 7.2 will serve our server-side php script files, and a different version of PHP running in the subdirectory will be run with the same user.

The solution

Create subdirectory and set permissions

Go to the experiment root web root and create a subdirectory in it:

cd /var/www/kiserletioldal/
mkdir mkdir php7-2

Then set your permission:

chown kiserletioldal:kiserletioldal php7-2/
It is important to note here that although we could use a different user for this purpose, if we are already moving on a webhost, it is advisable to configure everything with the same user so that later interactions between the systems in the storage (eg inter-system file operations, etc.) , let's not get into trouble with eligibility.

Create a PHP-FPM pool

Create a PHP-FPM Pool 7.2 for PHP running in a subdirectory:

cd /etc/php/7.2/fpm/pool.d/
cp www.conf kiserletioldal-php7-2.conf

I just named this conf file for the sake of consistency so we can later know where it belongs, etc. Open for editing:

nano kiserletioldal-php7-2.conf

And here are some of the familiar parts:

[kiserletioldal-php7-2]

[...]

user = kiserletioldal
group = kiserletioldal

[...]

listen = /run/php/php7.2-kiserletioldal-php7-2-fpm.sock

[...]

Here, too, you might want to name your socket file based on a similar scheme. Save and restart FPM 7.2:

systemctl restart php7.2-fpm

You can also check:

systemctl status php7.2-fpm
 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 Sat 2019-09-28 20:54:45 CEST; 6s ago
     Docs: man:php-fpm7.2(8)
 Main PID: 7870 (php-fpm7.2)
   Status: "Ready to handle connections"
    Tasks: 5 (limit: 4915)
   CGroup: /system.slice/php7.2-fpm.service
           ├─7870 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
           ├─7871 php-fpm: pool kiserletioldal-php7-2
           ├─7872 php-fpm: pool kiserletioldal-php7-2
           ├─7873 php-fpm: pool www
           └─7874 php-fpm: pool www

szept 28 20:54:44 debian9 systemd[1]: Starting The PHP 7.2 FastCGI Process Manager...
szept 28 20:54:45 debian9 systemd[1]: Started The PHP 7.2 FastCGI Process Manager.

Our pool is going nicely ...

Modify Apache virtual host

Here we are not creating a new virtual host, but modifying the experimental page. Open for editing:

nano /etc/apache2/sites-available/kiserletioldal.local.conf

And let’s add the new detail below to “ "after the line:

		# php7-2 alkönyvtár beállítása
		<Directory /var/www/kiserletioldal/php7-2>
			DirectoryIndex index.php index.html
			<FilesMatch "\.(php)$">
				SetHandler "proxy:unix:/run/php/php7.2-kiserletioldal-php7-2-fpm.sock|fcgi://localhost"
			</FilesMatch>
		</Directory>

to make the whole file look like this:

<VirtualHost *:80>
	ServerName kiserletioldal.local
	ServerAlias www.kiserletioldal.local

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www/kiserletioldal

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

	<IfModule mod_proxy_fcgi.c>
		<Directory /var/www/kiserletioldal>
			DirectoryIndex index.php index.html
			<FilesMatch "\.(php)$">
				SetHandler "proxy:unix:/run/php/php7.4-kiserletioldal-fpm.sock|fcgi://localhost"
			</FilesMatch>		
		</Directory>

		# php7-2 alkönyvtár beállítása
		<Directory /var/www/kiserletioldal/php7-2>
			DirectoryIndex index.php index.html
			<FilesMatch "\.(php)$">
				SetHandler "proxy:unix:/run/php/php7.2-kiserletioldal-php7-2-fpm.sock|fcgi://localhost"
			</FilesMatch>
		</Directory>

	</IfModule>
</VirtualHost>

Save and then restart Apache:

systemctl restart apache2

 

 

Testing a subdirectory

Finally, create a phpinfo.php file in this subdirectory:

nano /var/www/kiserletioldal/php7-2/phpinfo.php

And let's add the usual lines:

<?php
    phpinfo();
?>

Then load the script into your browser from the subdirectory:

Website Testing: Experiment Page Sub - Run Phpinfo 1.

Below is the user and the environment variables:

Website Testing: Experiment Page Sub - Run Phpinfo 2.

What may be even more interesting here is that PHP sees "DOCUMENT_ROOT" here as the document_root of the original website, since this environment also comes from the same virtual host as the experiment page itself.

It is also worth noting here that if you include .php files (or vice versa) from another subdirectory that uses PHP in php, it will run with the same PHP version and environment as the one you called from, because the other .php file in this case, it is not Apache that redirects to the appropriate FPM, but the part of the PHP processor that is already running loads the other .php file, which inherits the environment of the file that was originally run. So if you want to use two (or even more) different versions of PHP within a web application, load the .php files from the subdirectory within the application using AJAX technology, so that they are reloaded from the browser via Apache using the appropriate PHP-FPM -el. For example, you can share the database from two separate PHPs, or load your different config files, or perform other file operations with the two separate versions at the same time. And if you also want to share internal variables between php files running two versions, you can use JSON or other structured data, such as We pass it in POST to the other php.

 

Conclusion

So these would be the different versions of PHP and their combined use within a server or even within a web page. Of course, we can launch any number of new pools from any version according to our individual needs.

sury.org luggage compartment and it is constantly being updated, so we don't have to worry about missing out on updates due to our different versions of PHP. However, it doesn’t hurt to keep in mind the fact that it’s an external storage that is maintained by a small group or even just one person. so if anything happens to the repository operators, we will no longer receive updates to our software installed from the repository. Therefore, we should also be able to compile software from source ourselves - in this case different versions of PHP - so that if we accidentally lose this great opportunity, we can provide ourselves with the necessary versions of PHP.

 

 

Navigation

This description consists of several pages: