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

时间:2020-02-23 14:38:45  来源:igfitidea点击:

如何在Ubuntu上安装PHP 7.4?
如何在Ubuntu 20.04/18.04/19.04/16.04 Linux上安装PHP 7.4。

PHP是一种用于创建动态和交互式网页的流行服务器脚本语言。
PHP是Web中广泛使用的编程语言。
使用以下步骤在Ubuntu 20.04/19.04/18.04/16.04上安装PHP 7.4.

在Ubuntu 20.04上安装PHP 7.4

注意:Ubuntu 20.04在其上游存储库中使用PHP 7.4船舶。
只需将其安装并使用APT Package Manager即可。

sudo apt update
sudo apt install php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath

确认PHP版本:

$php --version
PHP 7.4.3 (cli) (built: Mar 26 2017 20:24:23) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

列出所有加载的PHP模块:

$php -m

在Ubuntu 18.04/16.04上安装PHP 7.4

对于其他版本的Ubuntu,请使用以下过程:

第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步:在Ubuntu 18.04/19.04/16.04上安装PHP 7.4

使用命令在Ubuntu 18.04/19.04/16.04上安装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

使用下一个命令安装其他包:

sudo apt-get install php7.4-xxx

例子:

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

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

使用php 7.4与ubuntu上的nginx:

在Ubuntu上安装PHP配置Apache。
对于有兴趣运行nginx和PHP的用户,我们需要停止和禁用Apache服务。

sudo systemctl disable --now apache2

然后安装FPM扩展和nginx包。

sudo apt-get install nginx php7.4-fpm

应该运行nginx和fpm服务。

$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。