在 Debian 上配置 Laravel 缓存,通常包含两部分:
下面按常见方案说明。
Laravel 默认就是文件缓存。
编辑 .env:
CACHE_DRIVER=file
对应 config/cache.php 中:
'default' => env('CACHE_DRIVER', 'file'),
缓存文件位于
storage/framework/cache
优点:简单
缺点:高并发性能一般
sudo apt update
sudo apt install redis-server
启动并设置开机自启:
sudo systemctl enable redis-server
sudo systemctl start redis-server
测试:
redis-cli ping
# 返回 PONG 表示正常
sudo apt install php-redis
重启 PHP(根据你用的 PHP 版本):
sudo systemctl restart php8.2-fpm
# 或
sudo systemctl restart apache2
编辑 .env:
CACHE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
config/cache.php 中确认:
'redis' => [
'driver' => 'redis',
'connection' => 'cache',
],
php artisan tinker
>>> Cache::put('test', 'hello', 60);
>>> Cache::get('test');
sudo apt install memcached libmemcached-tools
sudo apt install php-memcached
sudo systemctl restart php8.2-fpm
CACHE_DRIVER=memcached
php artisan cache:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache
redis.conf 最大内存策略如果你愿意,可以告诉我:
我可以给你一套更精确的 Debian + Laravel 缓存配置方案。