首頁  >  文章  >  後端開發  >  php無法連接memcache怎麼辦

php無法連接memcache怎麼辦

藏色散人
藏色散人原創
2021-06-19 09:30:272051瀏覽

php無法連接memcache的解決方法:先檢查「/var/log/messages」;然後以root身分執行「[liang@www ~]$ sudo /usr/sbin/setsebool -P... ”命令即可。

php無法連接memcache怎麼辦

本文操作環境:CentOS 5.4 i386系統、PHP7.1版,DELL G3電腦

php無法連接memcached

安裝配置好memcached後,telnet可以連接上,但php怎麼也連接不上。 memadmin也提示無法連線到伺服器。最後終於在網路上找到解決方法。

********************

SELinux 導致PHP 無法使用fsockopen 連接到Memcached 伺服器

事情是這樣的:

首先是伺服器硬碟出問題了:-(,我給換了區塊硬碟,然後重裝系統(CentOS 5.4 i386),然後安裝各種程式、還原各種資料。最後一步是使用memcache.php 來監控Memcache 狀態。然而卻發現該工具無法連接上Memcached 伺服器。經檢查,Memcached 伺服器已經正常啟動,使用telnet 能夠正常連接上去,使用Memcached 的應用程式(PHP程式)也正常工作。查看memcache.php 程式碼發現其是使用fsockopen 來連接Memcached 伺服器,遂懷疑Socket 擴展的問題。然而,檢查發現可以在命令行中使用fsockopen 連接到任意地址的任意端口,說明Socket 擴展沒問題。但在httpd中使用fsockopen 來就只能連接本機的80、8080、443 端口,連接其他端口均失敗。

檢查httpd 的log 也沒發現任何問題。上網搜索也沒發現類似問題,鬱悶ing ...

於是又想到是否是SELinux 的問題。grep 了下/var/log/audit/audit.log,發現以下線索:

 

[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

看來又是SELinux 搞的鬼。繼續檢查,發現/var/log/messages 有以下錯誤訊息:

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
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 ]

———————省略若干輸出———————

錯誤訊息說得很明了: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!

此文純粹是工作備忘。但希望也能給碰到同樣問題的朋友一點幫助。

推薦學習:《PHP影片教學

以上是php無法連接memcache怎麼辦的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn