php無(wú)法連接memcache的解決辦法:首先檢查“/var/log/messages”;然后以root身份運(yùn)行“[liang@www ~]$ sudo /usr/sbin/setsebool -P…”命令即可。
本文操作環(huán)境:CentOS 5.4 i386系統(tǒng)、PHP7.1版,DELL G3電腦
php無(wú)法連接memcached
安裝配置好memcached后,telnet可以連接上,但php怎么也連接不上。memadmin也提示無(wú)法連接到服務(wù)器。最后終于在網(wǎng)上找到解決辦法。
********************
SELinux 導(dǎo)致 PHP 無(wú)法使用 fsockopen 連接到 Memcached 服務(wù)器
事情是這樣的:
首先是服務(wù)器硬盤(pán)出問(wèn)題了:-(,我給換了塊硬盤(pán),然后重裝系統(tǒng)(CentOS 5.4 i386),然后安裝各種程序、還原各種數(shù)據(jù)。最后一步是使用 memcache.php 來(lái) 監(jiān)控 Memcache 狀態(tài)。然而卻發(fā)現(xiàn)該工具無(wú)法連接上 Memcached 服務(wù)器。經(jīng)檢查,Memcached 服務(wù)器已經(jīng)正常啟動(dòng),使用 telnet 能夠正常連接上去,使用 Memcached 的應(yīng)用程序(PHP程序)也正常工作。查看 memcache.php 代碼發(fā)現(xiàn)其是使用 fsockopen 來(lái)連接 Memcached 服務(wù)器,遂懷疑 Socket 擴(kuò)展的問(wèn)題。然而,檢查發(fā)現(xiàn)可以在命令行中使用 fsockopen 連接到任意地址的任意端口,說(shuō)明 Socket 擴(kuò)展沒(méi)問(wèn)題。但在 httpd 中使用 fsockopen 來(lái)就只能連接本機(jī)的 80、8080、443 端口,連接其他端口均失敗。
檢查 httpd 的 log 也沒(méi)發(fā)現(xiàn)任何問(wèn)題。上網(wǎng)搜索也沒(méi)發(fā)現(xiàn)類(lèi)似問(wèn)題,郁悶ing……
于是又想到是否是 SELinux 的問(wèn)題。grep 了下 /var/log/audit/audit.log,發(fā)現(xiàn)以下線(xiàn)索:
[liang@www ~]$ sudo grep denied /var/log/audit/audit.log 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_socket
看來(lái)又是 SELinux 搞的鬼。繼續(xù)檢查,發(fā)現(xiàn) /var/log/messages 有以下錯(cuò)誤信息:
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 引起的問(wèn)題。根據(jù)提示繼續(xù)檢查:
[liang@www ~]$ sealert -l 23d1381f-9d4b-439a-9ad6-d52f1025f247 Summary: SELinux is preventing the http daemon from connecting to the itself or the relay ports Detailed 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 ]
———————省略若干輸出———————
錯(cuò)誤信息說(shuō)得很明了了:SELinux 阻止了 httpd 的連接。修改方式也給出來(lái)了,以 root 身份運(yùn)行以下命令即可:
[liang@www ~]$ sudo /usr/sbin/setsebool -P httpd_can_network_relay=1
注意該命令成功運(yùn)行后沒(méi)有任何輸出。要檢查是否設(shè)置成功,可以查看運(yùn)行 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
設(shè)置成功了。重新刷新下 memcache.php, 發(fā)現(xiàn)已經(jīng)能夠正常工作了。job done!
此文純粹是工作備忘。但希望也能給碰到同樣問(wèn)題的朋友一點(diǎn)幫助。
推薦學(xué)習(xí):《PHP視頻教程》