LNMP见我2023-04-17 10:51:16 发布的企业网站架构部署与优化 LNMP
https://blog.csdn.net/Richard_Sniper/article/details/130158518?spm=1001.2014.3001.5501
1、安装 LNMP 各个组件
2、安装 redis 服务
3、安装 redis 扩展
官网:http://redis.io/
下载包:
https://codeload.github.com/phpredis/phpredis/zip/develop
http://download.redis.io/releases/redis-3.2.5.tar.gz
tar -zxvf redis-4.0.2.tgz
cd /opt/redis-4.0.2
//运行 phpize 脚本的用途是动态安装 php 扩展模块
/usr/local/php/bin/phpize
//开始编译安装
./configure --with-php-config=/usr/local/php/bin/php-config --enable-redis
make && make install
//Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/
ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/
redis.so
4、修改 php 配置文件
vim /usr/local/php/lib/php.ini
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20160303/" #736行,指定redis模块目录
extension = "redis.so" #738行,修改参数项名称为extension,指定redis模块名称
systemctl restart php-fpm.service
php -m | grep redis
5、测试连接
cd /usr/local/nginx/html/
vim set.php
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test',"1111111111");
echo $redis->get('test');
?>
curl http://127.0.0.1/set.php
vim get.php
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$result = $redis->get('test');
var_dump($result);
?>
curl http://127.0.0.1/get.php
//结果:string(10) "1111111111"
ps: 执行上述php需要注释掉/usr/local/redis/conf/redis.conf中1037-1038行密码,否则会报错
或者修改代码为:
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth('123123');
$redis->set('test',"1111111111");
echo $redis->get('test');
?>
添加$redis->auth('密码'); 行 即可