安裝 PHP 8.1 版 1 增加新版 PHP 套件庫來源 要安裝新版的 PHP 8 需要加入 Ondřej Surý (PHP 和 Debian 的首席開發人員,並維護 Ubuntu 和 Debian 套件) PHP PPA 的來源,指令如下: $ sudo apt install software-properties-common [sudo] password for ossii: 接下來新增 PHP 8 的 PPA 來源,指令如下: $ sudo add-apt-repository ppa:ondrej/php 中間會出現這行訊息,請按下「Enter」鍵繼續跑 Press [ENTER] to continue or Ctrl-c to cancel adding it. 出現以下訊息就代表匯入完成了: Hit:1 http://tw.archive.ubuntu.com/ubuntu focal InRelease Get:2 http://tw.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Get:3 http://tw.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB] Get:4 http://tw.archive.ubuntu.com/ubuntu focal-security InRelease [114 kB] Get:5 http://tw.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [924 kB] Get:6 http://tw.archive.ubuntu.com/ubuntu focal-updates/universe Translation-en [208 kB] Get:7 http://tw.archive.ubuntu.com/ubuntu focal-updates/universe amd64 c-n-f Metadata [21.0 kB] Get:8 http://tw.archive.ubuntu.com/ubuntu focal-security/universe amd64 Packages [711 kB] Get:9 http://tw.archive.ubuntu.com/ubuntu focal-security/universe Translation-en [128 kB] Get:10 http://tw.archive.ubuntu.com/ubuntu focal-security/universe amd64 c-n-f Metadata [14.7 kB] Get:11 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease [23.9 kB] Get:12 http://ppa.launchpad.net/ondrej/php/ubuntu focal/main amd64 Packages [102 kB] Get:13 http://ppa.launchpad.net/ondrej/php/ubuntu focal/main Translation-en [33.6 kB] Fetched 2,503 kB in 4s (637 kB/s) Reading package lists... Done 2 更新 apt 來源 執行 apt update 指令,也許會有需要更新的套件。 $ sudo apt update $ sudo apt upgrade -y 3  安裝 PHP 8.1 相關套件 PHP 的套件與模組很多,以下是一般常會用到的套件,安裝指令如下: $ sudo apt install php8.1 libapache2-mod-php8.1 php8.1-gd php8.1-mysql php8.1-curl php8.1-mbstring php8.1-intl -y $ sudo apt install php8.1-gmp php8.1-bcmath php8.1-imagick php8.1-xml php8.1-zip -y 4 啟用 Apache 的 PHP 模組 如同前一章所述,Ubuntu 下的 Apache 模組需要先以 a2enmod 的指令啟用,指令如下: $ sudo a2enmod php8.1 $ sudo systemctl restart apache2 5 測試 PHP 版本資訊 啟動完成後,在 /var/www/html/ 的目錄下新增一個名叫 info.php 的檔案,指令和內容如下: $ sudo vim /var/www/html/info.php 把以下的內容寫入 info.php 中,內容如下: 存檔後離開,接下來請用瀏覽器存取這個 php 檔,位置是 https://yourhostip/info.php,正常會得到以下內容畫面: 你可以透過這個頁面瞭解目前 Apache 伺服器上與 PHP 相關的資訊,測試完後,可以把 info.php 這個檔案刪掉,如此就完成 PHP 的安裝作業了。