What should I do if php cannot connect to memcache?
Solution to the problem that php cannot connect to memcache: first check "/var/log/messages"; then run "[liang@www ~]$ sudo /usr/sbin/setsebool -P... "Just give the order.
The operating environment of this article: CentOS 5.4 i386 system, PHP7.1 version, DELL G3 computer
php cannot connect to memcached
After installing and configuring memcached, telnet can connect, but php cannot connect. memadmin also prompts that it cannot connect to the server. Finally I found a solution online.
********************
SELinux causes PHP to be unable to use fsockopen to connect to the Memcached server
The thing is Like this:
First of all, there is a problem with the server hard disk:-(, I replaced the hard disk, then reinstalled the system (CentOS 5.4 i386), then installed various programs and restored various data. The last step Memcache.php is used to monitor Memcache status. However, it is found that the tool cannot connect to the Memcached server. After checking, the Memcached server has started normally, can be connected normally using telnet, and the application program (PHP program) using Memcached also works normally. Looking at the memcache.php code, we found that it uses fsockopen to connect to the Memcached server, so we suspected a problem with the Socket extension. However, we checked and found that we can use fsockopen on the command line to connect to any port at any address, indicating that there is no problem with the Socket extension. But in httpd When using fsockopen, I can only connect to the 80, 8080, and 443 ports of the local machine, and the connection to other ports fails.
I checked the httpd log and found no problems. I searched online and found no similar problems. I was depressed. ……
Then I thought about whether it was a SELinux problem. I grep /var/log/audit/audit.log and found the following clues:
[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
It seems that SELinux is causing the problem again. Continue to check and find that /var/log/messages has the following error message:
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
It is indeed a problem caused by SELinux. Continue to check according to the prompts:
[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 ]
————————Omit some output————————
The error message is very clear: SELinux blocks the httpd connection. The modification method is also given. Just run the following command as root:
[liang@www ~]$ sudo /usr/sbin/setsebool -P httpd_can_network_relay=1
Note that there is no output after the command is successfully run. To check whether the setting is successful, you can run the getsebool command or directly view the 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
Set successfully . Refresh memcache.php and find that it is working normally. Job done!
This article is purely a work note. But I hope it can also help friends who encounter the same problem.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What should I do if php cannot connect to memcache?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
