Nginx Error Connect to Php5-fpm.sock Failed (13: Permission Denied)

Better Stack Team
Updated on October 7, 2024

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

  1. 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
    
  2. 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 or php-fpm for RedHat-based systems.

2. Adjust Permissions and Ownership

  1. 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.

  2. 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.

  1. Open the PHP-FPM Pool Configuration File

     
    sudo nano /etc/php/7.x/fpm/pool.d/www.conf
    
  2. Verify user and group Directives

    Ensure the user and group directives match the ownership of the socket file:

     
    user = www-data
    group = www-data
    

    Adjust these values to match your setup.

  3. Verify listen Directive

    Ensure 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.

  1. Open the Nginx Configuration File

     
    sudo nano /etc/nginx/sites-available/default
    
  2. Verify fastcgi_pass Directive

    Ensure 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.

  1. Restart PHP-FPM

     
    sudo systemctl restart php7.4-fpm
    

    Replace php7.4-fpm with the appropriate service name for your PHP version.

  2. Restart Nginx

     
    sudo systemctl restart nginx
    

6. Review Logs

If the issue persists, review the PHP-FPM and Nginx logs for additional information.

  1. Check PHP-FPM Log

     
    sudo tail -f /var/log/php7.4-fpm.log
    
  2. 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, and listen 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.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

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 us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build 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.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github