温馨提示×

debian如何提升laravel性能

小樊
52
2026-09-23 13:04:34
栏目: 智能运维

在 Debian 上提升 Laravel 性能,可以从 系统层、PHP 层、Laravel 层、缓存与队列、运维配置 多个维度来做。下面按实战优先级整理。


一、PHP 与运行环境优化(最关键)

1. 使用 PHP 8.1 / 8.2 / 8.3(推荐 8.2+)

sudo apt install php8.2 php8.2-fpm php8.2-cli \
php8.2-mysql php8.2-redis php8.2-opcache php8.2-mbstring php8.2-xml

2. 开启 OPcache(必开)

编辑:

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

生产环境 validate_timestamps=0,改代码后需重启 PHP-FPM

sudo systemctl restart php8.2-fpm

二、Laravel 自身性能优化(必做)

1. 生产环境配置

APP_ENV=production
APP_DEBUG=false

2. 配置缓存(非常关键)

php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache

修改配置后必须重新执行

3. 关闭未使用服务

config/app.php 中移除不需要的 providers


三、Web 服务器优化(Nginx 为例)

1. 使用 PHP-FPM + Nginx

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}

2. 启用 Gzip / Brotli

gzip on;
gzip_types text/css application/javascript application/json;

3. 静态资源走 CDN 或本地缓存

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

四、数据库优化(Laravel 性能瓶颈常在这里)

1. 使用 Redis 作为缓存 & Session

CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis

安装 Redis:

sudo apt install redis-server

2. 开启慢查询日志

SET slow_query_log = 1;
SET long_query_time = 1;

3. 使用索引 & 避免 N+1

User::with('posts')->get();

五、队列与异步任务(非常重要)

1. 使用队列处理耗时任务

php artisan queue:work --daemon

2. Supervisor 守护队列

sudo apt install supervisor

示例:

[program:laravel-worker]
command=php /var/www/artisan queue:work --daemon
autostart=true
autorestart=true

六、Debian 系统层优化

1. 调整文件描述符

ulimit -n 65535

2. 使用 SSD & 足够内存

Laravel + Redis + OPcache 对内存友好

3. 关闭不必要的服务

systemctl disable bluetooth

七、部署方式推荐(性能最佳)

✅ 推荐组合

  • Debian 12
  • Nginx
  • PHP-FPM
  • OPcache
  • Redis
  • Supervisor
  • Laravel Octane(高并发场景)

Laravel Octane(进阶)

composer require laravel/octane
php artisan octane:start

八、一键性能检查清单 ✅

  • [ ] PHP 8.2+
  • [ ] OPcache 开启
  • [ ] config/route/view cache
  • [ ] Redis 缓存
  • [ ] 队列异步
  • [ ] Nginx 静态缓存
  • [ ] APP_DEBUG=false

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

  • 当前 Debian 版本
  • PHP 版本
  • 是否用 Docker
  • 访问量级别(并发)

我可以给你一套 针对性配置方案。

0 踩