php Fatal error: Uncaught exception 'RedisExcepti

<p>运行PHP Redis中的函数的时候出现如下错误:</p><p>php Fatal error: Uncaught exception &#39;RedisException&#39; with message &#39;redis server went away&#39; in /var/www/html/index.php:5</p><p>原因:</p><p>系统中selinux的问题,selinux禁止了httpd对外访问的权限,打开即可</p><p>[root@localhost redis]# getsebool -a|grep -i httpd_can_network_connect</p><p>httpd_can_network_connect --&gt; off</p><p>httpd_can_network_connect_cobbler --&gt; off</p><p>httpd_can_network_connect_db --&gt; off</p><p>解决方法:</p><p># setsebool -P httpd_can_network_connect=1</p><p>或者:</p><p>#/usr/sbin/setsebool -P httpd_can_network_connect=1</p><p>或者直接关闭selinux</p><p># setenforce 0</p><p>补充:</p><p>setsebool命令</p><p>配置VSFTPD时本地用户无法切换</p><p>用到命令</p><p>setsebool -P allow_ftpd_anon_write=1 allow_ftpd_full_access=1 ftp_home_dir=1 ftpd_disable_trans=1 ftpd_is_daemon=1</p><p>setsebool命令查询了相关的资料,这是SELinux的设置命令.</p><p>其实 man 一下setsebool也知道了:</p><p>setsebool - set SELinux boolean value</p><p>SYNOPSIS</p><p> setsebool [ -PNV ] boolean value | bool1=val1 bool2=val2 ...</p><p>Redhat从FC3开始,就使用了SELinux来增强安全,但是使用起来比较繁琐,就想关闭它,但是如果安装服务器的时候开始没有图形界面,还真很难找到地方可以关闭。</p><p>正确方法如下:修改/etc/selinux/config文件中的SELINUX=&quot;&quot; 为 disabled ,然后重启。</p><p>下面是一些 收集来的 设置命令</p><p>===ftp===</p><p>//If you want to share files anonymously</p><p>chcon -R -t public_content_t /var/ftp</p><p>//If you want to setup a directory where you can upload files</p><p>chcon -t public_content_rw_t /var/ftp/incoming</p><p>You must also turn on the boolean allow_ftpd_anon_write</p><p>setsebool -P allow_ftpd_anon_write=1</p><p>//If you are setting up this machine as a ftpd server and wish to allow users to access their home directorories</p><p>setsebool -P ftp_home_dir 1</p><p>//If you want to run ftpd as a daemon</p><p>setsebool -P ftpd_is_daemon 1</p><p>//You can disable SELinux protection for the ftpd daemon</p><p>setsebool -P ftpd_disable_trans 1</p><p>===httpd===</p><p>//If you want a particular domain to write to the public_content_rw_t domain</p><p>setsebool -P allow_httpd_anon_write=1</p><p>or</p><p>setsebool -P allow_httpd_sys__anon_write=1</p><p>//httpd can be setup to allow cgi s to be executed</p><p>setsebool -P httpd_enable_cgi 1</p><p>//If you want to allow access to users home directories</p><p>setsebool -P httpd_enable_homedirs 1</p><p>chcon -R -t httpd_sys_content_t ~user/public_html</p><p>//httpd is allowed access to the controling terminal</p><p>setsebool -P httpd_tty_comm 1</p><p>//such that one httpd service can not interfere with another</p><p>setsebool -P httpd_unified 0</p><p>//loadable modules run under the same context as httpd</p><p>setsebool -P httpd_builtin_ing 0</p><p>//httpd s are allowed to connect out to the network</p><p>setsebool -P httpd_can_network_connect 1</p><p>// You can disable suexec transition</p><p>setsebool -P httpd_suexec_disable_trans 1</p><p>//You can disable SELinux protection for the httpd daemon by executing</p><p>setsebool -P httpd_disable_trans 1</p><p>service httpd restart</p><p>===named===</p><p>//If you want to have named update the master zone files</p><p>setsebool -P named_write_master_zones 1</p><p>//You can disable SELinux protection for the named daemon by executing</p><p>setsebool -P named_disable_trans 1</p><p>service named restart</p><p>===nfs===</p><p>//If you want to setup this machine to share nfs partitions read only</p><p>setsebool -P nfs_export_all_ro 1</p><p>//If you want to share files read/write</p><p>setsebool -P nfs_export_all_rw 1</p><p>//If you want to use a remote NFS server for the home directories on this machine</p><p>setsebool -P use_nfs_home_dirs 1</p><p>===samba===</p><p>//If you want to share files other than home directorie</p><p>chcon -t samba_share_t /directory</p><p>//If you want to share files with multiple domains</p><p>setsebool -P allow_smbd_anon_write=1</p><p>//If you are setting up this machine as a Samba server and wish to share the home directories</p><p>setsebool -P samba_enable_home_dirs 1</p><p>//If you want to use a remote Samba server for the home directories on this machine</p><p>setsebool -P use_samba_home_dirs 1</p><p>//You can disable SELinux protection for the samba daemon by executing</p><p>setsebool -P smbd_disable_trans 1</p><p>service smb restart</p><p>===rsync===</p><p>//If you want to share files using the rsync daemon</p><p>chcon -t public_content_t /directories</p><p>//If you want to share files with multiple domains</p><p>setsebool -P allow_rsync_anon_write=1</p><p>//You can disable SELinux protection for the rsync daemon by executing</p><p>setsebool -P rsync_disable_trans 1</p><p>===kerberos===</p><p>//allow your system to work properly in a Kerberos environment</p><p>setsebool -P allow_kerberos 1</p><p>//If you are running Kerberos daemons kadmind or krb5kdc</p><p>setsebool -P krb5kdc_disable_trans 1</p><p>service krb5kdc restart</p><p>setsebool -P kadmind_disable_trans 1</p><p>service kadmind restart</p><p>===nis===</p><p>Allow your system to work properly in a NIS environment</p><p>setsebool -P allow_ypbind 1</p><p>1.1 SElinux概述</p><p>SELinux(Security-Enhanced Linux) 是美国国家安全局(NAS)对于强制访问控制的实现,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。大部分使用 SELinux 的人使用的都是SELinux就绪的发行版,例如 Fedora、Red Hat Enterprise Linux (RHEL)、Debian 或 Gentoo。它们都是在内核中启用 SELinux 的,并且提供一个可定制的安全策略,还提供很多用户层的库和工具,它们都可以使用 SELinux 的功能。</p><p>1.1.1 SElinux特点</p><p>1.MAC</p><p>对访问的控制彻底化,对所有的文件、目录、端口的访问都是基于策略设定的,可由管理员时行设定。</p><p>2.RBAC</p><p>对于用户只赋予最小权限。用户被划分成了一些role(角色),即使是root用户,如果不具有sysadm_r角色的话,也不是执行相关的管理。哪里role可以执行哪些domain,也是可以修改的。</p><p>3.安全上下文</p><p>当启动selinux的时候,所有文件与对象都有安全上下文。进程的安全上下文是域,安全上下文由用户:角色:类型表示。</p><p>(1)系统根据pam子系统中的pam_selinux.so模块设定登录者运行程序的安全上下文</p><p>(2)rpm包安装会根据rpm包内记录来生成安全上下文,</p><p>(3)如果是手工他建的,会根据policy中规定来设置安全上下文,</p><p>(4)如果是cp,会重新生成安全上下文。</p><p>(5)如果是mv,安全上下文不变。</p><p>1.1.2 安全上下文格式</p><p>安全上下文由user:role:type三部分组成,下面分别说明其作用:</p><p>1.user identity:类似linux系统中的UID,提供身份识别,安全上下文中的一部分。</p><p>三种常见的user:</p><p>user_u-: 普通用户登录系统后预设;</p><p>system_u-:开机过程中系统进程的预设;</p><p>root-: root登录后预设;</p><p>在targeted policy中users不是很重要;</p><p>在strict policy中比较重要,的有预设的selinux users都以 &quot;_u&quot;结尾,root除外。</p><p>2.role</p><p>文件与目录的role,通常是object_r;</p><p>程序的role,通常是system_r;</p><p>用户的role,targeted policy为system_r;</p><p>strict policy为sysadm_r,staff_r,user_r</p><p>用户的role,类似于系统中的GID,不同的角色具备不同的权限;用户可以具备多个role;但是同一时间内只能使用一role;</p><p>role是RBAC的基础;</p><p>3.type</p><p>type:用来将主体与客体划分为不同的组,组每个主体和系统中的客体定义了一个类型;为进程运行提供最低的权限环境。</p><p>当一个类型与执行的进程关联时,该type也称为domain,也叫安全上下文。</p><p>域或安全上下文是一个进程允许操作的列表,决字一个进程可以对哪种类型进行操作。</p><p>1.1.3 SElinux配置文件</p><p>vi /etc/selinux/config</p><p># This file controls the state of SELinux on the system.</p><p># SELINUX= can take one of these three values:</p><p># enforcing - SELinux security policy is enforced.</p><p># permissive - SELinux prints warnings instead of enforcing.</p><p># disabled - SELinux is fully disabled.</p><p>SELINUX=enforcing</p><p>#SELINUX=disabled</p><p># SELINUXTYPE= type of policy in use. Possible values are:</p><p># targeted - Only targeted network daemons are protected.</p><p># strict - Full SELinux protection.</p><p>SELINUXTYPE=targeted</p><p>#SELINUX有「disabled」「permissive」,「enforcing」3种选择。</p><p>1.模式的设置</p><p>enforcing:强制模式,只要selinux不允许,就无法执行</p><p>permissive:警告模式,将该事件记录下来,依然允许执行</p><p>disabled:关闭selinux;停用,启用需要重启计算机。</p><p>2.策略的设置</p><p>targeted:保护常见的网络服务,是selinux的默认值;</p><p>stric:提供RBAC的policy,具备完整的保护功能,保护网络服务,一般指令及应用程序。</p><p>策略改变后,需要重新启动计算机。</p><p>也可以通过命令来修改相关的具体的策略值,也就是修改安全上下文,来提高策略的灵活性。</p><p>3.策略的位置</p><p>/etc/selinux/&lt;策略名&gt;/policy/</p><p>1.2 SElinux命令介绍</p><p>1.2.1 查询SElinux状态命令</p><p>1.查询selinux状态</p><p>[root@redhat ~]# sestatus</p><p>SELinux status: enabled</p><p>SELinuxfs mount: /selinux</p><p>Current mode: enforcing</p><p>Mode from config file: enforcing</p><p>Policy version: 21</p><p>Policy from config file: targeted</p><p>2.查询selinux激活状态</p><p>[root@redhat ~]# selinuxenabled</p><p>[root@redhat ~]# echo $?</p><p>0</p><p>如果为-256为非激活状态。</p><p>1.2.2 切换SElinux类型</p><p>1.切换成警告模式</p><p>[root@redhat ~]# setenforce 0或setenforce permissive</p><p>[root@redhat ~]# sestatus</p><p>SELinux status: enabled</p><p>SELinuxfs mount: /selinux</p><p>Current mode: permissive</p><p>Mode from config file: enforcing</p><p>Policy version: 21</p><p>Policy from config file: targeted</p><p>或</p><p>[root@redhat ~]# getenforce</p><p>Permissive</p><p>2.切换成强制模式</p><p>[root@redhat ~]# setenforce 1</p><p>[root@redhat ~]# getenforce</p><p>Enforcing</p><p>注:使用setenforce切换enforcing与permissive模式不需要重启计算机。</p><p>1.2.3 检查安全上下文</p><p>1.检查帐号的安全上下文</p><p>[root@redhat ~]# id -Z</p><p>root:system_r:unconfined_t:SystemLow-SystemHigh</p><p>2.检查进程的安全上下文</p><p>[root@redhat home]# ps -Z</p><p>LABEL PID TTY TIME CMD</p><p>root:system_r:unconfined_t:SystemLow-SystemHigh 2383 pts/0 00:00:00 bash</p><p>root:system_r:unconfined_t:SystemLow-SystemHigh 2536 pts/0 00:00:00 ps</p><p>3.检查文件与目录的安全上下文</p><p>[root@redhat home]# ls -Z</p><p>drwx—— tom tom system_u:object_r:user_home_dir_t tom</p><p>1.2.4 修改文件/目录安全上下文与策略</p><p>1.chcon命令</p><p>chcon -u [user] 对象</p><p>-r [role]</p><p>-t [type]</p><p>-R 递归</p><p>示例:</p><p>chcon -R -t samba_share_t /tmp/abc</p><p>注:安全上下文的简单理解说明,受到selinux保护的进程只能访问标识为自己只够访问的安全上下文的文件与目录。</p><p>例如:上面解释为使用smb进程能够访问/tmp/abc目录而设定的安全上下文。</p><p>2.getsebool命令</p><p>获取本机selinux策略值,也称为bool值。</p><p>getsebool -a 命令同sestatus -b</p><p>[root@redhat files]# getsebool -a</p><p>NetworkManager_disable_trans --&gt; off</p><p>allow_cvs_read_shadow --&gt; off</p><p>allow_daemons_dump_core --&gt; on</p><p>allow_daemons_use_tty --&gt; off</p><p>allow_execheap --&gt; off</p><p>allow_execmem --&gt; on</p><p>allow_execmod --&gt; off</p><p>allow_execstack --&gt; on</p><p>allow_ftpd_anon_write --&gt; off /*是否允许ftp匿名访问*/</p><p>allow_ftpd_full_access --&gt; off</p><p>...</p><p>httpd_disable_trans --&gt; off /*只要有disable_trans关闭保护*/</p><p>说明:selinux的设置一般通过两个部分完成的,一个是安全上下文,另一个是策略,策略值是对安全上下文的补充。</p><p>3.setsebool命令</p><p>setsebool -P allow_ftpd_anon_write=1</p><p>-P 是永久性设置,否则重启之后又恢复预设值。</p><p>示例:</p><p>[root@redhat files]# setsebool -P allow_ftpd_anon_write=1</p><p>[root@redhat files]# getsebool allow_ftpd_anon_write</p><p>allow_ftpd_anon_write --&gt; on</p><p>说明:如果仅仅是安全上下文中设置了vsftpd进程对某一个目录的访问,配置文件中也允许可写,但是selinux中策略中不允许可写,仍然不可写。所以基于selinux保护的服务中,安全性要高于很多。</p><p>1.3 SElinux应用</p><p>selinux的设置分为两个部分,修改安全上下文以及策略,下面收集了一些应用的安全上下文,供配置时使用,对于策略的设置,应根据服务应用的特点来修改相应的策略值。</p><p>1.3.1 SElinux与samba</p><p>1.samba共享的文件必须用正确的selinux安全上下文标记。</p><p>chcon -R -t samba_share_t /tmp/abc</p><p>如果共享/home/abc,需要设置整个主目录的安全上下文。</p><p>chcon -R -r samba_share_t /home</p><p>2.修改策略(只对主目录的策略的修改)</p><p>setsebool -P samba_enable_home_dirs=1</p><p>setsebool -P allow_smbd_anon_write=1</p><p>getsebool 查看</p><p>samba_enable_home_dirs --&gt;on</p><p>allow_smbd_anon_write --&gt; on /*允许匿名访问并且可写*/</p><p>1.3.2 SElinux与nfs</p><p>selinux对nfs的限制好像不是很严格,默认状态下,不对nfs的安全上下文进行标记,而且在默认状态的策略下,nfs的目标策略允许nfs_export_all_ro</p><p>nfs_export_all_ro</p><p>nfs_export_all_rw值为0</p><p>所以说默认是允许访问的。</p><p>但是如果共享的是/home/abc的话,需要打开相关策略对home的访问。</p><p>setsebool -P use_nfs_home_dirs boolean 1</p><p>getsebool use_nfs_home_dirs</p><p>1.3.3 SElinux与ftp</p><p>1.如果ftp为匿名用户共享目录的话,应修改安全上下文。</p><p>chcon -R -t public_content_t /var/ftp</p><p>chcon -R -t public_content_rw_t /var/ftp/incoming</p><p>2.策略的设置</p><p>setsebool -P allow_ftpd_anon_write =1</p><p>getsebool allow_ftpd_anon_write</p><p>allow_ftpd_anon_write--&gt; on</p><p>1.3.4 SElinux与http</p><p>apache的主目录如果修改为其它位置,selinux就会限制客户的访问。</p><p>1.修改安全上下文:</p><p>chcon -R -t httpd_sys_content_t /home/html</p><p>由于网页都需要进行匿名访问,所以要允许匿名访问。</p><p>2.修改策略:</p><p>setsebool -P allow_ftpd_anon_write = 1</p><p>setsebool -P allow_httpd_anon_write = 1</p><p>setsebool -P allow_&lt;协议名&gt;_anon_write = 1</p><p>关闭selinux对httpd的保护</p><p>httpd_disable_trans=0</p><p>1.3.5 SElinux与公共目录共享</p><p>如果ftp,samba,web都访问共享目录的话,该文件的安全上下文应为:</p><p>public_content_t</p><p>public_content_rw_t</p><p>其它各服务的策略的bool值,应根据具体情况做相应的修改。</p><p>1.3.6 SElinux配置总结</p><p>以上内容的selinux的配置实验还需要进行相关验证,以便在实际环境中能够直接应用,相关的内容还需要继续补充</p>
RangeTime:0.050035s
RangeMem:219.6 KB
返回顶部 留言