参考
如何在 Ubuntu 20.04 上安装 PHP
ubuntu20.4安装指定版本php
ubuntu 20.04 直接安装的 php版本是 php7.4 ,总有些php构造的网站还不完全支持高版本,所以,经常会同时安装若干个 php 版本
直接 apt 安装-
sudo apt update
-
sudo apt install php-fpm
这个安装的是 php 7.4
以下再使用 ppa 方式安装一个 php 7.2
-
安装必要依赖软件
apt -y install software-properties-common apt-transport-https lsb-release ca-certificates -
加入 ppa 源
add-apt-repository ppa:ondrej/php... 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 ... 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. ...按照提示,因为我这里使用 nginx
$ sudo add-apt-repository ppa:ondrej/nginx-mainline
$ apt update
$ apt search php7.2
确认可以找到 php 7.2
-
开始安装
sudo apt install php7.2 libapache2-mod-php7.2 php7.2-fpm php7.2-mysql php7.2-curl php7.2-gd php7.2-mbstring php7.2-xml php7.2-xmlrpc php7.2-zip php7.2-opcache php7.2-gmp php7.2-bcmath php7.2-dom确认安装信息
$ php -m
$ php -v
-
$ which php
/usr/bin/php
-
$ whereis php
php: /usr/bin/php7.4 /usr/bin/php7.2 /usr/bin/php /usr/lib/php /etc/php /usr/share/php7.2-xmlrpc /usr/share/php7.2-opcache /usr/share/php7.2-gmp /usr/share/php7.2-mysql /usr/share/php7.4-json /usr/share/php7.2-gd /usr/share/php7.4-readline /usr/share/php7.2-common /usr/share/php7.2-curl /usr/share/php /usr/share/php7.2-xml /usr/share/php7.4-common /usr/share/php7.2-mbstring /usr/share/php7.2-zip /usr/share/php7.4-opcache /usr/share/php7.2-readline /usr/share/php7.2-bcmath /usr/share/php7.2-json /usr/share/man/man1/php.1.gz
-
$ cd /usr/bin/
$ ll php*
lrwxrwxrwx 1 root root 21 Sep 30 01:52 php -> /etc/alternatives/php* -rwxr-xr-x 1 root root 5047480 Aug 26 15:55 php7.2* -rwxr-xr-x 1 root root 4773816 Aug 13 05:39 php7.4*
目前存在 3 个可执行脚本 php7.2 、 php7.4 和 php
-
$ cd /etc/alternatives
$ ll php*
rwxrwxrwx 1 root root 15 Sep 30 01:52 php -> /usr/bin/php7.4* lrwxrwxrwx 1 root root 31 Sep 30 01:52 php.1.gz -> /usr/share/man/man1/php7.4.1.gz lrwxrwxrwx 1 root root 24 Sep 30 02:11 php-fpm.sock -> /run/php/php7.4-fpm.sock=
-
尝试切换缺省 php 版本
$ sudo mv php php.old
$ sudo ln -s /usr/bin/php7.2 php
$ ll php*lrwxrwxrwx 1 root root 15 Oct 2 01:06 php -> /usr/bin/php7.2* lrwxrwxrwx 1 root root 31 Sep 30 01:52 php.1.gz -> /usr/share/man/man1/php7.4.1.gz lrwxrwxrwx 1 root root 24 Sep 30 02:11 php-fpm.sock -> /run/php/php7.4-fpm.sock= lrwxrwxrwx 1 root root 15 Sep 30 01:52 php.old -> /usr/bin/php7.4*
感觉没有必要这么折腾!分别使用 php 和 php7.2 ,没有什么障碍!
$ php -v
PHP 7.4.3 (cli) (built: Aug 13 2021 05:39:12) ( 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$ php7.2 -v
PHP 7.2.34-24+ubuntu20.04.1+deb.sury.org+1 (cli) (built: Aug 26 2021 15:55:49) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.34-24+ubuntu20.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies$ php7.4 -v 同 $ php -v
-
这里直接修改缺省站点
cd /etc/nginx/sites-available/
sudo vim default
location ~ .php$ { include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; # fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; } -
重启 nginx 服务生效
sudo service nginx reload
Ubuntu PPA 使用指南
这个 ppa:ondrej/php 在哪里找到的PPA 软件源 ppa:ondrej/php 在哪里找到的



