16
June
2023
15:42

How to install PHP 7.0, PHP 7.1, PHP 7.2, PHP 7.3 or PHP 7.4 in Ubuntu 22.04 "Jammy Jellyfish" or Linux Mint 21.1 "Vera" and switch the version

16 June 2023 15:42

In the new version of Ubuntu 22.04, the installation from the ONDREJ repository of the old versions of PHP is related to difficulties.

Background

I wanted to install a decrease in the PHP version from 8.1 on PHP 7.4, but it turned out to be difficult on the fresh version of Ubuntu "Jammy" (Linux Mint "Vera").
In recent versions, PHP 8.1 is made exclusively, when searching for the Apt-Cache Search PHP7, nothing is.

Adding developer repositories, I completed the team according to the instructions:

sudo add-apt-repository ppa:ondrej/apache2

sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php

error
GPG: failure when receiving keys from the server: unacceptable URI

When performing

sudo apt update

Errors were issued:

W: Bug GPG: https://ppa.launchpadContent.net/ondrej/apache2/ubuntu Jammy Inrelease: The following signatures cannot be tested, as the open key is not available: no_pubkey 4f4eaae5267a6c
E: The repository “https://ppa.launchpadcontent.net/ondrej/apache2/ubuntu Jammy Inrelease” has not been signed.

W: Bug GPG: https://ppa.launchpadContent.net/ondrej/php/ubuntu Jammy Inrelease: The following signatures cannot be checked, as the open key is not available: no_pubkey 4f4eae5267a6c
E: The repository “https://ppa.launchpadcontent.net/ondrej/php/ubuntu Jammy Inrelease” has not been signed.

I tried to add a signature key to the system using the commands, but the error has been preserved:

gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv 4F4EA0AAE5267A6C
gpg --export 4F4EA0AAE5267A6C | sudo tee /etc/apt/trusted.gpg.d/php_ondrej.gpg > /dev/null

But the APT Update team gave an error, since both repositories of the ONDREJ were disconnected in the system, because Not signed. I did not find the GPG key ligament for the GPG keys for the Oandrej repository. Accordingly, to execute the SUDO GPG --import command Name_fila.gpg was not possible.

Key bunch that is at the end of the page https://www.php.net/gpg-keys.php не помогла (sudo sudo gpg --import php-keyring.gpg), т.е. в файле gpg оказались ключи других разработчиков.

Solution

It is necessary to open files in the Sources catalog for editing:

cd /etc/apt/sources.list.d

We look at what is

 ls ondrej*

Result:

ondrej-apache2-jammy.list ondrej-php-bionic.list ondrej-php-jammy.list

In turn, we open the files ondrej *. List to change in the nano editor:

1) Repository ONDREJ for Apache2 web server

sudo nano ondrej-apache2-jammy.list

Correct the first line, removing the text related to the signature.

Was:
deb [arch=amd64 signed-by=/etc/apt/keyrings/ondrej-apache2-jammy.gpg] https://ppa.launchpadcontent.net/ondrej/apache2/ubuntu jammy main

It became:
deb https://ppa.launchpadcontent.net/ondrej/apache2/ubuntu jammy main

2) Repository ONDREJ for PHP versions

sudo nano ondrej-php-jammy.list

Was:
deb [arch=amd64 signed-by=/etc/apt/keyrings/ondrej-php-jammy.gpg] https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy main

It became:
deb https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy main

Result

sudo apt update

Errors no (on the warning "Key is stored in legasted.gpg keyring (/etc/apt/trusted.gpg), See the Depredion Section in Apt-Key (8) for Details." We do not pay attention)

Now you can install PHP 7.4:

sudo apt install php7.4
sudo apt install php7.4-{cli,common,curl,zip,gd,mysql,xml,mbstring,json,intl}

How to switch the PHP version from 8.1 to 7.4 and vice versa in the system?

php --version

PHP 8.1.2-1ubuntu2.11 (CLI) (Built: Feb 22 2023 22:56:18) (NTS)

PHP switching in the system is made by command

sudo update-alternatives --config php

There are 3 options for the PHP alternative (provides/usr/bin/php).

Choice Path Prior condition -------------------------------------------------------- * 0 /usr/bin/php.default 100 automatic mode 1 /usr/bin/php.default 100 ручной режим 2 /usr/bin/php7.4 74 ручной режим 3 /usr/bin/php8.1 81 ручной режим Click "Enter" so as not to change the current choice [*], or enter the right number: When pressing 2, the PHP interpreter will switch to version 7.4. php --version PHP 7.4.33 (CLI) (Built: Jun 8 2023 15:23:32) (NTS) Another way to switch PHP commands: ** 8.1 -> 7.4 ** sudo update-alternatives --set php /usr/bin/php7.4 ** 7.4 -> 8.1 ** sudo update-alternatives --set php /usr/bin/php8.1 ### PHP switch to the Apache2 web server ! Supplement from 07.2023: ** PHP 7.4 module for Apache2 must be installed in advance: ** sudo apt-get install libapache2-mod-php7.4 ** 8.1 -> 7.4 ** sudo a2dismod php8.1 sudo a2enmod php7.4 ** 7.4 -> 8.1 ** sudo a2dismod php7.4 sudo a2enmod php8.1 ### Examination Create the simplest test.php file and open it from the target line of the browser from the web server: When opening the URL, the phpinfo () function should be obtained. In the line "Configuration File (PHP.ini) Path" in full way to the php.ini file, the current version of PHP used by the interpreter will be displayed, for example, 7.4. ### FastCGI shutdown The presence of a neglected FPM module version of the PHP 8.1 can be checked by a command from the console: sudo systemctl status php8.1-fpm In my case, FPM is launched and uses PHP 8.1 (ignoring the version of the PHP7.4 module in Apache2):
 PHP8.1-FPM.Service - The PHP 8.1 Fastcgi Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2023-07-04 09:01:29 MSK; 38min ago
       Docs: man:php-fpm8.1(8)
    Process: 1411 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 81>
   Main PID: 1207 (php-fpm8.1)
     Status: "Processes active: 0, idle: 2, Requests: 2, slow: 0, Traffic: 0req/sec" 
      Tasks: 3 (limit: 3408)
     Memory: 23.7M
        CPU: 254ms
     CGroup: /system.slice/php8.1-fpm.service
             ├─1207 "php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)"   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   "" >
             ├─1409 "php-fpm: pool www"   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   "">
             └─1410 "php-fpm: pool www"   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""   ""
Also, when using Fastcgi (PHP-FPM), the use of an erroneous version 8.1 is visible by command **`echo phpinfo()`**;. To make the version 7.4, I turned off the PHP-FPM module on the Apache2 test server, command: sudo a2dismod actions alias proxy_fcgi setenvif Then, confirmed the shutdown: "Yes, do as i say!" Completed the Apache2 server restart: sudo systemctl restart apache2 Now phpinfo () shows the version I need - 7.4 (instead of 8.1): `Configuration File (php.ini) Path /etc/php/7.4/apache2` `Loaded Configuration File (none)` `Scan this dir for additional .ini files /etc/php/7.4/apache2/conf.d` --- _ Last change: 02.06.2025: replacement GPG --Export --armor 4f4ea0aae5267a6c | Sudo Apt -Key Add - on GPG --Export 4F4EA0AAE5267A6C | SUDO TEE /TC/APT/TRusted.gpg.d/php_ondrej.gpg>/DEV/NULL _


Related publications