温馨提示×

laravel在debian怎么配缓存

小樊
47
2026-10-22 19:49:54
栏目: 智能运维

在 Debian 上配置 Laravel 缓存,通常包含两部分:

  1. 配置 Laravel 使用哪种缓存驱动
  2. 在 Debian 上安装并运行对应的缓存服务(如 Redis / Memcached)

下面按常见场景说明。


一、Laravel 缓存基础配置

Laravel 缓存配置文件在:

config/cache.php

环境变量在 .env 中设置:

CACHE_DRIVER=redis
# 或
CACHE_DRIVER=memcached
# 或
CACHE_DRIVER=file

修改后清缓存:

php artisan config:clear
php artisan cache:clear

二、使用 File 缓存(最简单,无需安装)

Debian 上默认即可用:

CACHE_DRIVER=file

缓存文件在:

storage/framework/cache

✅ 适合小型项目或测试环境


三、使用 Redis(推荐生产环境)

1. Debian 安装 Redis

sudo apt update
sudo apt install redis-server -y

启动并设置开机自启:

sudo systemctl enable redis-server
sudo systemctl start redis-server

测试:

redis-cli ping
# 返回 PONG

2. 安装 PHP Redis 扩展

sudo apt install php-redis -y
sudo systemctl restart apache2   # 或 php-fpm

3. 配置 Laravel

.env:

CACHE_DRIVER=redis

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

测试:

php artisan tinker
Cache::put('test', 'hello', 10);
Cache::get('test');

四、使用 Memcached

1. 安装 Memcached

sudo apt install memcached libmemcached-tools -y
sudo systemctl enable memcached
sudo systemctl start memcached

2. 安装 PHP 扩展

sudo apt install php-memcached -y
sudo systemctl restart apache2

3. Laravel 配置

CACHE_DRIVER=memcached

MEMCACHED_HOST=127.0.0.1

五、使用 APCu(单机 PHP 缓存)

sudo apt install php-apcu -y
sudo systemctl restart apache2

.env:

CACHE_DRIVER=apc

⚠️ 不适合多服务器


六、权限问题注意

确保 storage 可写:

sudo chown -R www-data:www-data storage
sudo chmod -R 775 storage

七、常用命令

php artisan cache:clear
php artisan config:clear
php artisan cache:forget key

推荐方案总结

场景 推荐
本地开发 file
单服务器 redis
高并发 redis
多服务器 redis / memcached

如果你告诉我 Laravel 版本 + Debian 版本 + Web 服务(Apache / Nginx + PHP-FPM),我可以给你一份更精确的配置。

0 踩