Unable to bind listening socket for address php-fpm
When deploying a LEMP stack on CentOS 7, encountering Error: unable to bind listening socket for address '/var/run/php-fpm.d/www.conf' is a classic configuration mismatch. The error message itself reveals the root cause immediately: the PHP-FPM daemon cannot locate the file specified in its listen directive.
In the provided www.conf, the configuration explicitly sets listen = /var/run/php-fpm.d/www.conf. This path points to a specific location where the socket file is expected to reside. However, by default, PHP-FPM expects its Unix sockets to be created directly inside /var/run/php-fpm/ or within the directory specified by your chosen pool configuration prefix, not inside a subdirectory named php-fpm.d.
The DigitalOcean tutorial you followed often assumes a specific file structure that might not match the default installation of PHP-FPM on CentOS. The standard behavior for PHP-FPM is to listen on a socket defined in the pool files located within /etc/php-fpm.d/ or similar, and to create the actual socket file at a path like /var/run/php-fpm/www.sock.
To resolve this, you must align your www.conf with where the socket actually gets created. The most straightforward fix is to change the listen directive in your www.conf to match the default socket location provided by the PHP-FPM package manager.
Update your /etc/php-fpm.d/www.conf (or wherever your pool file resides) as follows:
[www]
listen = /var/run/php-fpm/www.sock
listen.allowed_clients = 127.0.0.1
user = apache
group = apache
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 5
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
Once you have made this change, restart the service to apply the new configuration:
systemctl restart php-fpm
After restarting, verify that the socket file has been created at the correct path:
ls -l /var/run/php-fpm/www.sock
You should see output indicating a socket file exists and is owned by the apache user (or nobody, depending on your setup). If the socket still fails to appear, ensure that the directory /var/run/php-fpm/ exists and has the correct permissions:
mkdir -p /var/run/php-fpm
chown apache:apache /var/run/php-fpm
chmod 750 /var/run/php-fpm
Verifying Nginx Configuration
Your nginx.conf snippet correctly references the socket with fastcgi_pass unix:/var/run/php-fpm.sock;. However, there is a subtle but critical discrepancy here. The path in your Nginx config ends in .sock, while the corrected PHP-FPM socket path ends in www.sock.
To ensure seamless communication between Nginx and PHP-FPM, update your Nginx configuration to match the exact filename:
location ~\.php$ {
fastcgi_split_path_info ^(.+?\.php)(./*)$;
fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Don't forget to reload Nginx after making this change:
systemctl reload nginx
Understanding the Trade-offs
Using a named socket like www.sock is generally preferred over listening on a port (e.g., 127.0.0.1:9000) for several reasons. Named sockets offer better security isolation and are less prone to connection leaks in high-concurrency environments, which is particularly relevant when using the dynamic process manager. Additionally, they avoid potential conflicts with other local services that might try to bind to port 9000.
If you are running multiple PHP versions or need complex pool configurations (e.g., separating front-end and back-end logic), keep the socket filenames distinct. Always ensure that the user and group specified in www.conf match the user under which Nginx runs, typically apache or nginx. A mismatch here will result in "Permission denied" errors even if the path is correct.
Finally, remember that /var/run/ is often a symlink to /run/ on modern Linux distributions. If you encounter issues related to permissions or persistence across reboots, consider creating symlinks explicitly or using persistent directories as outlined in advanced PHP-FPM documentation. However, for standard CentOS 7 deployments with VirtualBox, the default /var/run behavior is usually sufficient once permissions are aligned.