How to Fix “/var/run/php-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1”

The “/var/run/php-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1” error is a common issue that developers and administrators encounter when working with PHP-FPM on a Linux-based server. This error is typically related to incorrect file permissions or ownership on the PHP-FPM socket file, which prevents the web server (like Nginx or Apache) from connecting to it.

2014/09/17 00:17:30 [crit] 11909#0: *34 connect() to unix:/var/run/php-fpm.sock failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: www.webhostinggeeks.com, request: "GET /feed/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm.sock:", host: "www.webhostinggeeks.com"

In this tutorial, we will walk through the steps necessary to diagnose and resolve this issue. Our goal is to ensure that your web server can communicate effectively with PHP-FPM, thus maintaining the smooth functioning of your PHP applications.

Step 1: Confirm the Error

The first step in resolving any issue is to ensure you’ve correctly identified the problem. Check your server’s error logs to confirm the presence of the “permission denied” error. On most systems, you can use the tail command to display the latest entries in the log file:

tail /var/log/nginx/error.log

Look for the “permission denied” error message in the output.

See also  How to Enable mod_rewrite Apache module on CentOS

Step 2: Check PHP-FPM Socket Permissions

The next step is to examine the permissions on the PHP-FPM socket file. Use the ls command with the -l option to display detailed information about the file:

ls -l /var/run/php-fpm/php-fpm.sock

The output should show the file owner, group, and permissions. The web server needs to have the necessary rights to read and write to this file.

Step 3: Change Socket Permissions or Ownership

If the permissions or ownership are incorrect, you need to change them. This can be done using the chmod and chown commands, respectively.

To change the permissions, you can use the chmod command. For example, to give read and write access to the owner and group:

sudo chmod 660 /var/run/php-fpm/php-fpm.sock

If the file ownership needs to be changed, use the chown command. For instance, if your web server runs as the www-data user and group:

sudo chown www-data:www-data /var/run/php-fpm/php-fpm.sock

Step 4: Restart PHP-FPM and Your Web Server

After making these changes, you need to restart both PHP-FPM and your web server for the changes to take effect. The commands to do this will depend on your specific server setup, but they might look like this:

sudo service php7.4-fpm restart
sudo service nginx restart

Commands Mentioned:

  • tail – Displays the last part of files
  • ls – List directory contents
  • chmod – Change file mode bits
  • chown – Change file owner and group
  • service – Run a System V init script
See also  How to Install and Configure Apache2, PHP and MySQL 5.6 on Ubuntu 14.04

Conclusion

The “/var/run/php-fpm.sock failed (13: Permission denied) while connecting to upstream” error can be a stumbling block when running PHP applications on a web server. However, by carefully checking the permissions and ownership of the PHP-FPM socket file, and making necessary adjustments, this issue can be resolved effectively. It’s critical to understand that the web server must have the right permissions to read and write to the PHP-FPM socket file; otherwise, it will be unable to process PHP requests, leading to errors and potential downtime for your applications.

See also  How to Install Httpd on CentOS 5.7

In the process of troubleshooting and resolving this error, we’ve used several Linux commands, including tail to review server logs, ls to examine file permissions, chmod and chown to adjust file permissions and ownership respectively, and service to restart services. These commands are fundamental tools in the system administrator’s toolkit, enabling you to manage and maintain the smooth operation of your server.

By following this guide, you should now have a functioning PHP-FPM setup with the correct permissions, contributing to the overall performance and reliability of your PHP applications. Understanding these steps not only helps in resolving this specific issue but also enhances your ability to troubleshoot similar issues in the future.

We encourage you to share your experiences, ask questions, or suggest improvements in the comments section below.

Your feedback helps us to refine our content and better assist you in your journey with Linux and PHP-FPM.

Comments

1 Comment

  • Avatar troy says:

    Hi Dimitri, following your great advice, i still get error /var/run/php/php-fpm-librenms.sock failed (13: Permission denied) while connecting to upstream, here is the config.

    administrator@nms:~$ nano /etc/php/8.1/fpm/pool.d/librenms.conf
    ——————————————————-
    listen = /var/run/php/php-fpm-librenms.sock
    ——————————————————-

    administrator@nms:~$ nano /etc/nginx/conf.d/librenms.conf
    ——————————————————-
    fastcgi_pass unix:/var/run/php/php-fpm-librenms.sock;
    ——————————————————-

    administrator@nms:~$ ls -l /etc/nginx/conf.d/librenms.conf
    -rw-r–r– 1 root root 563 Feb 17 10:24 /etc/nginx/conf.d/librenms.conf

    administrator@nms:~$ ls -l /opt/librenms/config.php
    -rw-r–r– 1 librenms librenms 1702 Feb 17 10:44 /opt/librenms/config.php

    administrator@nms:~$ ls -l /var/run/php/php-fpm-librenms.sock
    srw-rw—- 1 www-data www-data 0 Aug 10 16:04 /var/run/php/php-fpm-librenms.sock

    please your advice for this issue

Leave a Reply

Your email address will not be published. Required fields are marked *