Ubuntu22.04でデフォルトのPHPのバージョンを7.4にする

  • Ubuntu Server 22.04でデフォルトのPHPを8.1から7.4にする

メモです。後ほど見やすく整形する予定です。

root@deto105:~# php
Command 'php' not found, but can be installed with:
apt install php-cli     # version 2:8.1+92ubuntu1, or
apt install php8.1-cli  # version 8.1.2-1ubuntu2.14
root@deto105:~# apt install -y php
--- 中略 ---
Creating config file /etc/php/8.1/apache2/php.ini with new version
Module mpm_event disabled.
Enabling module mpm_prefork.
apache2_switch_mpm Switch to prefork
apache2_invoke: Enable module php8.1
Setting up php8.1 (8.1.2-1ubuntu2.14) ...
Setting up php (2:8.1+92ubuntu1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for php8.1-cli (8.1.2-1ubuntu2.14) ...
Processing triggers for libapache2-mod-php8.1 (8.1.2-1ubuntu2.14) ...
Scanning processes...                                                                                                           
Scanning processor microcode...                                                                                                 
Scanning linux images...                                                                                                        
 
Running kernel seems to be up-to-date.
 
The processor microcode seems to be up-to-date.
 
No services need to be restarted.
 
No containers need to be restarted.
 
No user sessions are running outdated binaries.
 
No VM guests are running outdated hypervisor (qemu) binaries on this host.
 
 
 
root@deto105:~# php  -v
PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies
 
 
 
 
■php7.4の存在確認
 
root@deto105:~# apt search php7.4
Sorting... Done
Full Text Search... Done
libapache2-mod-php7.4/jammy-updates,jammy-security 8.1.2-1ubuntu2.14 amd64
  Transitional package
 
 
リポジトリを追加
Ubuntu22.04の標準リポジトリにはPHP7.4がない為、PHP7.4を持っているリポジトリを追加
 
root@deto105:~# add-apt-repository ppa:ondrej/php
PPA publishes dbgsym, you may need to include 'main/debug' component
Repository: 'deb https://ppa.launchpadcontent.net/ondrej/php/ubuntu/ jammy main'
Description:
Co-installable PHP versions: PHP 5.6, PHP 7.x, PHP 8.x and most requested extensions are included. Only Supported Versions of PHP (http://php.net/supported-versions.php) for Supported Ubuntu Releases (https://wiki.ubuntu.com/Releases) are provided. Don't ask for end-of-life PHP versions or Ubuntu release, they won't be provided.
 
Debian oldstable and stable packages are provided as well: https://deb.sury.org/#debian-dpa
 
You can get more information about the packages at https://deb.sury.org
 
IMPORTANT: The <foo>-backports is now required on older Ubuntu releases.
 
BUGS&FEATURES: This PPA now has a issue tracker:
https://deb.sury.org/#bug-reporting
 
CAVEATS:
1. If you are using php-gearman, you need to add ppa:ondrej/pkg-gearman
2. If you are using apache2, you are advised to add ppa:ondrej/apache2
3. If you are using nginx, you are advised to add ppa:ondrej/nginx-mainline
   or ppa:ondrej/nginx
 
PLEASE READ: If you like my work and want to give me a little motivation, please consider donating regularly: https://donate.sury.org/
 
WARNING: add-apt-repository is broken with non-UTF-8 locales, see
https://github.com/oerdnj/deb.sury.org/issues/56 for workaround:
 
# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
More info: https://launchpad.net/~ondrej/+archive/ubuntu/php
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel.
--- Enter を押す ---
Adding deb entry to /etc/apt/sources.list.d/ondrej-ubuntu-php-jammy.list
Adding disabled deb-src entry to /etc/apt/sources.list.d/ondrej-ubuntu-php-jammy.list
Adding key to /etc/apt/trusted.gpg.d/ondrej-ubuntu-php.gpg with fingerprint 14AA40EC0831756756D7F66C4F4EA0AAE5267A6C
Hit:1 http://jp.archive.ubuntu.com/ubuntu jammy InRelease                        
Get:2 http://jp.archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]       
Get:3 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy InRelease [23.9 kB]
Get:4 http://jp.archive.ubuntu.com/ubuntu jammy-backports InRelease [109 kB]     
Get:5 http://jp.archive.ubuntu.com/ubuntu jammy-security InRelease [110 kB]                
Get:6 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 Packages [115 kB]
Get:7 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main Translation-en [35.6 kB]
Fetched 512 kB in 6s (91.4 kB/s)          
Reading package lists... Done
 
 
■php7.4の存在を再び確認
----- 中略 -----
root@deto105:~# apt search php7.4
 
php7.4-xmlrpc/jammy 1:7.4.33-8+ubuntu22.04.1+deb.sury.org+1 amd64
  XMLRPC-EPI module for PHP
 
php7.4-xsl/jammy 1:7.4.33-8+ubuntu22.04.1+deb.sury.org+1 all
  XSL module for PHP (dummy)
 
php7.4-yac/jammy 2.3.1++-1+ubuntu22.04.1+deb.sury.org+1 amd64
  YAC (Yet Another Cache) for PHP
 
php7.4-yaml/jammy 1:2.2.3-1+ubuntu22.04.1+deb.sury.org+2 amd64
  YAML-1.1 parser and emitter for PHP
 
php7.4-zip/jammy 1:7.4.33-8+ubuntu22.04.1+deb.sury.org+1 amd64
  Zip module for PHP
 
php7.4-zmq/jammy 1.1.3-24+ubuntu22.04.1+deb.sury.org+1 amd64
  ZeroMQ messaging bindings for PHP
 
php7.4-zstd/jammy 0.12.0-1+ubuntu22.04.1+deb.sury.org+1 amd64
  Zstandard extension for PHP
 
root@deto105:~# 
 
 
■php7.4をインストール
root@deto105:~# apt install -y php7.4
----- 中略 -----
Creating config file /etc/php/7.4/cli/php.ini with new version
Setting up libapache2-mod-php7.4 (1:7.4.33-8+ubuntu22.04.1+deb.sury.org+1) ...
 
Creating config file /etc/php/7.4/apache2/php.ini with new version
libapache2-mod-php7.4: php8.1 module already enabled, not enabling PHP 7.4
Setting up php7.4 (1:7.4.33-8+ubuntu22.04.1+deb.sury.org+1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for php7.4-cli (1:7.4.33-8+ubuntu22.04.1+deb.sury.org+1) ...
Processing triggers for libapache2-mod-php7.4 (1:7.4.33-8+ubuntu22.04.1+deb.sury.org+1) ...
Scanning processes...                                                                                                           
Scanning processor microcode...                                                                                                 
Scanning linux images...                                                                                                        
 
Running kernel seems to be up-to-date.
 
The processor microcode seems to be up-to-date.
 
No services need to be restarted.
 
No containers need to be restarted.
 
No user sessions are running outdated binaries.
 
No VM guests are running outdated hypervisor (qemu) binaries on this host.
 
 
 
root@deto105:~# php -v
PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies
 
 
 
バージョン変更
8.1と7.4が存在しているがデフォルトでは、8.1が実行される状態。
この後に切り替えを行う。
 
まず、Apache2のモジュールを切り替えます。
root@deto105:~# a2dismod php8.1 
Module php8.1 disabled.
To activate the new configuration, you need to run:
  systemctl restart apache2
root@deto105:~# a2enmod php7.4
Considering dependency mpm_prefork for php7.4:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php7.4:
Enabling module php7.4.
To activate the new configuration, you need to run:
  systemctl restart apache2
root@deto105:~# 
 
 
root@deto105:/etc/apache2/mods-enabled# a2enmod userdir 
Enabling module userdir.
To activate the new configuration, you need to run:
  systemctl restart apache2
 
 
# vi php7.4.conf
---------------------
<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
        php_admin_flag engine Off
    </Directory>
</IfModule>
 
    ↓↓↓↓↓
 
<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
#        php_admin_flag engine Off
    </Directory>
</IfModule>
 
 
 
root@deto105:/etc/apache2/mods-enabled# systemctl restart apache2
 
kunisaki@deto105:/home$ ls -latr
total 12
drwxr-xr-x 19 root     root     4096 Sep 17 07:12 ..
drwxr-xr-x  3 root     root     4096 Sep 17 07:13 .
drwxr-x---  6 kunisaki kunisaki 4096 Sep 19 08:47 kunisaki
kunisaki@deto105:/home$ chmod 755 kunisaki
 
kunisakiユーザーで /home/public_html/t.phpを作成
<?php
phpinfo();
?>
 
 
ブラウザで
http://192.168.1.105/~kunisaki/t.php
にアクセスし、PHPのバージョンが7.4になっていることを確認。実際は、7.4.33 など
 
シェルでphpとだけしたときのバージョンは上記の設定とは関係がない。
unisaki@deto105:/home$ php -v
PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies
 
 
 
切り替えは以下のようにして選択する
kunisaki@deto105:/home$ sudo update-alternatives --config php
[sudo] password for kunisaki: 
There are 2 choices for the alternative php (providing /usr/bin/php).
 
  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.1   81        auto mode
  1            /usr/bin/php7.4   74        manual mode
  2            /usr/bin/php8.1   81        manual mode
 
Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in manual mode
kunisaki@deto105:/home$ php -v
PHP 7.4.33 (cli) (built: Sep  2 2023 08:03:46) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies
kunisaki@deto105:/home$