Nginx Error Connect to Php5-fpm.sock Failed (13: Permission Denied)
The error connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) indicates that Nginx is unable to connect to the PHP-FPM socket due to permission issues. This is a common issue when the Nginx user does not have the necessary permissions to access the PHP-FPM socket file.
Here’s a step-by-step guide to resolve this issue:
1. Check PHP-FPM Socket File Permissions
Locate the Socket File
Find out where the PHP-FPM socket file is located. This is typically defined in the PHP-FPM configuration file (
/etc/php/7.x/fpm/pool.d/www.confor similar).Example output:
Check the Permissions and Ownership
Verify the permissions and ownership of the socket file:
You should see output like this:
Ensure the socket file is writable by the Nginx user. Typically, the socket should be owned by the PHP-FPM user and group, which is often
www-datafor Debian-based systems orphp-fpmfor RedHat-based systems.
2. Adjust Permissions and Ownership
Change Ownership
Ensure the socket file is owned by the PHP-FPM user and group. If necessary, change the ownership:
Replace
www-datawith the appropriate user and group for your setup.Modify Permissions
Ensure the permissions allow the Nginx user to read and write to the socket file. Set the appropriate permissions if needed:
3. Check PHP-FPM Configuration
Ensure the PHP-FPM configuration file specifies the correct user and group and that they match the permissions on the socket.
Open the PHP-FPM Pool Configuration File
Verify
userandgroupDirectivesEnsure the
userandgroupdirectives match the ownership of the socket file:Adjust these values to match your setup.
Verify
listenDirectiveEnsure the
listendirective specifies the correct path for the socket:
4. Check Nginx Configuration
Ensure the Nginx configuration is set up to use the correct socket file path for PHP-FPM.
Open the Nginx Configuration File
Verify
fastcgi_passDirectiveEnsure the
fastcgi_passdirective points to the correct socket file:
5. Restart PHP-FPM and Nginx
After making changes to the permissions or configuration files, restart PHP-FPM and Nginx to apply the changes.
Restart PHP-FPM
Replace
php7.4-fpmwith the appropriate service name for your PHP version.Restart Nginx
6. Review Logs
If the issue persists, review the PHP-FPM and Nginx logs for additional information.
Check PHP-FPM Log
Check Nginx Log
Summary
- Check Socket File Permissions: Ensure the Nginx user has access to the PHP-FPM socket file.
- Adjust Ownership and Permissions: Set appropriate ownership and permissions for the socket file.
- Verify PHP-FPM Configuration: Ensure
user,group, andlistendirectives are correctly configured. - Verify Nginx Configuration: Ensure
fastcgi_passpoints to the correct socket. - Restart Services: Restart PHP-FPM and Nginx to apply changes.
- Review Logs: Check logs for further diagnostics.
By following these steps, you should be able to resolve the permission denied error and allow Nginx to successfully connect to the PHP-FPM socket.