![尝试安装 PHP 7.4,但获取的是 PHP 8.1.2](https://linux22.com/image/1188839/%E5%B0%9D%E8%AF%95%E5%AE%89%E8%A3%85%20PHP%207.4%EF%BC%8C%E4%BD%86%E8%8E%B7%E5%8F%96%E7%9A%84%E6%98%AF%20PHP%208.1.2.png)
为了我的需要,我需要安装PHP 7.4
(和 Apache2 的模块),而不是最新的PHP 8.x
。
因此我运行以下命令:apt-get install -y php7.4 libapache2-mod-php7.4
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Note, selecting 'libapache2-mod-php7.4' for regex 'php7.4'
Note, selecting 'php7.4-fpm' for regex 'php7.4'
Note, selecting 'php7.4-common' for regex 'php7.4'
The following additional packages will be installed:
libapache2-mod-php libapache2-mod-php8.1 php-common php8.1-cli php8.1-common php8.1-opcache php8.1-readline
Suggested packages:
php-pear
The following NEW packages will be installed:
libapache2-mod-php libapache2-mod-php7.4 libapache2-mod-php8.1 php-common php8.1-cli php8.1-common php8.1-opcache php8.1-readline
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 840 B/5193 kB of archives.
After this operation, 21.9 MB of additional disk space will be used.
Get:1 http://eu-frankfurt-1-ad-3.clouds.ports.ubuntu.com/ubuntu-ports jammy-updates/universe arm64 libapache2-mod-php7.4 arm64 8.1.2-1ubuntu2.2 [840 B]
Fetched 840 B in 0s (23.5 kB/s)
Selecting previously unselected package php-common.
(Reading database ... 114642 files and directories currently installed.)
Preparing to unpack .../0-php-common_2%3a92ubuntu1_all.deb ...
Unpacking php-common (2:92ubuntu1) ...
Selecting previously unselected package php8.1-common.
Preparing to unpack .../1-php8.1-common_8.1.2-1ubuntu2.2_arm64.deb ...
Unpacking php8.1-common (8.1.2-1ubuntu2.2) ...
Selecting previously unselected package php8.1-opcache.
Preparing to unpack .../2-php8.1-opcache_8.1.2-1ubuntu2.2_arm64.deb ...
Unpacking php8.1-opcache (8.1.2-1ubuntu2.2) ...
Selecting previously unselected package php8.1-readline.
Preparing to unpack .../3-php8.1-readline_8.1.2-1ubuntu2.2_arm64.deb ...
Unpacking php8.1-readline (8.1.2-1ubuntu2.2) ...
Selecting previously unselected package php8.1-cli.
Preparing to unpack .../4-php8.1-cli_8.1.2-1ubuntu2.2_arm64.deb ...
Unpacking php8.1-cli (8.1.2-1ubuntu2.2) ...
Selecting previously unselected package libapache2-mod-php8.1.
Preparing to unpack .../5-libapache2-mod-php8.1_8.1.2-1ubuntu2.2_arm64.deb ...
Unpacking libapache2-mod-php8.1 (8.1.2-1ubuntu2.2) ...
Selecting previously unselected package libapache2-mod-php.
Preparing to unpack .../6-libapache2-mod-php_2%3a8.1+92ubuntu1_all.deb ...
Unpacking libapache2-mod-php (2:8.1+92ubuntu1) ...
Selecting previously unselected package libapache2-mod-php7.4.
Preparing to unpack .../7-libapache2-mod-php7.4_8.1.2-1ubuntu2.2_arm64.deb ...
Unpacking libapache2-mod-php7.4 (8.1.2-1ubuntu2.2) ...
Setting up php-common (2:92ubuntu1) ...
Setting up php8.1-common (8.1.2-1ubuntu2.2) ...
Setting up php8.1-readline (8.1.2-1ubuntu2.2) ...
Setting up php8.1-opcache (8.1.2-1ubuntu2.2) ...
Setting up php8.1-cli (8.1.2-1ubuntu2.2) ...
update-alternatives: using /usr/bin/php8.1 to provide /usr/bin/php (php) in auto mode
update-alternatives: using /usr/bin/phar8.1 to provide /usr/bin/phar (phar) in auto mode
update-alternatives: using /usr/bin/phar.phar8.1 to provide /usr/bin/phar.phar (phar.phar) in auto mode
Setting up libapache2-mod-php8.1 (8.1.2-1ubuntu2.2) ...
apache2_invoke: Enable module php8.1
Setting up libapache2-mod-php7.4 (8.1.2-1ubuntu2.2) ...
Setting up libapache2-mod-php (2:8.1+92ubuntu1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for php8.1-cli (8.1.2-1ubuntu2.2) ...
Processing triggers for libapache2-mod-php8.1 (8.1.2-1ubuntu2.2) ...
Scanning processes...
Scanning linux images...
Running kernel seems to be up-to-date.
No services need to be restarted.
No containers need to be restarted.
No user sessions are running outdated binaries.
No VM guests are running outdated hypervisor (qemu) binaries on this host.
但是当我检查我安装的 PHP 版本时php --version
,我得到了以下信息:
PHP 8.1.2 (cli) (built: Jul 21 2022 12:10:37) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies
问题
为什么PHP 8.1.2
我明确尝试安装后还是会出现PHP 7.4
?如何安装PHP 7.4
后不留下任何痕迹PHP 8.x
?
答案1
不是 100% 确定,但看起来你没有指定精确的软件包名称...我建议您通过命令
查找软件包名称 此外,您可能要查看您可能想要添加的模块(就您而言,我认为已经没问题了)。 有了这些,我认为您的最终安装命令行应该是: 无论如何,如果两个版本仍然继续安装,应该可以帮助您处理它。apt-cache policy php7.4*
apt list |grep php7.4
sudo apt-get install -y php7.4-common libapache2-mod-php7.4
sudo update-alternative --config php
答案2
在您想要 php7.4 的目录中,创建一个 .htaccess 文件并在其中添加以下内容:
<FilesMatch "\.(php4|php5|php3|php2|php|phtml)$">
AddHandler application/x-httpd-php74 .php
</FilesMatch>
通常最好让最新的 php 版本在整个服务器上运行,并且只为每个子域、目录或您正在使用的任何文件添加特殊情况。