CentOS7下PHP-FPM 503错误:套接字存在但权限修改无效求助
Hey Pavel, let's work through this frustrating socket issue you're facing. I've debugged dozens of these on CentOS 7, so let's break down the most likely fixes step by step:
1. Stop manually setting socket permissions (they get overwritten!)
When you restart PHP-FPM, it recreates the socket file from scratch—so any manual chmod or chown changes get wiped out. You need to set the permissions directly in your PHP-FPM www.conf:
- Open your
www.conffile and add/locate these three lines:listen.owner = apache listen.group = apache listen.mode = 0660 - This ensures every time PHP-FPM starts, it creates the socket with the exact permissions Apache needs (no need for risky
777permissions).
2. Kill stale PHP-FPM processes
The "Socket Exists" error often pops up when a crashed PHP-FPM process is still holding onto the socket. Let's clean that up:
- List all running PHP-FPM processes:
ps aux | grep php-fpm - Kill any orphaned processes (look for entries that don't match your current running service):
kill -9 <PID> - Restart PHP-FPM properly:
systemctl restart php56-fpm
3. Verify Apache's ProxyPassMatch configuration
Double-check that your Apache virtual host is pointing to the exact same socket path as PHP-FPM:
- In your
httpd.confvirtual host block, confirm theProxyPassMatchline looks like this (adjust the document root to match your setup):ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/tmp/php56-fpm.sock|fcgi://localhost/var/www/your-document-root - Typos here (like missing
56in the socket name) are a super common culprit.
4. Check SELinux (the silent CentOS 7 troublemaker)
CentOS 7 enables SELinux by default, and it often blocks Apache from accessing PHP-FPM sockets even if file permissions are correct:
- Test if SELinux is the issue by temporarily disabling it:
setenforce 0 - If the error goes away, don't leave SELinux disabled—fix it properly with these commands:
# Allow Apache to connect to FPM sockets setsebool -P httpd_can_network_connect 1 # Apply correct SELinux context to your socket semanage fcontext -a -t httpd_sys_rw_content_t "/tmp/php56-fpm.sock" restorecon -v /tmp/php56-fpm.sock
5. If all else fails: Move the socket out of /tmp
The /tmp directory has special cleanup rules on CentOS 7 that can sometimes delete or mess with sockets. Try moving it to a dedicated directory:
- Create a directory for PHP-FPM sockets:
mkdir -p /var/run/php-fpm chown apache:apache /var/run/php-fpm - Update
www.confto use the new path:listen = /var/run/php-fpm/php56-fpm.sock - Update your Apache
ProxyPassMatchline to match the new path, then restart both Apache and PHP-FPM.
内容的提问来源于stack exchange,提问作者Pavel Novák

