nginx error connect to php5-fpm.sock failed (13: Permission denied)

后端 未结 25 1733
悲哀的现实
悲哀的现实 2020-11-27 09:12

I update nginx to 1.4.7 and php to 5.5.12, After that I got the 502 error. Before I update everything works fine.

相关标签:
25条回答
  • 2020-11-27 09:35

    Check which user runs nginx. As of Ubuntu 12.04 nginx runs by nginx user which is not a member of www-data group.

    usermod -a -G www-data nginx

    and restarting nginx and php5-fpm daemons solves the problem.

    0 讨论(0)
  • 2020-11-27 09:35

    The following simple fix worked for me, bypassing possible permissions issues with the socket.

    In your nginx config, set fastcgi_pass to:

    fastcgi_pass   127.0.0.1:9000;
    

    Instead of

    fastcgi_pass   /var/run/php5-fpm.sock;
    

    This must match the listen = parameter in /etc/php5/fpm/pool.d/www.conf, so also set this to:

    listen = 127.0.0.1:9000;
    

    Then restart php5-fpm and nginx

    service php5-fpm restart
    

    And

    service nginx restart
    

    For more info, see: https://wildlyinaccurate.com/solving-502-bad-gateway-with-nginx-php-fpm/

    0 讨论(0)
  • 2020-11-27 09:35

    To those who tried everything in this thread and still stuck: This solved my problem. I updated /usr/local/nginx/conf/nginx.conf

    1. Uncomment the line saying user

    2. make it www-data so it becomes: user www-data;

    3. Save it (root access required)

    4. Restart nginx

    0 讨论(0)
  • 2020-11-27 09:36

    If you have tried everything in this post but are not having success getting PHP to work, this is what fixed it for my case:

    Make sure you have these lines uncommented in /etc/php5/fpm/pool.d/www.conf:

    listen.owner = www-data
    listen.group = www-data
    listen.mode = 0660
    

    Make sure /etc/nginx/fastcgi_params looks like this:

    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;
    
    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;
    fastcgi_param  PATH_INFO          $fastcgi_script_name;
    fastcgi_param  HTTPS              $https if_not_empty;
    
    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
    
    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;
    
    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;
    

    These two lines were missing from my /etc/nginx/fastcgi_params, make sure they are there!

    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO          $fastcgi_script_name;
    

    Then, restart php5-fpm and nginx. Should do the trick.

    0 讨论(0)
  • 2020-11-27 09:36

    The most important thing here is wich user is using nginx then do you need specify it as well

    in your nginx.conf

    user www-data;
    worker_processes  1;
    
            location / {
                root   /usr/home/user/public_html;
                index  index.php index.html index.htm;
            }
            location ~ [^/]\.php(/|$) {
                fastcgi_split_path_info ^(.+?\.php)(/.*)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param  SCRIPT_FILENAME    /usr/home/user/public_html$fastcgi_script_name;
                include fastcgi_params;
            }
    

    in your www.conf

    listen.owner = www-data
    listen.group = www-data
    ;listen.mode = 0660
    

    in your case the user and group is "www" so just replace it.

    • restart nginx and php fpm
    0 讨论(0)
  • 2020-11-27 09:38

    @Xander's solution works, but does not persist after a reboot.

    I found that I had to change listen.mode to 0660 in /etc/php5/fpm/pool.d/www.conf.

    Sample from www.conf:

    ; Set permissions for unix socket, if one is used. In Linux, read/write
    ; permissions must be set in order to allow connections from a web server. Many
    ; BSD-derived systems allow connections regardless of permissions. 
    ; Default Values: user and group are set as the running user
    ;                 mode is set to 0660
    ;listen.owner = www-data
    ;listen.group = www-data
    ;listen.mode = 0660
    

    Edit: Per @Chris Burgess, I've changed this to the more secure method.

    I removed the comment for listen.mode, .group and .owner:

    listen.owner = www-data
    listen.group = www-data
    listen.mode = 0660
    

    /var/run Only holds information about the running system since last boot, e.g., currently logged-in users and running daemons. (http://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard#Directory_structure).

    Side note:

    My php5-fpm -v Reports: PHP 5.4.28-1+deb.sury.org~precise+1. The issue did happen after a recent update as well.

    0 讨论(0)
提交回复
热议问题