# 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**

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).
    
    ```bash
    sudo grep 'listen' /etc/php/7.x/fpm/pool.d/www.conf
    ```
    
    Example output:
    
    ```bash
    listen = /var/run/php/php7.4-fpm.sock
    ```
    
2. **Check the Permissions and Ownership**
    
    Verify the permissions and ownership of the socket file:
    
    ```bash
    ls -l /var/run/php/php7.4-fpm.sock
    ```
    
    You should see output like this:
    
    ```bash
    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:
    
    ```bash
    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:
    
    ```bash
    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**
    
    ```bash
    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**
    
    ```bash
    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**
    
    ```bash
    sudo systemctl restart php7.4-fpm
    ```
    
    Replace `php7.4-fpm` with the appropriate service name for your PHP version.
    
2. **Restart Nginx**
    
    ```bash
    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**
    
    ```bash
    sudo tail -f /var/log/php7.4-fpm.log
    ```
    
2. **Check Nginx Log**
    
    ```bash
    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.