如何在Ubuntu 20.04 | 18.04上安裝PHP 8.0

PHP是一種流行的腳本語言,最初是為Web開發而創建的。 拉斯穆斯·拉多夫(Rasmus Radolf),1994年,丹麥-加拿大程序員。如果您安裝了PHP,則您創建的PHP腳本可以在Linux,macOS,Windows和許多其他Unix系統上運行。

本文介紹了在Ubuntu 20.04 | Ubuntu 18.04 Linux系統上安裝PHP 8.0的過程。上一指南捕獲了在CentOS 8/7上PHP 8.0的安裝。 網站上還有許多其他指南,介紹如何在Ubuntu Linux機器上安裝和使用其他版本的PHP。

PHP8正式發布開始 2020年11月26日..這是一個新的主要版本,具有許多新功能和性能改進。您還應該期待新的重大更改,這意味着您可能需要對代碼進行一些更改才能在PHP 8中運行。 PHP8附帶的許多新功能包括JIT編譯器,命名參數,聯合類型和屬性。等等

步驟1:系統更新

運行以下命令以將所有系統軟件包更新為最新版本。

sudo apt update
sudo apt -y upgrade

升級後重新啟動很重要。

sudo systemctl reboot

步驟2:添加OndřejSurýPPA存儲庫

最新的PHP軟件包是 OndřejSurýPPA 倉庫。通過運行以下共享命令將此存儲庫添加到您的Ubuntu系統。

sudo apt update
sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common -y
sudo add-apt-repository ppa:ondrej/php

當提示您添加存儲庫時,請按Enter。

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
Press [ENTER] to continue or Ctrl-c to cancel adding it.

從更新輸出中,您可以看到添加的存儲庫。

Hit:1 http://mirror.hetzner.de/ubuntu/packages focal InRelease
Hit:2 http://mirror.hetzner.de/ubuntu/packages focal-updates InRelease
Hit:3 http://mirror.hetzner.de/ubuntu/packages focal-backports InRelease
Hit:4 http://mirror.hetzner.de/ubuntu/packages focal-security InRelease
Get:5 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease [23.9 kB]
Hit:6 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:7 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:8 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:9 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Get:10 http://ppa.launchpad.net/ondrej/php/ubuntu focal/main amd64 Packages [74.8 kB]
Get:11 http://ppa.launchpad.net/ondrej/php/ubuntu focal/main i386 Packages [20.2 kB]
Get:12 http://ppa.launchpad.net/ondrej/php/ubuntu focal/main Translation-en [28.2 kB]
Fetched 147 kB in 1s (158 kB/s)
Reading package lists... Done

步驟3:在Ubuntu 20.04 | 18.04上安裝PHP8

更新APT索引以確保軟件包安裝正常。

sudo apt update

然後在您的Ubuntu 20.04 | 18.04 Linux機器上安裝PHP 8.0。

 sudo apt install php8.0

擊中 ÿ 開始安裝的關鍵:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  apache2-bin libapache2-mod-php8.0 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libgdbm-compat4 libjansson4 liblua5.2-0 libmagic-mgc libmagic1
  libperl5.30 perl perl-modules-5.30 php-common php8.0-cli php8.0-common php8.0-opcache php8.0-readline
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser php-pear file perl-doc libterm-readline-gnu-perl | libterm-readline-perl-perl make
  libb-debug-perl liblocale-codes-perl
Recommended packages:
  apache2
The following NEW packages will be installed:
  apache2-bin libapache2-mod-php8.0 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libgdbm-compat4 libjansson4 liblua5.2-0 libmagic-mgc libmagic1
  libperl5.30 perl perl-modules-5.30 php-common php8.0 php8.0-cli php8.0-common php8.0-opcache php8.0-readline
0 upgraded, 20 newly installed, 0 to remove and 7 not upgraded.
Need to get 13.0 MB of archives.
After this operation, 78.4 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

檢查服務器上的默認PHP版本。

$ php -v
PHP 8.0.0RC3 (cli) (built: Oct 31 2020 17:06:41) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.0RC3, Copyright (c), by Zend Technologies

步驟4:在Ubuntu 20.04 | 18.04上安裝PHP8擴展

要在Ubuntu 20.04 | 18.04上安裝PHP8擴展,請使用以下命令語法:

sudo apt install php8.0-<extension>

以下是一些可用的擴展名:

$ sudo apt install php8.0-
php8.0-amqp       php8.0-common     php8.0-gd         php8.0-ldap       php8.0-odbc       php8.0-readline   php8.0-sqlite3    php8.0-xsl
php8.0-apcu       php8.0-curl       php8.0-gmp        php8.0-mailparse  php8.0-opcache    php8.0-redis      php8.0-sybase     php8.0-yac
php8.0-ast        php8.0-dba        php8.0-igbinary   php8.0-mbstring   php8.0-pgsql      php8.0-rrd        php8.0-tidy       php8.0-yaml
php8.0-bcmath     php8.0-dev        php8.0-imagick    php8.0-memcached  php8.0-phpdbg     php8.0-smbclient  php8.0-uuid       php8.0-zip
php8.0-bz2        php8.0-ds         php8.0-imap       php8.0-msgpack    php8.0-pspell     php8.0-snmp       php8.0-xdebug     php8.0-zmq
php8.0-cgi        php8.0-enchant    php8.0-interbase  php8.0-mysql      php8.0-psr        php8.0-soap       php8.0-xhprof
php8.0-cli        php8.0-fpm        php8.0-intl       php8.0-oauth      php8.0-raphf      php8.0-solr       php8.0-xml

例:

sudo apt install php8.0-cli php8.0-common php8.0-imap php8.0-redis php8.0-snmp php8.0-xml

使用相同的格式來安裝其他擴展。

參考:

Sidebar