您的位置:首页 > 数据库 > Redis

Fatal error: Uncaught exception 'RedisException' with message 'Redis server went away'

2017-07-17 15:57 621 查看
运行PHP Redis中的函数的时候出现如下错误:

php Fatal error:  Uncaught exception 'RedisException' with message 'redis server went away' in /var/www/html/index.php:5


原因:

系统中selinux的问题,selinux禁止了httpd对外访问的权限,打开即可

[root@localhost redis]# getsebool  -a|grep -i httpd_can_network_connect
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off


解决方法:

# setsebool -P httpd_can_network_connect=1


或者:

#/usr/sbin/setsebool -P httpd_can_network_connect=1


或者直接关闭selinux

# setenforce 0


补充:

setsebool命令

配置VSFTPD时本地用户无法切换

用到命令

setsebool -P allow_ftpd_anon_write=1 allow_ftpd_full_access=1 ftp_home_dir=1 ftpd_disable_trans=1 ftpd_is_daemon=1


setsebool命令查询了相关的资料,这是SELinux的设置命令.

其实 man 一下setsebool也知道了:

setsebool - set SELinux boolean value

SYNOPSIS
setsebool [ -PNV ] boolean value | bool1=val1 bool2=val2 ...


Redhat从FC3开始,就使用了SELinux来增强安全,但是使用起来比较繁琐,就想关闭它,但是如果安装服务器的时候开始没有图形界面,还真很难找到地方可以关闭。

正确方法如下:修改/etc/selinux/config文件中的SELINUX=“” 为 disabled ,然后重启。

下面是一些 收集来的 设置命令

===ftp===
//If you want to share files anonymously
chcon -R -t public_content_t /var/ftp
//If you want to setup a directory where you can upload files
chcon -t public_content_rw_t /var/ftp/incoming
You must also turn on the boolean allow_ftpd_anon_write
setsebool -P allow_ftpd_anon_write=1
//If you are setting up this machine as a ftpd server and wish to allow users to access their home directorories
setsebool -P ftp_home_dir 1
//If you want to run ftpd as a daemon
setsebool -P ftpd_is_daemon 1
//You can disable SELinux protection for the ftpd daemon
setsebool -P ftpd_disable_trans 1
===httpd===
//If you want a particular domain to write to the public_content_rw_t domain
setsebool -P allow_httpd_anon_write=1
or
setsebool -P allow_httpd_sys__anon_write=1
//httpd can be setup to allow cgi s to be executed
setsebool -P httpd_enable_cgi 1
//If you want to allow access to users home directories
setsebool -P httpd_enable_homedirs 1
chcon -R -t httpd_sys_content_t ~user/public_html
//httpd is allowed access to the controling terminal
setsebool -P httpd_tty_comm 1
//such that one httpd service can not interfere with another
setsebool -P httpd_unified 0
//loadable modules run under the same context as httpd
setsebool -P httpd_builtin_ing 0
//httpd s are allowed to connect out to the network
setsebool -P httpd_can_network_connect 1
// You can disable suexec transition
setsebool -P httpd_suexec_disable_trans 1
//You can disable SELinux protection for the httpd daemon by executing
setsebool -P httpd_disable_trans 1
service httpd restart
===named===
//If you want to have named update the master zone files
setsebool -P named_write_master_zones 1
//You can disable SELinux protection for the named daemon by executing
setsebool -P named_disable_trans 1
service named restart
===nfs===
//If you want to setup this machine to share nfs partitions read only
setsebool -P nfs_export_all_ro 1
//If you want to share files read/write
setsebool -P nfs_export_all_rw 1
//If you want to use a remote NFS server for the home directories on this machine
setsebool -P use_nfs_home_dirs 1
===samba===
//If you want to share files other than home directorie
chcon -t samba_share_t /directory
//If you want to share files with multiple domains
setsebool -P allow_smbd_anon_write=1
//If you are setting up this machine as a Samba server and wish to share the home directories
setsebool -P samba_enable_home_dirs 1
//If you want to use a remote Samba server for the home directories on this machine
setsebool -P use_samba_home_dirs 1
//You can disable SELinux protection for the samba daemon by executing
setsebool -P smbd_disable_trans 1
service smb restart
===rsync===
//If you want to share files using the rsync daemon
chcon -t public_content_t /directories
//If you want to share files with multiple domains
setsebool -P allow_rsync_anon_write=1
//You can disable SELinux protection for the rsync daemon by executing
setsebool -P rsync_disable_trans 1
===kerberos===
//allow your system to work properly in a Kerberos environment
setsebool -P allow_kerberos 1
//If you are running Kerberos daemons kadmind or krb5kdc
setsebool -P krb5kdc_disable_trans 1
service krb5kdc restart
setsebool -P kadmind_disable_trans 1
service kadmind restart
===nis===
Allow your system to work properly in a NIS environment
setsebool -P allow_ypbind 1


