在 Debian 上配置 PHP 使用 Redis,一般分三步:安装 Redis 服务 → 安装 PHP Redis 扩展 → 配置并测试。下面以 Debian 11/12 为例(PHP 7.4 / 8.x 通用)。
sudo apt update
sudo apt install redis-server -y
启动并设置开机自启:
sudo systemctl enable redis-server
sudo systemctl start redis-server
测试 Redis:
redis-cli ping
# 返回 PONG 说明正常
php -v
以 PHP 8.2 为例(根据你实际版本改):
sudo apt install php-redis -y
如果是 PHP 8.2:
sudo apt install php8.2-redis -y
✅ 如果是 PHP 8.3 / 自定义源,可用 pecl:
sudo apt install php-dev autoconf make gcc pkg-config
sudo pecl install redis
然后在 php.ini 加:
extension=redis
sudo systemctl restart apache2
sudo systemctl restart php8.2-fpm
sudo systemctl restart nginx
新建测试文件 test_redis.php:
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test', 'hello redis');
echo $redis->get('test');
浏览器访问或命令行:
php test_redis.php
输出:
hello redis
redis-server 已启动php -m | grep redis
requirepass如果你用的是 Docker / LNMP / 宝塔 / 特定 PHP 版本,告诉我,我可以给你更精确的配置。