优化 Debian 上的 PHP,通常可以从系统层面、PHP 本身配置、PHP-FPM(如适用)、Opcode/缓存、以及应用层几个方向来做。下面按实用顺序给你一套清晰可操作的方案。
Debian 自带版本往往偏旧,建议用 Sury 仓库安装较新 PHP:
apt install lsb-release ca-certificates apt-transport-https
curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg
echo "deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list
apt update
apt install php8.3 php8.3-fpm php8.3-cli
✅ 新版本 PHP 性能更好(8.1+ 明显优于 7.x)
路径示例(CLI / FPM 分开):
/etc/php/8.3/fpm/php.ini
/etc/php/8.3/cli/php.ini
memory_limit = 256M ; 按需调整
max_execution_time = 30
max_input_time = 60
post_max_size = 64M
upload_max_filesize = 64M
expose_php = Off
display_errors = Off
log_errors = On
apt install php8.3-opcache
/etc/php/8.3/fpm/conf.d/10-opcache.ini:
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.validate_timestamps=0 ; 生产环境建议关
✅ 可提升 30%~300% 性能
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
或高并发:
pm = static
pm.max_children = 100
max_children × 单进程内存php-fpm status 观察listen = /run/php/php8.3-fpm.sock
ulimit -n 65535
/etc/security/limits.conf:
* soft nofile 65535
* hard nofile 65535
systemctl disable --now apache2 # 如果用 nginx
apt install cpufrequtils
cpufreq-set -g performance
apt install php8.3-redis
expose_php = Off
disable_functions = exec,passthru,shell_exec,system
php -v
php -m | grep opcache
ab -n 1000 -c 100 http://localhost/
| 场景 | 建议 |
|---|---|
| 小型站点 | pm=dynamic, opcache on |
| API 服务 | static + redis |
| 高并发 | socket + opcache + nginx |
如果你愿意,可以告诉我:
我可以直接给你一套定制配置。