1.1 SElinux概述

SELinux(Security-Enhanced Linux) 是美国国家安全局(NAS)对于强制访问控制的实现,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。大部分使用 SELinux 的人使用的都是SELinux就绪的发行版,例如 Fedora、Red Hat Enterprise Linux (RHEL)、Debian 或 Gentoo。它们都是在内核中启用 SELinux 的,并且提供一个可定制的安全策略,还提供很多用户层的库和工具,它们都可以使用 SELinux 的功能。

1.1.1 SElinux特点

1.MAC

对访问的控制彻底化,对所有的文件、目录、端口的访问都是基于策略设定的,可由管理员时行设定。

2.RBAC

对于用户只赋予最小权限。用户被划分成了一些role(角色),即使是root用户,如果不具有sysadm_r角色的话,也不是执行相关的管理。哪里role可以执行哪些domain,也是可以修改的。

3.安全上下文

当启动selinux的时候,所有文件与对象都有安全上下文。进程的安全上下文是域,安全上下文由用户:角色:类型表示。

(1)系统根据pam子系统中的pam_selinux.so模块设定登录者运行程序的安全上下文

(2)rpm包安装会根据rpm包内记录来生成安全上下文,

(3)如果是手工他建的,会根据policy中规定来设置安全上下文,

(4)如果是cp,会重新生成安全上下文。

(5)如果是mv,安全上下文不变。

1.1.2 安全上下文格式

安全上下文由user:role:type三部分组成,下面分别说明其作用:

1.user identity:类似linux系统中的UID,提供身份识别,安全上下文中的一部分。

三种常见的user:

user_u-: 普通用户登录系统后预设;

system_u-:开机过程中系统进程的预设;

root-: root登录后预设;

在targeted policy中users不是很重要;

在strict policy中比较重要,的有预设的selinux users都以 “_u”结尾,root除外。

2.role

文件与目录的role,通常是object_r;

程序的role,通常是system_r;

用户的role,targeted policy为system_r;

strict policy为sysadm_r,staff_r,user_r

用户的role,类似于系统中的GID,不同的角色具备不同的权限;用户可以具备多个role;但是同一时间内只能使用一role;

role是RBAC的基础;

3.type

type:用来将主体与客体划分为不同的组,组每个主体和系统中的客体定义了一个类型;为进程运行提供最低的权限环境。

当一个类型与执行的进程关联时,该type也称为domain,也叫安全上下文。

域或安全上下文是一个进程允许操作的列表,决字一个进程可以对哪种类型进行操作。

1.1.3 SElinux配置文件

vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - SELinux is fully disabled.
SELINUX=enforcing
#SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
# targeted - Only targeted network daemons are protected.
# strict - Full SELinux protection.
SELINUXTYPE=targeted
#SELINUX有「disabled」「permissive」,「enforcing」3种选择。


1.模式的设置

enforcing:强制模式,只要selinux不允许,就无法执行

permissive:警告模式,将该事件记录下来,依然允许执行

disabled:关闭selinux;停用,启用需要重启计算机。

2.策略的设置

targeted:保护常见的网络服务,是selinux的默认值;

stric:提供RBAC的policy,具备完整的保护功能,保护网络服务,一般指令及应用程序。

策略改变后,需要重新启动计算机。

也可以通过命令来修改相关的具体的策略值,也就是修改安全上下文,来提高策略的灵活性。

3.策略的位置

/etc/selinux/<策略名>/policy/

1.2 SElinux命令介绍

1.2.1 查询SElinux状态命令

1.查询selinux状态

