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.conf
or similar).sudo grep 'listen' /etc/php/7.x/fpm/pool.d/www.conf
Example output:
listen = /var/run/php/php7.4-fpm.sock
Check the Permissions and Ownership
Verify the permissions and ownership of the socket file:
ls -l /var/run/php/php7.4-fpm.sock
You should see output like this:
srw-rw---- 1 www-data www-data 0 Aug 1 12:00 /var/run/php/php7.4-fpm.sock
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-data
for Debian-based systems orphp-fpm
for 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:
sudo chown www-data:www-data /var/run/php/php7.4-fpm.sock
Replace
www-data
with 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:
sudo chmod 660 /var/run/php/php7.4-fpm.sock
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
sudo nano /etc/php/7.x/fpm/pool.d/www.conf
Verify
user
andgroup
DirectivesEnsure the
user
andgroup
directives match the ownership of the socket file:user = www-data group = www-data
Adjust these values to match your setup.
Verify
listen
DirectiveEnsure the
listen
directive specifies the correct path for the socket:listen = /var/run/php/php7.4-fpm.sock
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
sudo nano /etc/nginx/sites-available/default
Verify
fastcgi_pass
DirectiveEnsure the
fastcgi_pass
directive points to the correct socket file:location ~ \\.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
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
sudo systemctl restart php7.4-fpm
Replace
php7.4-fpm
with the appropriate service name for your PHP version.Restart Nginx
sudo systemctl restart nginx
6. Review Logs
If the issue persists, review the PHP-FPM and Nginx logs for additional information.
Check PHP-FPM Log
sudo tail -f /var/log/php7.4-fpm.log
Check Nginx Log
sudo tail -f /var/log/nginx/error.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
, andlisten
directives are correctly configured. - Verify Nginx Configuration: Ensure
fastcgi_pass
points 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.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github