在 Debian 上安装 PHP 7,取决于你使用的 Debian 版本 以及你需要的 PHP 7.x 具体版本。下面按常见场景给你详细说明。
cat /etc/debian_version
常见版本对应关系:
sudo apt update
sudo apt install php7.4 php7.4-cli php7.4-fpm php7.4-mysql php7.4-curl php7.4-mbstring php7.4-xml
常用扩展说明:
php7.4-fpm:用于 Nginxphp7.4-mysql:MySQL 支持php7.4-curl:HTTP 请求php7.4-mbstring:多字节字符串php7.4-xml:XML 支持Debian 12 默认只有 PHP 8,需要 第三方源(SURY)。
sudo apt install -y curl wget gnupg2 ca-certificates lsb-release apt-transport-https
curl -sSL https://packages.sury.org/php/README.txt
wget -qO - https://packages.sury.org/php/apt.gpg | sudo apt-key add -
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/sury-php.list
sudo apt update
sudo apt install php7.4 php7.4-fpm php7.4-mysql
php -v
输出示例:
PHP 7.4.33 (cli)
sudo systemctl enable php7.4-fpm
sudo systemctl start php7.4-fpm
Nginx 配置示例:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
sudo apt install libapache2-mod-php7.4
sudo systemctl restart apache2
sudo update-alternatives --config php
如果你愿意,可以告诉我:
我可以给你 精确的命令和配置方案。