[root@redhat ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: enforcing
Mode from config file: enforcing
Policy version: 21
Policy from config file: targeted


2.查询selinux激活状态

[root@redhat ~]# selinuxenabled

[root@redhat ~]# echo $?

0

如果为-256为非激活状态。

1.2.2 切换SElinux类型

1.切换成警告模式

[root@redhat ~]# setenforce 0或setenforce permissive
[root@redhat ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: permissive
Mode from config file: enforcing
Policy version: 21
Policy from config file: targeted
或
[root@redhat ~]# getenforce
Permissive


2.切换成强制模式

[root@redhat ~]# setenforce 1
[root@redhat ~]# getenforce
Enforcing


注:使用setenforce切换enforcing与permissive模式不需要重启计算机。

1.2.3 检查安全上下文

1.检查帐号的安全上下文

[root@redhat ~]# id -Z

root:system_r:unconfined_t:SystemLow-SystemHigh

2.检查进程的安全上下文

[root@redhat home]# ps -Z
LABEL PID TTY TIME CMD
root:system_r:unconfined_t:SystemLow-SystemHigh 2383 pts/0 00:00:00 bash
root:system_r:unconfined_t:SystemLow-SystemHigh 2536 pts/0 00:00:00 ps


3.检查文件与目录的安全上下文

[root@redhat home]# ls -Z

drwx—— tom tom system_u:object_r:user_home_dir_t tom

1.2.4 修改文件/目录安全上下文与策略

1.chcon命令

chcon -u [user] 对象
-r [role]
-t [type]
-R 递归
示例:
chcon -R -t samba_share_t /tmp/abc


注:安全上下文的简单理解说明,受到selinux保护的进程只能访问标识为自己只够访问的安全上下文的文件与目录。

例如:上面解释为使用smb进程能够访问/tmp/abc目录而设定的安全上下文。

2.getsebool命令

获取本机selinux策略值,也称为bool值。

getsebool -a 命令同sestatus -b

[root@redhat files]# getsebool -a
NetworkManager_disable_trans --> off
allow_cvs_read_shadow --> off
allow_daemons_dump_core --> on
allow_daemons_use_tty --> off
allow_execheap --> off
allow_execmem --> on
allow_execmod --> off
allow_execstack --> on
allow_ftpd_anon_write --> off /*是否允许ftp匿名访问*/
allow_ftpd_full_access --> off
...
httpd_disable_trans --> off /*只要有disable_trans关闭保护*/


说明:selinux的设置一般通过两个部分完成的,一个是安全上下文,另一个是策略,策略值是对安全上下文的补充。

3.setsebool命令

setsebool -P allow_ftpd_anon_write=1

-P 是永久性设置,否则重启之后又恢复预设值。

示例:

[root@redhat files]# setsebool -P allow_ftpd_anon_write=1
[root@redhat files]# getsebool allow_ftpd_anon_write
allow_ftpd_anon_write --> on


说明:如果仅仅是安全上下文中设置了vsftpd进程对某一个目录的访问,配置文件中也允许可写,但是selinux中策略中不允许可写,仍然不可写。所以基于selinux保护的服务中,安全性要高于很多。

1.3 SElinux应用

selinux的设置分为两个部分,修改安全上下文以及策略,下面收集了一些应用的安全上下文,供配置时使用,对于策略的设置,应根据服务应用的特点来修改相应的策略值。

1.3.1 SElinux与samba

1.samba共享的文件必须用正确的selinux安全上下文标记。

chcon -R -t samba_share_t /tmp/abc

如果共享/home/abc,需要设置整个主目录的安全上下文。

chcon -R -r samba_share_t /home

2.修改策略(只对主目录的策略的修改)

setsebool -P samba_enable_home_dirs=1
setsebool -P allow_smbd_anon_write=1

getsebool 查看
samba_enable_home_dirs -->on
allow_smbd_anon_write --> on /*允许匿名访问并且可写*/


1.3.2 SElinux与nfs

selinux对nfs的限制好像不是很严格,默认状态下,不对nfs的安全上下文进行标记,而且在默认状态的策略下,nfs的目标策略允许nfs_export_all_ro

nfs_export_all_ro

nfs_export_all_rw值为0

所以说默认是允许访问的。

但是如果共享的是/home/abc的话,需要打开相关策略对home的访问。

setsebool -P use_nfs_home_dirs boolean 1
getsebool use_nfs_home_dirs


1.3.3 SElinux与ftp

1.如果ftp为匿名用户共享目录的话,应修改安全上下文。

chcon -R -t public_content_t /var/ftp

chcon -R -t public_content_rw_t /var/ftp/incoming

2.策略的设置

setsebool -P allow_ftpd_anon_write =1
getsebool allow_ftpd_anon_write
allow_ftpd_anon_write--> on


1.3.4 SElinux与http

apache的主目录如果修改为其它位置,selinux就会限制客户的访问。

1.修改安全上下文:

chcon -R -t httpd_sys_content_t /home/html

由于网页都需要进行匿名访问,所以要允许匿名访问。

2.修改策略:

setsebool -P allow_ftpd_anon_write = 1
setsebool -P allow_httpd_anon_write = 1
setsebool -P allow_<协议名>_anon_write = 1
关闭selinux对httpd的保护
httpd_disable_trans=0


1.3.5 SElinux与公共目录共享

如果ftp,samba,web都访问共享目录的话,该文件的安全上下文应为:

public_content_t
public_content_rw_t


其它各服务的策略的bool值,应根据具体情况做相应的修改。

1.3.6 SElinux配置总结

以上内容的selinux的配置实验还需要进行相关验证,以便在实际环境中能够直接应用,相关的内容还需要继续补充。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  redis server
相关文章推荐