PHP 8.2 是 PHP 编程语言的一个版本,于 2022 年底发布。这是 PHP 的一个主要版本,包含许多新功能,包括只读类、作为独立类型的 null、false 和 true、已弃用的动态属性、性能改进等等。 PHP 8.2 还包括与 8.1 等之前版本相比的错误修复和性能改进。我们建议您在生产设置中升级之前使用 PHP 8.2 测试您的代码库,以确保一切按预期工作。
在本文中,我们将介绍在 Ubuntu 22.04|20.04|18.04 上安装 PHP 8.2 所使用的步骤。操作系统存储库上可用的默认 PHP 版本通常早于 PHP 官方最新版本。 PHP 的 PPA(个人软件包存档)软件存储库允许您在 Ubuntu 系统上安装 Linux 发行版的官方存储库中未提供的较新版本的 PHP。
Debian 安装文章:如何在 Debian 上安装 PHP 8.2
更新Ubuntu系统
在执行任何其他操作之前检查更新并安装它们。
sudo apt update && sudo apt -y upgrade升级后清理 APT 缓存。
sudo apt autoremove
检查是否需要重新启动并采取相应措施。
[ -f /var/run/reboot-required ] && sudo reboot -f可以使用此处给出的命令检查操作系统版本详细信息。
$ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.1 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy添加 Surý PHP APT 存储库
为了在 Ubuntu 上安装 PHP 8.2 软件包,我们将使用 Ondřej Surý 的 PHP PPA,它为 Ubuntu 和 Debian 系统提供了最新的稳定版本的 PHP。
对于 Ubuntu 18.04,此存储库不包含 PHP 8.2 软件包。查看我们的从源代码构建指南:
- 在 Ubuntu 18.04 (Bionic Beaver) 上构建并安装 PHP 8,7
在添加存储库之前安装一些依赖包。
sudo apt update
sudo apt install -y lsb-release gnupg2 ca-certificates apt-transport-https software-properties-common在终端中运行以下命令将 Surý PPA 添加到您的系统。
sudo 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 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.
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 https://mirror.hetzner.com/ubuntu/packages jammy InRelease
Hit:2 https://mirror.hetzner.com/ubuntu/packages jammy-updates InRelease
Hit:3 https://mirror.hetzner.com/ubuntu/packages jammy-backports InRelease
Hit:4 https://mirror.hetzner.com/ubuntu/security jammy-security InRelease
Get:5 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy InRelease [23.9 kB]
Get:6 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 Packages [108 kB]
Get:7 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main Translation-en [33.3 kB]
Fetched 166 kB in 1s (120 kB/s)
Reading package lists... Done您可以通过运行 apt update 命令手动确认存储库是否正常工作。
$ sudo apt update
Hit:1 https://mirror.hetzner.com/ubuntu/packages jammy InRelease
Hit:2 https://mirror.hetzner.com/ubuntu/packages jammy-updates InRelease
Hit:3 https://mirror.hetzner.com/ubuntu/packages jammy-backports InRelease
Hit:4 https://mirror.hetzner.com/ubuntu/security jammy-security InRelease
Hit:5 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done在 Ubuntu 上安装 PHP 8.2
添加 PPA 后,使用 apt 命令在 Ubuntu 服务器上安装 PHP 8.2 和任何其他关联的 PHP 模块。
sudo apt install php8.2根据提示的要求接受软件包安装。
The following additional packages will be installed:
apache2 apache2-bin apache2-data apache2-utils bzip2 file libapache2-mod-php8.2 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0 mailcap mime-support php-common
php8.2-cli php8.2-common php8.2-opcache php8.2-readline ssl-cert
Suggested packages:
apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser bzip2-doc php-pear
The following NEW packages will be installed:
apache2 apache2-bin apache2-data apache2-utils bzip2 file libapache2-mod-php8.2 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.3-0 mailcap mime-support php-common php8.2
php8.2-cli php8.2-common php8.2-opcache php8.2-readline ssl-cert
0 upgraded, 21 newly installed, 0 to remove and 2 not upgraded.
Need to get 6,990 kB of archives.
After this operation, 29.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
要检查系统上当前安装的 PHP 版本,请运行以下命令:
$ php -v
PHP 8.2.13 (cli) (built: Nov 21 2023 09:55:59) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.2.13, Copyright (c) Zend Technologies
with Zend OPcache v8.2.13, Copyright (c), by Zend Technologies您还可以使用以下命令获取当前活动且系统正在使用的 PHP 版本的详细信息:
php --version安装 PHP 8.2 模块
PHP 模块是为向 PHP 添加功能而创建的软件库。这些模块通常用 C 编写,您可以动态加载到 PHP 中或编译为共享对象。
如果您想在 Ubuntu 系统上安装 PHP 8.2 模块,请使用 apt 包管理器以及此处给出的命令语法:
sudo apt install -y php8.2-<module-name>在哪里 :
替换为要安装的模块的实际名称。
在此示例中,我们从之前添加的 PPA 安装 PHP 8.2 fpm 模块。
$ sudo apt install php8.2-fpm
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
php-pear
The following NEW packages will be installed:
php8.2-fpm
0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
Need to get 1,886 kB of archives.
After this operation, 5,756 kB of additional disk space will be used.
Get:1 https://ppa.launchpadcontent.net/ondrej/php/ubuntu jammy/main amd64 php8.2-fpm amd64 8.2.1-2+ubuntu22.04.1+deb.sury.org+1 [1,886 kB]
Fetched 1,886 kB in 0s (5,211 kB/s)
Selecting previously unselected package php8.2-fpm.
(Reading database ... 56412 files and directories currently installed.)
Preparing to unpack .../php8.2-fpm_8.2.1-2+ubuntu22.04.1+deb.sury.org+1_amd64.deb ...
Unpacking php8.2-fpm (8.2.1-2+ubuntu22.04.1+deb.sury.org+1) ...
Setting up php8.2-fpm (8.2.1-2+ubuntu22.04.1+deb.sury.org+1) ...
....常用模块的安装。
sudo apt install php8.2-{bcmath,fpm,xml,mysql,zip,intl,ldap,gd,cli,bz2,curl,mbstring,pgsql,opcache,soap,cgi}可以使用以下命令列出已安装的模块:
$ php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
FFI
fileinfo
filter
ftp
gd
gettext
hash
iconv
intl
json
ldap
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_pgsql
pgsql
Phar
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zip
zlib
[Zend Modules]
Zend OPcache将 PHP 与 Nginx 或 Apache 结合使用
我们将考虑使用 Nginx 和 Apache Web 服务器配置来托管您的 PHP 应用程序。
使用 Nginx Web 服务器托管 PHP 应用程序
对于 Nginx,PHP 代码通常由单独的进程执行,例如PHP-FPM(FastCGI Process Manager)。 PHP-FPM 是一个守护进程,用于侦听传入的 PHP 请求并在单独的进程中运行它们。 Nginx 只是将传入请求转发到 PHP-FPM,并在其中执行它们。
安装 Nginx Web 服务器和 FPM 扩展。
sudo apt install nginx php8.2-fpm打开 Nginx 服务器配置并在 http 块中添加以下块,以配置要转发的 PHP 请求以由 PHP-FPM 执行:
$ sudo vim /etc/nginx/nginx.conf
server {
listen 80;
server_name mysite.example.com;
root /var/www/mysite;
index index.php index.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
}
}验证 Nginx 配置是否正确。
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful使用 Apache Web 服务器托管 PHP 应用程序
在 Apache 中,我们可以使用名为 mod_php 的内置模块直接在 Apache 进程中处理 PHP 代码。
但首先您需要安装 Apache Web 服务器包、PHP 和 Apache PHP 扩展:
sudo apt install apache2 libapache2-mod-php8.2 启用mod_php模块:
sudo a2enmod php8.2配置完成后重新启动 Apache Web 服务器。
sudo systemctl restart apache2包起来
总而言之,在 Ubuntu Linux 系统上安装 PHP 8.2 的过程非常简单。我们首先将 Ondřej Surý 的 PHP PPA 添加到系统的包源中,这使我们能够安装 PHP 8.2 和其他模块。将第三方 PPA 添加到生产服务器可能会带来稳定性和安全风险,应始终谨慎使用。如果执行 PHP 升级,请考虑在更新系统之前备份重要文件,以防更新过程中出现问题。
推荐阅读的 Web 开发书籍:
- 学习 Web 开发的最佳书籍 - PHP、HTML、CSS、JavaScript 和 jQuery
- 掌握网页设计的最佳书籍
- 学习 CSS 和 CSS3 的最佳书籍
- 学习 HTML 和 HTML5 的最佳书籍
- 最佳 Apache 和 Nginx 参考书