Ubuntu 安装 php8.1

虽然这段时间一直是在用 go 开发,但 PHP 对我的影响一直影响着我,语言只是一种工具,所用的场景一直都是由使用者来决定。虽然 PHP 没有前几年那么受欢迎,但它也在往前发展,变的越来越好。

在平时开发、测试时,往往需要本地启动一些服务器,令我感觉非常方便的一个工具是 PHP 内置的一个服务器功能。通过命令行工具可以看得到使用方法:

➜  $ php --help
Usage: php [options] [-f] <file> [--] [args...]
   php [options] -r <code> [--] [args...]
   php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
   php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
   php [options] -S <addr>:<port> [-t docroot] [router]
   php [options] -- [args...]
   php [options] -a

  -a               Run as interactive shell (requires readline extension)
  -c <path>|<file> Look for php.ini file in this directory
  -n               No configuration (ini) files will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -f <file>        Parse and execute <file>.
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -r <code>        Run PHP <code> without using script tags <?..?>
  -B <begin_code>  Run PHP <begin_code> before processing input lines
  -R <code>        Run PHP <code> for every input line
  -F <file>        Parse and execute <file> for every input line
  -E <end_code>    Run PHP <end_code> after processing all input lines
  -H               Hide any passed arguments from external tools.
  -S <addr>:<port> Run with built-in web server.
  -t <docroot>     Specify document root <docroot> for built-in web server.
  -s               Output HTML syntax highlighted source.
  -v               Version number
  -w               Output source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.

  args...          Arguments passed to script. Use -- args when first argument
                   starts with - or script is read from stdin

  --ini            Show configuration file names

 ...

根据说明,只需要简单的使用命令 php -S 0.0.0.0:8001 就能在本地启动一个 8001 端口的测试用的服务器,真的特别方便。

由于我本地的操作系统是 win10,但我同时也很喜欢 Linux 系统的开发环境,于是,也偶尔使用 win10 上的 wsl2 Ubuntu 20.04。接下来,我就简单记录一下如何在 Ubuntu 20.04 中安装 php8.1。

安装方式深度参考 如何在Ubuntu 20.04 上安装 php8 ,如果你感兴趣可以点击查看。

作者在编写《如何在Ubuntu 20.04 上安装 php8》时,Ubuntu 20.04 默认的软件库已经自带了 PHP7.4。要安装 PHP8 需要基于 ondrej/php PPA 中安装。

如果你给你生产环境安装 PHP8 请确保你的应用支持 PHP8。

这篇文章介绍的 PHP8 安装步骤也同样适用于所有基于 Ubuntu 的发行版 Linux 系统。如: Kubuntu、Linux Mint 和 Elementary OS。

启用软件仓库

Ondřej Surý 是一名 Debian 开发人员,他维护着一个包含多个 PHP 版本的仓库。启用 PPA 仓库可以通过运行以下命令:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php

由于我本地并不需要 php-fpm 等组件,只需要 php8 cli 模块,因此我省略其他部分,直讨论 PHP8 cli 部分的安装。

安装 PHP8

由于在编写本文时,PHP8.1 已经处于 RC 阶段,而且软件库中也有 PHP8.1 了,因此我直接安装 PHP8.1

sudo apt update
sudo apt install php8.1

等待安装完成。此时你运行 php -v 已经可以看到 PHP 的版本信息,表示你已经成功安装 PHP8.1。

如果需要试试启动内置服务器的功能,可以运行 php -S 0.0.0.0:8001,此时可以通过浏览器访问 http://127.0.0.1:8001,会得到一个 404 页面。

通过这个测试服务器,你可以访问 PHP 脚本页面,也可以访问一些静态资源,用于开发会特别简单、方便。

参考

posted @ 2022-03-27 14:32  suhanyujie  阅读(1432)  评论(0编辑  收藏  举报