下面给你几种在 Ubuntu 上安装 PHP-FPM 的常见方式,按使用场景从简单到进阶说明。
sudo apt update
sudo apt install php8.2-fpm
如果你不确定版本,可以先查看可用版本:
apt search php-fpm
systemctl status php8.2-fpm
启动 / 重启 / 停止:
sudo systemctl start php8.2-fpm
sudo systemctl restart php8.2-fpm
sudo systemctl stop php8.2-fpm
sudo systemctl enable php8.2-fpm
sudo apt install php8.2-fpm php8.2-mysql php8.2-xml php8.2-curl php8.2-mbstring php8.2-zip php8.2-gd
/etc/php/8.2/fpm/php-fpm.conf
/etc/php/8.2/fpm/pool.d/www.conf
/etc/php/8.2/fpm/php.ini
在 www.conf 中:
listen = /run/php/php8.2-fpm.sock
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}
}
重载 Nginx:
sudo systemctl reload nginx
sudo apt install php8.1-fpm php8.2-fpm
切换版本时只需修改 Nginx 中的 fastcgi_pass。
| Ubuntu 版本 | 默认 PHP |
|---|---|
| 20.04 | 7.4 |
| 22.04 | 8.1 |
| 24.04 | 8.3 |
journalctl -u php8.2-fpm
如果你告诉我:
我可以给你更精确的配置示例。