在 Debian 上配置 Laravel 缓存,通常包含两部分:
下面按常见场景说明。
Laravel 缓存配置文件在:
config/cache.php
环境变量在 .env 中设置:
CACHE_DRIVER=redis
# 或
CACHE_DRIVER=memcached
# 或
CACHE_DRIVER=file
修改后清缓存:
php artisan config:clear
php artisan cache:clear
Debian 上默认即可用:
CACHE_DRIVER=file
缓存文件在:
storage/framework/cache
✅ 适合小型项目或测试环境
sudo apt update
sudo apt install redis-server -y
启动并设置开机自启:
sudo systemctl enable redis-server
sudo systemctl start redis-server
测试:
redis-cli ping
# 返回 PONG
sudo apt install php-redis -y
sudo systemctl restart apache2 # 或 php-fpm
.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');
sudo apt install memcached libmemcached-tools -y
sudo systemctl enable memcached
sudo systemctl start memcached
sudo apt install php-memcached -y
sudo systemctl restart apache2
CACHE_DRIVER=memcached
MEMCACHED_HOST=127.0.0.1
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),我可以给你一份更精确的配置。