CentOS+Apache+php无法访问redis的解决方法
PHP 使用 Redis
安装
开始在 PHP 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 PHP redis 驱动,且你的机器上能正常使用 PHP。 接下来让我们安装 PHP redis 驱动:下载地址为:https://github.com/phpredis/phpredis/releases。
PHP安装redis扩展
以下操作需要在下载的 phpredis 目录中完成:
- $ wget https://github.com/phpredis/phpredis/archive/2.2.4.tar.gz
- $ cd phpredis-2.2.7 # 进入 phpredis 目录
- $ /usr/local/php/bin/phpize # php安装后的路径
- $ ./configure --with-php-config=/usr/local/php/bin/php-config
- $ make && make install
如果你是 PHP7 版本,则需要下载指定分支:
- git clone -b php7 https://github.com/phpredis/phpredis.git
修改php.ini文件
- vi /usr/local/php/lib/php.ini
增加如下内容:
- extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20090626"
- extension=redis.so
安装完成后重启php-fpm 或 apache。查看phpinfo信息,就能看到redis扩展。
连接到 redis 服务
- <?php
- //连接本地的 Redis 服务
- $redis = new Redis();
- $redis->connect('127.0.0.1', 6379);
- echo "Connection to server sucessfully";
- //查看服务是否运行
- echo "Server is running: " . $redis->ping();
- ?>
执行脚本,输出结果为:
- Connection to server sucessfully
- Server is running: PONG
Redis PHP String(字符串) 实例
- <?php
- //连接本地的 Redis 服务
- $redis = new Redis();
- $redis->connect('127.0.0.1', 6379);
- echo "Connection to server sucessfully";
- //设置 redis 字符串数据
- $redis->set("tutorial-name", "Redis tutorial");
- // 获取存储的数据并输出
- echo "Stored string in redis:: " . $redis->get("tutorial-name");
- ?>
执行脚本,输出结果为:
- Connection to server sucessfully
- Stored string in redis:: Redis tutorial
Redis PHP List(列表) 实例
- <?php
- //连接本地的 Redis 服务
- $redis = new Redis();
- $redis->connect('127.0.0.1', 6379);
- echo "Connection to server sucessfully";
- //存储数据到列表中
- $redis->lpush("tutorial-list", "Redis");
- $redis->lpush("tutorial-list", "Mongodb");
- $redis->lpush("tutorial-list", "Mysql");
- // 获取存储的数据并输出
- $arList = $redis->lrange("tutorial-list", 0 ,5);
- echo "Stored string in redis";
- print_r($arList);
- ?>
执行脚本,输出结果为:
- Connection to server sucessfully
- Stored string in redis
- Redis
- Mongodb
- Mysql
Redis PHP Keys 实例
- <?php
- //连接本地的 Redis 服务
- $redis = new Redis();
- $redis->connect('127.0.0.1', 6379);
- echo "Connection to server sucessfully";
- // 获取数据并输出
- $arList = $redis->keys("*");
- echo "Stored keys in redis:: ";
- print_r($arList);
- ?>
执行脚本,输出结果为:
- Connection to server sucessfully
- Stored string in redis::
- tutorial-name
- tutorial-list
问题处理:
在CentOS下配置Apache+php+redis+phpredis环境。
编辑访问redis缓存的php程序test.php,以应用程序方式在后台运行,可成功访问Redis,而在Apache下以网页形式访问时则出错,在
访问Redis以及之后的代码均不再执行。查看Apache的日志:/var/log/httpd/error_log,发现代码运行时出现异常:
PHP Fatal error: Uncaught exception ‘RedisException’ with message ‘Redis server went away’ in /var/www/html/test.php
在网上查该异常时均认为是php的Sokcet超时时间设置的过短,应该在代码前加上:ini_set(‘default_socket_timeout’, -1);
经测试仍无法解决该问题,因在代码后台运行正常,因此判断并非代码本身问题,而是Apache不允许访问网络资源,尝试如下解决方法:
打开/etc/selinux/config,找到其中的:
SELINUX=enforcing
改为:SELINUX=disabled
问题解决!
如果仍不能解决问题,可执行如下指令:
/usr/sbin/setsebool httpd_can_network_connect=1
CentOS+Apache+php无法访问redis的解决方法的更多相关文章
- CentOS+Apache+php无法访问redis的解决方法 Redis server went away
在CentOS下配置Apache+php+redis+phpredis环境.编辑访问redis缓存的php程序test.php,以应用程序方式在后台运行,可成功访问Redis,而在Apache下以网页 ...
- C#操作FTP报错,远程服务器返回错误:(550)文件不可用(例如,未找到文件,无法访问文件)的解决方法
最近在做项目的时候需要操作ftp进行文件的上传下载,但在调用using (var response = (FtpWebResponse)FtpWebRequest.GetResponse())的时候总 ...
- centos vsftpd 553 Could not create file解决方法
centos vsftpd 553 Could not create file解决方法 问题由于selinux引起的,问题解决办法: www.2cto.com 输入:getsebool - ...
- ASP.NET对路径"C:/......."的访问被拒绝 解决方法小结 [转载]
问题: 异常详细信息: System.UnauthorizedAccessException: 对路径“C:/Supermarket/output.pdf”的访问被拒绝. 解决方法: 一.在IIS中的 ...
- 使用IDEA操作Hbase API 报错:org.apache.hadoop.hbase.client.RetriesExhaustedException的解决方法:
使用IDEA操作Hbase API 报错:org.apache.hadoop.hbase.client.RetriesExhaustedException的解决方法: 1.错误详情: Excepti ...
- apache外网不能访问分析与解决方法
apache安装好以后,在本机可以用:http://localhost 或者 http://127.0.0.1进行访问,但是,在外网(相对本机来说的,局域网也算)不能访问. 这种情况可以分为两个问题, ...
- 关于出现 org.apache.commons.lang.exception.NestableRuntimeException的解决方法
最近做服务端和客户端之间的访问,出现了 org.apache.commons.lang.exception.NestableRuntimeException等状况.实在令人头大,翻到了一个很好的帖子说 ...
- 安装centos后无法引导启动windows7的解决方法
在电脑Windows7系统上安装Centos7,安装后找不到Windows7引导菜单. 原因:因为CentOS 7已采用新式的grub2系统,所以需要进入/boot/grub2目录后使用vi编辑gru ...
- "System.Security.Cryptography.CryptographicException: 拒绝访问" 问题的解决方法
.net web程序使用rsa算法进行加解密时,程序报告“System.Security.Cryptography.CryptographicException: 拒绝访问”错.按网上搜的解决方法做了 ...
随机推荐
- MongoDB:The Definitive Guide CHAPTER 1 Introduction
MongoDB is a powerful, flexible, and scalable data store. It combines the ability to scale out with ...
- hdoj 4006 The kth great number【优先队列】
The kth great number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- 8-11-Exercise
链接:第四次小练 A.POJ 3094 Quicksum 水题中的水题啊~ 直接上代码: #include <iostream> #include <cstdio> #in ...
- js select级联,上面分类,下面是内容
js select级联,上面分类,下面是内容. js级联效果如下: 分类: 请选择 水果 蔬菜 其他 内容: // html和js代码如下: <html> <hea ...
- 如何判断Android系统的版本
随着Android版本的增多,在不同的版本中使用不同的设计是必须的,根据程序运行的版本来提供不同的功能.这涉及到如何在程序中判断Android系统的版本. 在Android api中的android. ...
- 和菜鸟一起学linux内核源码之基础准备篇 系列 体系结构图
http://blog.csdn.net/eastmoon502136/article/details/8711104
- dbartisan下载地址
http://downloads.embarcadero.com/free/dbartisan
- RedHat7搭建MongoDB
yum安装MongoDB 添加MongoDB源# vi /etc/yum.repos.d/mongodb-org-3.0.repo [mongodb-org-3.0] name=MongoDB Rep ...
- 各种vpn协议介绍(重点介绍sslvpn的实现方式openvpn)
vpn介绍: VIrtual Private Network 虚拟专用网络哪些用户会用vpn? 公司的远程用户(出差.家里),公司的分支机构.idc机房.企业间.FQ常见vpn协议有哪些? ...
- css制作导航栏的上下三角
1)先完成一个导航条 <style type="text/css"> .nav-ul{ list-style: none; } .nav-ul li{ width: 1 ...