Debian/Ubuntu 環境安裝 PHP-FPM 8.3、8.2 或 8.1 版本

此篇文章主要記錄在Debian/Ubuntu系統上安裝目前還有在支援PHP-FPM(FastCGI Process Manager)的版本,以方便筆者自己日後在Debian/Ubuntu環境上用到需要PHP-FPM的版本時可以做一個參考。

此篇文章筆者會持續更新,且只會更新目前還有在支援PHP-FPM的版本。另外,系統部分此篇文章只會記錄Debian與Ubuntu兩個不同的Linux發行版,系統也是一樣僅會更新還有在支援的,主要注意的是Ubuntu系統方面,筆者只會記錄LTS版本。

**關於PHP目前還有在支援的版本,可以查看PHP官方網站的Supported Versions文章。

查詢Debian和Ubuntu預設所提供的PHP是否為你所要的版本:

**下面使用Debian 11系統版本為例:

#使用『apt-cache show PACKAGE_NAME』指令,可以查詢套件詳細資訊
apt-cache show php
#在如下面所示的資訊,以筆者查到的PHP版本資訊為例,可以看到Debian 10預設所提供的PHP版本為7.3
#Package: php
#Source: php-defaults (76)
#Version: 2:7.4+76
#Installed-Size: 13
#Maintainer: Debian PHP Maintainers <team+pkg-php@tracker.debian.org>
#Architecture: all
#Depends: php7.4
#Description-en: server-side, HTML-embedded scripting language (default)
# PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
# open source general-purpose scripting language that is especially suited
# for web development and can be embedded into HTML.
# .
# This package is a dependency package, which depends on latest stable
# PHP version (currently 7.4).
#Description-md5: 99c58fa41ae5c5908fd17241a53bfdd9
#Section: php
#Priority: optional
#Filename: pool/main/p/php-defaults/php_7.4+76_all.deb
#Size: 6340
#MD5sum: 33859926f35bdfdc784d9e7312d16a92
#SHA256: 10ca22712771dc343fb6600cbdbc88069069467fcc5c85782ee89abbc8c81f59
#假如你查到的PHP並不是你要的PHP版本,那接下來部分,你可以參考下面的方式,從第三方來獲取你要的PHP版本

 

Debian 12(Bookworm)、Debian 11(Bullseye)和Debian 10(Buster)安裝PHP-FPM 8.3版本、PHP-FPM 8.2版本或PHP-FPM 8.1版本:

Debian系統若使用『apt-cache show php』指令檢查套件庫預設PHP版本非你要的版本,那可經由第三方的DPA儲存庫取得你要的PHP版本:

#安裝apt-transport-https(確保APT可以在HTTPS中執行)、lsb-release(識別系統的Linux發行版的版本)及ca-certificates(CA憑證工具)
sudo apt-get install apt-transport-https lsb-release ca-certificates -y
#加入packages.sury.org的GPG key
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
#將packages.sury.org寫入至Debian的sources list套件來源清單
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
#更新sources list清單
sudo apt-get update

 

Debian 12、Debian 11和Debian 10安裝php8.3-fpm

#安裝php8.3-fpm
sudo apt-get install php8.3-fpm -y
#以及你還所需要的其他的php8.3套件,例如:
sudo apt-get install php8.3-common php8.3-gd php8.3-cli php8.3-mbstring php8.3-xml php8.3-opcache php8.3-mysql -y
#查看是否安裝好你所需要的PHP版本
php -v

 

Debian 12、Debian 11和Debian 10安裝php8.2-fpm

#安裝php8.2-fpm
sudo apt-get install php8.2-fpm -y
#以及你還所需要的其他的php8.1套件,例如:
sudo apt-get install php8.2-common php8.2-gd php8.2-cli php8.2-mbstring php8.2-xml php8.2-opcache php8.2-mysql -y
#查看是否安裝好你所需要的PHP版本
php -v

 

Debian 12、Debian 11和Debian 10安裝php8.1-fpm

