以下是在 Ubuntu 24.04 安装 PHP 8.4 并配置到 Apache 或 Nginx 的方法。
1. 安装 Apache 或 Nginx:
Apache 及 Nginx 各有优点,可以根据自身的需要选择要安装那一个网页服务器:
Apache:
$ sudo apt install apache2 -y
Nginx:
$ sudo apt install nginx -y
2. 安装 PHP 8.4
Ubuntu 24.04 默认 Repositories 没有包括 PHP 8.4,需要先加入相关的 repository:
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt update
$ sudo apt install php8.4 libapache2-mod-php8.4 php8.4-fpm php8.4-mysql php8.4-xml php8.4-mbstring php8.4-curl -y
$ sudo apt update
$ sudo apt install php8.4 libapache2-mod-php8.4 php8.4-fpm php8.4-mysql php8.4-xml php8.4-mbstring php8.4-curl -y
3. 配置 PHP 8.4 到 Apache:
如果系统内原来已经安装其他 PHP 版本,例如 PHP 8.3,这时需要把其他 PHP 版本关闭,并启用 PHP 8.4:
$ sudo a2dismod php8.3
$ sudo a2enmod php8.4
$ sudo a2enmod php8.4
现在重新启动 Apache:
$ sudo systemctl restart apache2
4. 配置 PHP 8.4 到 Nginx
如果使用 Nginx 作为网页服务器,开启 Nginx 的设定档:
$ sudo vi /etc/nginx/sites-available/default
找到 PHP 档案的段落,改成以下这样:
|
1 2 3 4 |
location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.4-fpm.sock; } |
储存盘案后重新启动 Nginx 及 PHP-FPM:
$ sudo systemctl restart nginx
$ sudo systemctl restart php8.4-fpm
$ sudo systemctl restart php8.4-fpm
以上是在 Ubuntu 24.04 安装 PHP 8.4 的方法。