去除 apcu
# 如果已经使用过 apcu 作为缓存,要先去除 apcu 缓存
grep -r apc /etc/php/8.2/ # 找到 apc 相关的配置行
sudo emacs /etc/php/8.2/mods-available/apcu.ini # 注释掉对 apcu 的调用
# 进 LocalSettings.php 取消对 CACHE_ACCEL 的调用
## Shared memory settings
## $wgMainCacheType = CACHE_ACCEL;
## $wgMemCachedServers = [];
使用 redis
# 安装并配置 redis 作缓存后端
sudo apt install redis-server php-redis
# 编辑 LocalSettings.php
$wgObjectCaches['redis'] = [
'class' => 'RedisBagOStuff',
'servers' => [ '127.0.0.1:6379' ],
// 'connectTimeout' => 1,
// 'persistent' => false,
// 'password' => 'secret',
// 'automaticFailOver' => true,
];
$wgMainCacheType = 'redis';
$wgParserCacheType = 'redis'; // 可选
$wgMessageCacheType = 'redis'; // 可选
// 若使用新版 API,可直接写 'redis'
// $wgMainCacheType = 'redis';
## $smwgMainCacheType = CACHE_REDIS;
$wgJobTypeConf['default'] = [
'class' => 'JobQueueRedis',
'redisServer' => '127.0.0.1:6379',
'redisConfig' => [],
'daemonized' => true
];
验证生效
# 验证 redis 正常工作的方法三选一即可
# 验证 redis 正常工作方法一
# 把以下写入 test.php 并打开,能看到 Hello, Redis! 返回
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
// 设置一个键值对
$redis->set('test_key', 'Hello, Redis!');
// 获取并输出该键的值
echo $redis->get('test_key');
?>
# 验证 redis 正常工作方法二
# 在需要验证的页面用 ?action=purge 删掉缓存
# 用 <code>redis-cli</code> 进入 redis 环境
KEYS * # 能看到一堆该 mediawiki 数据库相关的输出
# 验证 redis 正常工作方法三
# 在 LocalSettings.php 中加入以下项查看日志
$wgDebugLogGroups['redis'] = '/tmp/redis.log';
微调配置
#################################
# 1. 基础持久化设置(AOF + RDB)
#################################
# 开启 AOF 持久化,每秒同步到磁盘
appendonly yes # :contentReference[oaicite:0]{index=0}
appendfilename "appendonly.aof" # :contentReference[oaicite:1]{index=1}
appendfsync everysec # 推荐 everysec,在性能与安全间取平衡 :contentReference[oaicite:2]{index=2}
# 保留默认 RDB 快照规则(作二级备份)
save 900 1 # 900 秒内有至少 1 次写操作时进行快照 :contentReference[oaicite:3]{index=3}
save 300 10 # 300 秒内有至少10 次写操作时进行快照 :contentReference[oaicite:4]{index=4}
save 60 10000 # 60 秒内有至少10000 次写操作时进行快照 :contentReference[oaicite:5]{index=5}
dir /var/lib/redis # RDB/AOF 文件存放目录 :contentReference[oaicite:6]{index=6}
################################################
# 2. 内存管理与淘汰策略(防止 OOM / 大对象泛滥)
################################################
# 设置 Redis 可用的最大内存,例如对小站点分配 256 MB
maxmemory 256mb # :contentReference[oaicite:7]{index=7}
# 当超过内存限制时,使用 LRU 淘汰所有键,保留常用热点
maxmemory-policy allkeys-lru # :contentReference[oaicite:8]{index=8}
############################################
# 3. 网络、线程与安全(小型部署即可默认)
############################################
# 绑定本地接口,仅允许本地访问
bind 127.0.0.1 # :contentReference[oaicite:9]{index=9}
protected-mode yes # 默认开启,防止未授权访问 :contentReference[oaicite:10]{index=10}
# 可选:设置简单访问密码(生产环境推荐)
# requirepass your_redis_password
##################################
# 4. 其他可选优化(根据需求启用)
##################################
# # 关闭 AOF 混合持久化前导快照(免得每次重写开销太大)
# aof-use-rdb-preamble no
# # 开启主动碎片整理(Redis ≥4.0,当内存使用率过高时)
# activedefrag yes # :contentReference[oaicite:11]{index=11}
# # 关闭无用模块以减少启动慢与内存开销
# # loadmodule /usr/lib/redis/redisearch.so