#安裝php8.1-fpm
sudo apt-get install php8.1-fpm -y
#以及你還所需要的其他的php8.1套件,例如:
sudo apt-get install php8.1-common php8.1-gd php8.1-cli php8.1-mbstring php8.1-xml php8.1-opcache php8.1-mysql -y
#查看是否安裝好你所需要的PHP版本
php -v

 

於Debian系統中PHP套件的其他筆記:

筆記 1:若日後在更新或安裝PHP套件時,遇到金鑰過期問題:

Step 1:以下面的例子來說,可看到PHP套件的『B188E2B695BD4743』金鑰已過期。

Err:X https://packages.sury.org/php <系統代號> InRelease
  The following signatures were invalid: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key <deb@sury.org>
Fetched 4635 B in 0s (10.2 kB/s)
Reading package lists... Done
W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: https://packages.sury.org/php <系統代號> InRelease: The following signatures were invalid: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key <deb@sury.org>
W: Failed to fetch https://packages.sury.org/php/dists/<系統代號>/InRelease  The following signatures were invalid: EXPKEYSIG B188E2B695BD4743 DEB.SURY.ORG Automatic Signing Key <deb@sury.org>
W: Some index files failed to download. They have been ignored, or old ones used instead.

 

Step 2:刪除系統提示的過期金鑰。

sudo apt-key del B188E2B695BD4743

 

Step 3:重新加入packages.sury.org的GPG key。

sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

 

Step 4:更新sources list清單。

sudo apt-get update

 

Ubuntu 22.04 LTS、20.04 LTS和18.04 LTS(其他的Ubuntu版本也可以參考)安裝PHP-FPM 8.3版本、PHP-FPM 8.2版本或PHP-FPM 8.1版本:

Ubuntu系統若使用『apt-cache show php』指令檢查套件庫預設PHP版本非你要的版本,那可經由第三方的PPA儲存庫取得你要的PHP版本:

#由於要使用到第三方的PPA儲存庫,因此Ubuntu系統上需安裝好python-software-properties套件
sudo apt-get install python-software-properties
#若出現『Package python-software-properties is not available, but is referred to by another package.』訊息,那您應該安裝『software-properties-common』來替代『python-software-properties』。
sudo apt-get install software-properties-common
#因為需要在Ubuntu系統上安裝最新版本的PHP,所以就需要使用到第三方的PPA儲存庫,筆者這邊於Ondřej Surý所維護的為例:
sudo add-apt-repository ppa:ondrej/php
#中途會遇到如下面所示的再次確認訊息:
## LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
# More info: https://launchpad.net/~ondrej/+archive/ubuntu/php
#Press [ENTER] to continue or Ctrl-c to cancel adding it.
#那就按鍵盤的『Enter』以繼續。
#更新套件清單
sudo apt-get update

 

Ubuntu 22.04 LTS、20.04 LTS和18.04 LTS安裝php8.3-fpm:

#安裝php8.3-fpm
sudo apt-get install php8.3-fpm -y
#以及你還所需要的其他的php8.3套件,例如:
sudo apt-get install php8.3-common php8.3-gd php8.3-cli php8.3-mbstring php8.3-xml php8.3-opcache php8.3-mysql -y
#查看是否安裝好你所需要的PHP版本
php -v

 

Ubuntu 22.04 LTS、20.04 LTS和18.04 LTS安裝php8.2-fpm:

#安裝php8.2-fpm
sudo apt-get install php8.2-fpm -y
#以及你還所需要的其他的php8.1套件,例如:
sudo apt-get install php8.2-common php8.2-gd php8.2-cli php8.2-mbstring php8.2-xml php8.2-opcache php8.2-mysql -y
#查看是否安裝好你所需要的PHP版本
php -v

 

Ubuntu 22.04 LTS、20.04 LTS和18.04 LTS安裝php8.1-fpm:

#安裝php8.1-fpm
sudo apt-get install php8.1-fpm -y
#以及你還所需要的其他的php8.1套件,例如:
sudo apt-get install php8.1-common php8.1-gd php8.1-cli php8.1-mbstring php8.1-xml php8.1-opcache php8.1-mysql -y
#查看是否安裝好你所需要的PHP版本
php -v

 

此篇文章上次修改日期:
2023/12/09