在 Debian 13.5 將 libapache2-mod-php8.4升級到php8.5-fpm

回覆文章
Brave Ye
系統管理員
文章: 4747
註冊時間: 2025-11-08, 13:32
聯繫:

在 Debian 13.5 將 libapache2-mod-php8.4升級到php8.5-fpm

文章 Brave Ye »

Search Assist



To set up a Debian 13.5 (Trixie) mirror in Taiwan, you can add the following lines to your /etc/apt/sources.list file:

代碼: 選擇全部

deb http://deb.debian.org/debian trixie main
deb http://deb.debian.org/debian-security trixie-security main
deb http://deb.debian.org/debian trixie-updates main



更新必要套件與儲存庫金鑰
sudo apt update
Install Required Packages
Ensure you have the necessary tools to add the repository:
bash

代碼: 選擇全部

sudo apt install apt-transport-https lsb-release ca-certificates curl
Download and Add the Repository Key
Use the following command to download the GPG key:
bash

代碼: 選擇全部

sudo curl -fsSL https://packages.sury.org/php/apt.gpg -o /usr/share/keyrings/sury-php.gpg
Add the Repository
Add the SURY repository to your sources list:

代碼: 選擇全部

echo "deb [signed-by=/usr/share/keyrings/sury-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list

sudo apt update

代碼: 選擇全部

sudo apt install php8.5-fpm libapache2-mod-fcgid php8.5-cli php8.5-common \
  php8.5-mysql  php8.5-curl php8.5-gd php8.5-mbstring \
  php8.5-xml php8.5-zip php8.5-intl php8.5-redis \
  php8.5-bcmath php8.5-soap php8.5-imagick php8.5-zip \
  mariadb-server mariadb-client \
  php8.5-apcu  php8.5-gd  php8.5-ldap 

sudo a2enmod actions proxy proxy_fcgi fcgid
sudo systemctl restart apache2

If you are setting up a virtual host for your website, you must enable ExecCGI within your Apache site configuration so that the FastCGI handler is permitted to run scripts (e.g., Options Indexes FollowSymLinks ExecCGI

sudo nano /etc/apache2/sites-available/virtual.host.conf

代碼: 選擇全部

<VirtualHost *:80>
    ServerName zine-tw.com
 #   ServerAlias ://example.com
    DocumentRoot /var/www/html
ServerAdmin yourname@gmail.com
    <Directory /var/www/html>
        Options Indexes FollowSymLinks ExecCGI
        AllowOverride All
        Require all granted
    </Directory>

    # Route PHP requests to PHP 8.5-FPM via proxy_fcgi
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php/php8.5-fpm.sock|fcgi://localhost"
    </FilesMatch>

    ErrorLog /var/log/apache2/virtual.host.error.log
    CustomLog /var/log/apache2/virtual.host.access.log combined
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
sudo a2enmod rewrite
sudo a2ensite virtual.host.conf
sudo systemctl restart apache2

啟用必要的 Apache 模組
切換至 PHP-FPM 必須啟用 proxy_fcgi 及 actions 模組
sudo a2enmod proxy proxy_fcgi actions alias setenvif
關閉舊版 libapache2-mod-php8.4 模組
sudo a2dismod php8.4
sudo a2enconf php8.5-fpm

sudo nano /etc/php/8.5/fpm/php.ini

Key settings to change from defaults:

代碼: 選擇全部

; Memory and limits
memory_limit = 256M
max_execution_time = 60
max_input_time = 60
max_input_vars = 5000

; Uploads
upload_max_filesize = 64M
post_max_size = 64M

; Error handling
display_errors = Off
log_errors = On
error_log = /var/log/php8.5-fpm.log

; Timezone
date.timezone = UTC

; OPcache
opcache.enable = 1
opcache.memory_consumption = 256
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 20000
opcache.validate_timestamps = 0

; JIT (PHP 8.5 defaults to 64M buffer)
opcache.jit = tracing
opcache.jit_buffer_size = 64M

The opcache.validate_timestamps = 0 setting is important for production – PHP won’t stat files on every request. After deploying new code, restart PHP-FPM to pick up changes. Set it to 1 during development.

sudo systemctl restart php8.5-fpm

Step 6: Configure PHP-FPM Pool
The default pool config is at

代碼: 選擇全部

/etc/php/8.5/fpm/pool.d/www.conf
The defaults are conservative:
sudo nano /etc/php/8.5/fpm/pool.d/www.conf

代碼: 選擇全部

pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
For a 2 GB server, adjust to:

代碼: 選擇全部

sudo nano /etc/php/8.5/fpm/pool.d/www.conf

Copy
Add or update the following settings:

代碼: 選擇全部

pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 10
pm.max_requests = 500

; Slow log for finding bottlenecks
slowlog = /var/log/php8.5-fpm-slow.log
request_slowlog_timeout = 5s

To calculate pm.max_children for your server, check how much memory each PHP worker uses:

代碼: 選擇全部

ps -eo rss,comm | grep php-fpm | awk '{sum+=$1; n++} END {print sum/n/1024 " MB per process"}'
Copy
Divide your available RAM (minus what the OS and other services need) by that number.

sudo systemctl restart php8.5-fpm

php.ini changes not taking effect:

Debian has separate php.ini files for CLI and FPM. Web changes go in /etc/php/8.5/fpm/php.ini, not /etc/php/8.5/cli/php.ini. Always restart FPM after changes.

sudo systemctl restart php8.5-fpm

重啟 Apache 伺服器
sudo systemctl restart apache2
驗證
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
php8.5-fpm
php8.5-fpm
螢幕快照 2026-06-09 08-10-39.png (32.52 KiB) 已瀏覽 1400 次
回覆文章

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 1 位訪客