如何在 Ubuntu 18.04 / 19.04 / 16.04 上安装 PHP 7.4

程序员

如何在 Ubuntu 上安装 PHP 7.4?今天的指南就是了解如何在 Ubuntu 18.04 / 19.04 / 16.04 Linux 上安装 PHP 7.4。PHP 7.4.0 已向公众开放并在生产环境中使用。

PHP 是一种流行的服务器脚本语言,以创建动态和交互式 Web 页面而闻名。PHP 是 Web 中广泛使用的编程语言。 根据以下步骤开始在 Ubuntu 19.04 / 18.04 / 16.04 上安装 PHP 7.4。

1. 添加 PHP PPA 仓库

我们将添加 ppa:ondrej / php PPA 仓库,其中包含最新的 PHP 构建包。

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

2. 开始安装

使用下面的命令开始安装 PHP 7.4:

sudo apt -y install php7.4

等待安装完成,使用下面命令查看是否安装成功:

$ php -v
PHP 7.4.0beta4 (cli) (built: Aug 28 2019 11:41:49) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0-dev, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.0beta4, Copyright (c), by Zend Technologies

使用下面的命令安装 PHP 其他拓展、软件包:

sudo apt-get install php7.4-xxx

例如:

sudo apt-get install -y php7.4-{bcmath,bz2,intl,gd,mbstring,mysql,zip}

与 Apache 相关的 PHP 配置存储在 /etc/php/7.4/apache2/php.ini 文件中。

在 Ubuntu 中使用 PHP 7.4 和 Nginx:

在 Ubuntu 上安装 PHP 可以配置 Apache 作为服务器使用。对于有兴趣运行 Nginx + PHP 的用户,您需要停止并禁用 Apache 服务。

sudo systemctl disable --now apache2

然后安装 fpm 拓展和 Nginx 软件包。

sudo apt-get install nginx php7.4-fpm

安装完成后用下面命令查看 Nginx 和 PHP 运行状态:

$ systemctl status php7.4-fpm nginx
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-09-16 17:09:41 UTC; 27s ago
     Docs: man:php-fpm7.4(8)
 Main PID: 25321 (php-fpm7.4)
   Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
    Tasks: 3 (limit: 2362)
   CGroup: /system.slice/php7.4-fpm.service
           ├─25321 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
           ├─25339 php-fpm: pool www
           └─25340 php-fpm: pool www
Sep 16 17:09:41 ubuntu18 systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
Sep 16 17:09:41 ubuntu18 systemd[1]: Started The PHP 7.4 FastCGI Process Manager.
● nginx.service - A high performance web server and a reverse proxy server
    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
    Active: active (running) since Mon 2019-09-16 17:09:42 UTC; 6min ago
      Docs: man:nginx(8)
  Main PID: 25441 (nginx)
     Tasks: 2 (limit: 2362)
    CGroup: /system.slice/nginx.service
            ├─25441 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
            └─25442 nginx: worker process
 Sep 16 17:09:42 ubuntu18 systemd[1]: Starting A high performance web server and a reverse proxy server…
 Sep 16 17:09:42 ubuntu18 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
 Sep 16 17:09:42 ubuntu18 systemd[1]: Started A high performance web server and a reverse proxy server.

PHP FPM 配置文件为 /etc/php/7.4/fpm/pool.d/www.conf

现在你可以在 Ubuntu 19.04 / Ubuntu 18.04 / Ubuntu 16.04 Linux 系统上享受使用 PHP 7.4 的乐趣。

原文:https://computingforgeeks.com/how-to-install-php-on-ubuntu