温馨提示×

debian lnmp如何提速

小樊
45
2026-09-16 23:49:26
栏目: 智能运维

在 Debian 上搭建的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP)环境,提速通常从系统层、Nginx、PHP、MySQL、缓存、硬盘/网络几个维度入手。下面给你一套实用可落地的优化方案。


一、系统层优化

1. 关闭无用服务

systemctl disable --now bluetooth
systemctl disable --now cups

只保留 SSH、Nginx、PHP-FPM、MySQL

2. 调整文件描述符

ulimit -n 65535

写入:

/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535

3. 内核参数(高并发)

/etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
sysctl -p

二、Nginx 提速

1. 开启 Gzip

gzip on;
gzip_min_length 1k;
gzip_comp_level 5;
gzip_types text/css application/javascript image/svg+xml;

2. 开启缓存

open_file_cache max=10000 inactive=30s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;

3. 静态资源缓存

location ~* \.(js|css|png|jpg|gif|ico|svg)$ {
    expires 30d;
    access_log off;
}

4. 使用 HTTP/2

listen 443 ssl http2;

三、PHP 优化(PHP-FPM)

1. 调整进程数

/etc/php/*/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20

2. 开启 OPcache

/etc/php/*/fpm/conf.d/10-opcache.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60

3. 关闭无用函数(安全+性能)

disable_functions = exec,passthru,shell_exec,system

四、MySQL / MariaDB 提速

1. 基础配置

/etc/mysql/my.cnf
[mysqld]
innodb_buffer_pool_size = 1G   # 内存一半
innodb_log_file_size = 256M
query_cache_type = 0
max_connections = 200

2. 慢查询分析

SHOW PROCESSLIST;

开启慢日志:

slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1

五、缓存加速(强烈推荐)

1. Redis(PHP 会话 / 缓存)

apt install redis-server

PHP:

session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"

2. Nginx 静态缓存(可选)

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:10m;

六、硬盘 & 网络

  • SSD 必选(LNMP 提速最明显)
  • 使用 ext4 或 xfs
  • 挂载参数:
noatime,nodiratime

七、常见提速组合(推荐)

✅ Nginx + PHP-FPM + OPcache
✅ Redis 缓存
✅ Gzip + 浏览器缓存
✅ SSD + 合理 MySQL 参数


如果你愿意,可以告诉我:

  • Debian 版本
  • PHP / Nginx / MySQL 版本
  • 是 网站 / API / 商城 / WordPress
  • 服务器配置(CPU / 内存)

我可以直接给你一份可直接用的配置文件。

0 踩