温馨提示×

Debian LNMP怎么提速

小樊
36
2026-10-13 16:02:03
栏目: 智能运维

在 Debian 上搭建的 LNMP(Linux + Nginx + MySQL/MariaDB + PHP) 环境,提速一般从 系统层、Nginx、PHP、MySQL、缓存、静态资源 几个方面入手。下面按实战优先级给你一份「可操作清单」。


一、系统层优化(基础)

1. 关闭不必要的服务

systemctl list-unit-files --type=service | grep enabled

关闭用不到的:

systemctl disable --now xxx

2. 调整文件描述符

ulimit -n 65535

永久生效:

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

3. 开启 Swap(小内存机器)

fallocate -l 2G /swapfile
mkswap /swapfile
swapon /swapfile

二、Nginx 提速(最关键)

1. worker 进程优化

worker_processes auto;
worker_connections 10240;
use epoll;

2. 开启 Gzip

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

3. 开启文件缓存

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

4. 静态资源缓存

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

5. 开启 HTTP/2

listen 443 ssl http2;

三、PHP 提速(PHP-FPM)

1. 使用 PHP 8.x(强烈建议)

apt install php8.2-fpm

2. PHP-FPM 进程优化

/etc/php/8.2/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

3. 开启 OPcache(提速 2–5 倍)

opcache.enable=1
opcache.memory_consumption=128
opcache.revalidate_freq=60

4. 禁用无用函数

disable_functions = exec,passthru,shell_exec,system

四、MySQL / MariaDB 提速

1. 基础参数(2G内存示例)

/etc/mysql/my.cnf

innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
query_cache_type = 0
max_connections = 200

2. 使用 MariaDB(比 MySQL 轻)

apt install mariadb-server

3. 慢查询分析

SHOW FULL PROCESSLIST;

五、缓存层(立竿见影)

1. Redis 缓存

apt install redis-server

PHP:

apt install php-redis

2. Nginx 缓存(动态页)

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

六、网站层面优化

  • 图片压缩(webp)
  • JS/CSS 合并
  • CDN(Cloudflare / 国内 CDN)
  • 数据库索引优化
  • 减少外部请求

七、一键检测工具

apt install htop iotop netdata

或:

curl -sL https://github.com/domoticz/linux-dash | bash

总结(优先级)

✅ PHP 8 + OPcache
✅ Nginx Gzip + HTTP/2
✅ Redis 缓存
✅ MySQL 内存参数
✅ 静态资源缓存

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

  • Debian 版本
  • 内存大小
  • 网站类型(WordPress / 自建框架)

我可以给你 定制一份 LNMP 配置。

0 踩