安裝配置好memcached後,telnet能夠鏈接上,但php怎麼也鏈接不上。memadmin也提示沒法鏈接到服務器。最後終於在網上找到解決辦法。php
********************web
SELinux 致使 PHP 沒法使用 fsockopen 鏈接到 Memcached 服務器服務器
事情是這樣的:socket
首先是服務器硬盤出問題了:-(,我給換了塊硬盤,而後重裝系統(CentOS 5.4 i386),而後安裝各類程序、還原各類數據。最後一步是使用 memcache.php 來 監控 Memcache 狀態。然而卻發現該工具沒法鏈接上 Memcached 服務器。經檢查,Memcached 服務器已經正常啓動,使用 telnet 可以正常鏈接上去,使用 Memcached 的應用程序(PHP程序)也正常工做。查看 memcache.php 代碼發現其是使用 fsockopen 來鏈接 Memcached 服務器,遂懷疑 Socket 擴展的問題。然而,檢查發現能夠在命令行中使用 fsockopen 鏈接到任意地址的任意端口,說明 Socket 擴展沒問題。但在 httpd 中使用 fsockopen 來就只能鏈接本機的 80、8080、443 端口,鏈接其餘端口均失敗。tcp
檢查 httpd 的 log 也沒發現任何問題。上網搜索也沒發現相似問題,鬱悶ing……memcached
因而又想到是不是 SELinux 的問題。grep 了下 /var/log/audit/audit.log,發現如下線索:工具
[liang@www ~]$ sudo grep denied /var/log/audit/audit.logthis
type=AVC msg=audit(1280882021.681:780): avc: denied { name_connect } for pid=3822 comm="httpd" dest=11211 scontext=user_u:system_r:httpd_t:s0 tcontext=system_u:object_r:http_cache_port_t:s0 tclass=tcp_socket
type=AVC msg=audit(1280885410.800:805): avc: denied { name_connect } for pid=3790 comm="httpd" dest=11211 scontext=user_u:system_r:httpd_t:s0 tcontext=system_u:object_r:http_cache_port_t:s0 tclass=tcp_socketspa
看來又是 SELinux 搞的鬼。繼續檢查,發現 /var/log/messages 有如下錯誤信息:.net
Aug 4 08:11:59 www setroubleshoot: SELinux is preventing the http daemon from connecting to the itself or the relay ports For complete SELinux messages. run sealert -l 23d1381f-9d4b-439a-9ad6-d52f1025f247
果真是 SELinux 引發的問題。根據提示繼續檢查:
[liang@www ~]$ sealert -l 23d1381f-9d4b-439a-9ad6-d52f1025f247
Summary:
SELinux is preventing the http daemon from connecting to the itself or the relay
portsDetailed Description:
SELinux has denied the http daemon from connecting to itself or the relay ports.
An httpd script is trying to do a network connect to an http/ftp port. If you
did not setup httpd to network connections, this could signal a intrusion
attempt.Allowing Access:
If you want httpd to connect to httpd/ftp ports you need to turn on the
httpd_can_network_relay boolean: "setsebool -P httpd_can_network_relay=1"The following command will allow this access:
setsebool -P httpd_can_network_relay=1
Additional Information:
Source Context user_u:system_r:httpd_t
Target Context system_u:object_r:http_cache_port_t
Target Objects None [ tcp_socket ]———————省略若干輸出———————
錯誤信息說得很明瞭了:SELinux 阻止了 httpd 的鏈接。修改方式也給出來了,以 root 身份運行如下命令便可:
[liang@www ~]$ sudo /usr/sbin/setsebool -P httpd_can_network_relay=1
注意該命令成功運行後沒有任何輸出。要檢查是否設置成功,能夠查看運行 getsebool 命令或者直接查看 log:
[liang@www ~]$ /usr/sbin/getsebool httpd_can_network_relay
httpd_can_network_relay –> on
[liang@www ~]$ sudo tail /var/log/messages
Aug 4 10:50:23 www setsebool: The httpd_can_network_relay policy boolean was changed to 1 by root
設置成功了。從新刷新下 memcache.php, 發現已經可以正常工做了。job done!
此文純粹是工做備忘。但但願也能給碰到一樣問題的朋友一點幫助。