How to configure the socket permission for Nginx + php5-fpm? [duplicate] - nginx
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.
nginx-error.log
2014/05/03 13:27:41 [crit] 4202#0: *1 connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream, client: xx.xxx.xx.xx, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "xx.xx.xx.xx"
nginx.conf
user www www;
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;
}
I had a similar error after php update. PHP fixed a security bug where o had rw permission to the socket file.
Open /etc/php5/fpm/pool.d/www.conf or /etc/php/7.0/fpm/pool.d/www.conf, depending on your version.
Uncomment all permission lines, like:
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
Restart fpm - sudo service php5-fpm restart or sudo service php7.0-fpm restart
Note: if your webserver runs as user other than www-data, you will need to update the www.conf file accordingly
All the fixes currently mentioned here basically enable the security hole all over again.
What I ended up doing is adding the following lines to my PHP-FPM configuration file.
listen.owner = www-data
listen.group = www-data
Make sure that www-data is actually the user the nginx worker is running as. For debian it's www-data by default.
Doing it this way does not enable the security problem that this change was supposed to fix.
#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.
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.
In fact, "listen.mode" should be: "0660" and not "0666" as Other Writable or Other Readable is never a good choice here.
So try to find out as which user/group your webserver runs. I use CentOs and it runs as user "nginx"
So add to your php-fpm.conf:
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
finally restart php-fpm
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.
Alternative to broadening permissions in your php config, you could change the user specified in your nginx config.
On the first line of your nginx.conf excerpt above, the user and group are specified as www and www, respectively.
user www www;
Meanwhile, your php config probably specifies a user and group of www-data:
listen.owner = www-data
listen.group = www-data
You might change the line in your nginx.conf, to any of the following, then:
user www-data www;
user www-data www-data; # or any group, really, since you have the user matching
user www www-data; # requires that your php listen.mode gives rw access to the group
I had the similar error.
All recommendations didn't help.
The only replacement www-data with nginx has helped:
$ sudo chown nginx:nginx /var/run/php/php7.2-fpm.sock
/var/www/php/fpm/pool.d/www.conf
user = nginx
group = nginx
...
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
The problem in my case was that the Nginx web server was running as user nginx and the pool was running as user www-data.
I solved the issue by changing the user Nginx is running at in the /etc/nginx/nginx.conf file (could be different on your system, mine is Ubuntu 16.04.1)
Change: user nginx;
to: user www-data;
then restart Nginx: service nginx restart
Consideration must also be given to your individual FPM pools, if any.
I couldn't figure out why none of these answers was working for me today. This had been a set-and-forget scenario for me, where I had forgotten that listen.user and listen.group were duplicated on a per-pool basis.
If you used pools for different user accounts like I did, where each user account owns their FPM processes and sockets, setting only the default listen.owner and listen.group configuration options to 'nginx' will simply not work. And obviously, letting 'nginx' own them all is not acceptable either.
For each pool, make sure that
listen.group = nginx
Otherwise, you can leave the pool's ownership and such alone.
I just got this error again today as I updated my machine (with updates for PHP) running Ubuntu 14.04. The distribution config file /etc/php5/fpm/pool.d/www.conf is fine and doesn't require any changes currently.
I found the following errors:
dmesg | grep php
[...]
[ 4996.801789] traps: php5-fpm[23231] general protection ip:6c60d1 sp:7fff3f8c68f0 error:0 in php5-fpm[400000+800000]
[ 6788.335355] traps: php5-fpm[9069] general protection ip:6c5d81 sp:7fff98dd9a00 error:0 in php5-fpm[400000+7ff000]
The strange thing was that I have 2 sites running that utilize PHP-FPM on this machine one was running fine and the other (a Tiny Tiny RSS installation) gave me a 502, where both have been running fine before.
I compared both configuration files and found that fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; was missing for the affected site.
Both configuration files now contain the following block and are running fine again:
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include /etc/nginx/snippets/fastcgi-php.conf;
}
Update
It should be noted that Ubuntu ships two fastcgi related parameter files and also a configuration snippet which is available since Vivid and also in the PPA version. The solution was updated accordingly.
Diff of the fastcgi parameter files:
$ diff -up fastcgi_params fastcgi.conf
--- fastcgi_params 2015-07-22 01:42:39.000000000 +0200
+++ fastcgi.conf 2015-07-22 01:42:39.000000000 +0200
## -1,4 +1,5 ##
+fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
Configuration snippet in /etc/nginx/snippets/fastcgi-php.conf
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;
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/
Simple but works..
listen.owner = nginx
listen.group = nginx
chown nginx:nginx /var/run/php-fpm/php-fpm.sock
I have fixed same issue on Amazon Linux AMI 2016.09 (Centos 7) by taking following steps.
Open your www.conf files (Example : sudo nano /etc/php-fpm.d/www.conf)
Lastly, find the lines that set the listen.owner and listen.group and change their values from "nobody" to "nginx":
listen.owner = nginx
listen.group = nginx
listen.mode = 0666
Lastly, find the lines that set the user and group and change their values from "apache" to "nginx":
user = nginx
group = nginx
Restart php-fpm (sudo service php-fpm restart)
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
Just see /etc/php5/php-fpm.conf
pid = /var/run/php5-fpm.pid IS PID file
In file /etc/php5/fpm/pool.d/www.conf
listen = /var/run/php5-fpm.sock IS SOCKET file
if you pid equal listen (pid = /var/run/php5-fpm.sock and listen = /var/run/php5-fpm.sock) -> wrong settings
and finish sett /etc/php5/fpm/pool.d/www.conf
user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
Just to add, on CentOS (and probably Red Hat and Fedora) the file to change the permissions to is at:
/etc/php-fpm.d/www.conf
If you have different pool per user make sure user and group are set correctly in configuration file. You can find nginx user in /etc/nginx/nginx.conf file. nginx group is same as nginx user.
user = [pool-user]
group = [pool-group]
listen.owner = [nginx-user]
listen.group = [nginx-group]
Also check SELINUX (/etc/selinux):
# getenforce
turn it off:
# setenforce 0
In my case php-fpm wasn't running at all, so I just had to start the service 😂
service php7.3-fpm start
#on ubuntu 18.04
After upgrading from Ubuntu 14.04 lts to Ubuntu 16.04 lts I found a yet another reason for this error that I haven't seen before.
During the upgrading process I had somehow lost my php5-fpm executable altogether. All the config files were intact and it took me a while to realize that service php5-fpm start didn't really start a process, as it did not show any errors.
My moment of awakening was when I noticed that there were no socket file in /var/run/php5-fpm.sock, as there should be, nor did netstat -an show processes listening on the port that I tried as an alternative while trying to solve this problem. Since the file /usr/sbin/php5-fpm was also non-existing, I was finally on the right track.
In order to solve this problem I upgraded php from version 5.5 to 7.0. apt-get install php-fpm did the trick as a side effect. After that and installing other necessary packages everything was back to normal.
This upgrading solution may have problems of its own, however. Since php has evolved quite a bit, it's possible that the software will break in unimaginable ways. So, even though I did go down that path, you may want to keep the version you're fond of just for a while longer.
Luckily, there seems to be a neat way for that, as described on The Customize Windows site:
add-apt-repository ppa:ondrej/php
apt-get purge php5-common
apt-get update
apt-get install php5.6
Neater solution as it might be, I didn't try that. I expect the next couple of days will tell me whether I should have.
All right here:
user = nginx
group = nginx
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
But change here too:
; When set, listen.owner and listen.group are ignored
listen.acl_users = apache
;listen.acl_groups =
Comment this line:
; When set, listen.owner and listen.group are ignored
;listen.acl_users = apache
;listen.acl_groups =
I did change OS on my server quite a few times trying to get the most comfortable system.
It used to work very well most of the time but lastly I got this 502 Gateway error.
I use a php fpm socket for each account instead of keeping the same one for all. So if one crashes, at least the other applications keep running.
I used to have user and group www-data. But this changed on my Debian 8 with latest Nginx 1.8 and php5-fpm.
The default user is nginx and so is the group. To be sure of this, the best way is to check the /etc/group and /etc/passwd files. These can't lie.
It is there I found that now I have nginx in both and no longer www-data.
Maybe this can help some people still trying to find out why the error message keeps coming up.
It worked for me.
To those who tried everything in this thread and still stuck: This solved my problem.
I updated /usr/local/nginx/conf/nginx.conf
Uncomment the line saying user
make it www-data so it becomes: user www-data;
Save it (root access required)
Restart nginx
If you have declarations
pid = /run/php-fpm.pid
and
listen = /run/php-fpm.pid
in different configuration files, then root will owner of this file.
Please NOTICE (at least in centos 8) the user who you are assigning listen.owner and other stuff to it MUST
be in the same POOL with the user for example given I am the foo user
[www] # WRONG | IN MY CASE I WAS UNDER www POOL SO IT WASNT WORKING FOR ME.
[foo] # CORRECT | THE POOL AND THE USER MATCHES.
listen.owner = foo
listen.group = foo
listen.mode = 0660
user = foo
group = foo
I dont know if there's a global pool but after hours of searching I finlly did it.
For me it was unix: directive before PHP-FPM listen socket define /run/php-fpm/php-fpm.sock inside www.conf file
my simple Nginx config file php block looks like this
location ~ \.php$ {
root /home/mysuser/www;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Related
How to consistently setup PHP-FPM 5.6 with nginx on Amazon EC2 AMI instance
I cannot find a way to setup php-fpm on nginx on Amazon AMI EC2 instance from scratch. I know this should not be that difficult, but finding different answers based on *nix versions is confusing. Here are the condensed steps I've taken that I thought would work, but don't. Does anyone have a set of steps to reliably setup php-fpm with nginx in Amazon AMI EC2 instance? I've intentionally left out nginx.conf, etc from this post since they are the "stock" installations from the default yum repositories. nginx version: 1.6.2 Does anyone have reliable steps to setup php-fpm in nginx for Amazon AMI EC2 instances? I would prefer to setup myself instead of using the AMI in the Amazon marketplace that charges for this setup. Thanks # install packages yum install -y nginx yum install -y php56-fpm.x86_64 # enable php in nginx.conf vi /etc/nginx/nginx.conf # add index.php at the beginning of index index index.php index.html index.htm; # uncomment the php block in nginx.conf location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } # tell php-fpm to run as same account as nginx vi /etc/php-fpm-5.6.d/www.conf - change user and group apache to nginx # allow nginx user to read website files since they are typically owned by root cd /usr/share/nginx chown -R nginx:nginx html # check to see if php works - doesn't with these steps echo "<?php phpinfo(); ?>" > /usr/share/nginx/info.php # restart services since we changed things service nginx restart service php-fpm-5.6 restart # verify root path exists and is owned by nginx as we said above # ls -l /usr/share/nginx/html -rw-r--r-- 1 nginx nginx 3696 Mar 6 03:53 404.html -rw-r--r-- 1 nginx nginx 3738 Mar 6 03:53 50x.html -rw-r--r-- 1 nginx nginx 3770 Mar 6 03:53 index.html -rw-r--r-- 1 nginx nginx 20 Apr 14 14:01 index.php # I also verified php-fpm is listening on port 9000 and nginx is setup that way in the nginx.conf # port 9000 usage is the default and I left it as-is for this question, but I would prefer to use sock once I get this working. Edit This is what I see in the nginx error log 2015/04/14 17:08:25 [error] 916#0: *9 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 12.34.56.78, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "12.34.56.90"
What do you see in nginx error log (/var/log/nginx/errors.log)? Added after additional info (logs) provided: To me it looks root should be server section not location. server { ... root /usr/share/nginx/html; ... location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } }
Where is your index.php file? If it is here: /usr/share/nginx/html/index.php then change this line fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; to: fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
Nginx 504 Time-out with php-fpm
I got 504 Gateway Time-out for one of my sites. Please, help to find out why? Here's my nginx configuration: server { listen 80; server_name domain.com; root /home/user/domain.com; error_log /home/user/domain.com/error.log; location / { try_files $uri /index.php?q=$uri&$args; } location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.*)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } Nginx error log: $ sudo tail -n 1 domain.com/error.log 2014/08/21 17:09:35 [error] 16790#0: *30 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 5.18.54.52, server: domain.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock", host: "domain.com" And nothing in php-fpm log: sudo tail -n 1 /var/log/php5-fpm.log [21-Aug-2014 17:08:31] NOTICE: ready to handle connections And here my www.conf: $ sudo nano /etc/php5/fpm/pool.d/www.conf pm = dynamic pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 1 pm.max_spare_servers = 10 Please help me to solve this! Thanks!
As of PHP 5.5.12 FPM Socket permissions were changed to resolve a security related bug, you can read more about that here -> https://bugs.php.net/bug.php?id=67060 Your listen.mode = 0660 should now be set to listen.mode = 0666 inside of your FPM Pool Configuration. Also make certain that your listen.owner and listen.group is the same user name/group that is running Nginx. Here is what I have in my setup: listen.owner = nginx listen.group = nginx listen.mode = 0666 I am assuming that your Nginx Configuration has user www-data; then simply change the nginx reference I have above to www-data of course. As for Nginx here is a working example I am currently using: # PHP-FPM Support location ~ \.php$ { fastcgi_pass unix:/var/run/nginx.sock; include fastcgi.conf; } I think your Nginx Configuration is just fine, but in case you wanted to try something a little different I'm posting my config as a test for you. Don't forget to restart PHP-FPM after making any configuration changes too. Give this a try, I'm certain it should resolve your problem.
Unable to get error stack trace or error log while using php-fpm + nginx
I am using php-fpm 5.5.9 along with nginx 1.4.6 on my Ubuntu 14.04 machine. I have installed them using apt-get package manager. I am unable to get a stack trace of the error that my index.php script encounters in error log as well as on the browser. I searched and implemented a couple of solutions from stackoverflow and other articles but none of them worked for me. Here is my nginx conf along with my php-fpm conf file. Please help me out if I am doing any silly mistake. Nginx Configuration: location ~ \.php$ { # With php5-fpm: #try_files $uri =404; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; fastcgi_read_timeout 600; fastcgi_send_timeout 600; proxy_connect_timeout 600; proxy_send_timeout 600s; proxy_read_timeout 600s; fastcgi_pass 127.0.0.1:7777; fastcgi_index index.php; } PHP-FPM Configuration: error_log = /tmp/php5-fpm.log PHP-FPM pool Configuration: catch_workers_output = yes slowlog = /var/log/php-fpm/$pool.log.slow listen = 127.0.0.1:7777 php_flag[display_errors] = On php_admin_value[error_log] = /tmp/fpm-php.www.log php_admin_flag[log_errors] = On Thanks in advance.
Honestly couldn't find reasonable solution without using PHP xdebug module. sudo apt-get install php5-xdebug It should install the module configuration, may have to restart php-fpm afterwards though. sudo service php5-fpm restart Once that was installed I could finally get a stack trace out from php5-fpm.
In your configuration is says that the error_log file is in /etc/... However you could just be looking in the wrong place. Did you anyway check the default error log location? Usually it should be: /var/log/nginx/* - there is a nginx_error.log file in there. Possibly other log files as well. Also note that the PHP-FPM and nginx config configurations differ in syntax. Check if you have such an error anywhere. It may be parsed wrongly and thus your errors. Check file permissions for the error log. Is nginx's running user able to Write there?
nginx error connect to php5-fpm.sock failed (13: Permission denied)
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. nginx-error.log 2014/05/03 13:27:41 [crit] 4202#0: *1 connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream, client: xx.xxx.xx.xx, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "xx.xx.xx.xx" nginx.conf user www www; 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; }
I had a similar error after php update. PHP fixed a security bug where o had rw permission to the socket file. Open /etc/php5/fpm/pool.d/www.conf or /etc/php/7.0/fpm/pool.d/www.conf, depending on your version. Uncomment all permission lines, like: listen.owner = www-data listen.group = www-data listen.mode = 0660 Restart fpm - sudo service php5-fpm restart or sudo service php7.0-fpm restart Note: if your webserver runs as user other than www-data, you will need to update the www.conf file accordingly
All the fixes currently mentioned here basically enable the security hole all over again. What I ended up doing is adding the following lines to my PHP-FPM configuration file. listen.owner = www-data listen.group = www-data Make sure that www-data is actually the user the nginx worker is running as. For debian it's www-data by default. Doing it this way does not enable the security problem that this change was supposed to fix.
#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.
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.
In fact, "listen.mode" should be: "0660" and not "0666" as Other Writable or Other Readable is never a good choice here. So try to find out as which user/group your webserver runs. I use CentOs and it runs as user "nginx" So add to your php-fpm.conf: listen.owner = nginx listen.group = nginx listen.mode = 0660 finally restart php-fpm
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.
Alternative to broadening permissions in your php config, you could change the user specified in your nginx config. On the first line of your nginx.conf excerpt above, the user and group are specified as www and www, respectively. user www www; Meanwhile, your php config probably specifies a user and group of www-data: listen.owner = www-data listen.group = www-data You might change the line in your nginx.conf, to any of the following, then: user www-data www; user www-data www-data; # or any group, really, since you have the user matching user www www-data; # requires that your php listen.mode gives rw access to the group
I had the similar error. All recommendations didn't help. The only replacement www-data with nginx has helped: $ sudo chown nginx:nginx /var/run/php/php7.2-fpm.sock /var/www/php/fpm/pool.d/www.conf user = nginx group = nginx ... listen.owner = nginx listen.group = nginx listen.mode = 0660
The problem in my case was that the Nginx web server was running as user nginx and the pool was running as user www-data. I solved the issue by changing the user Nginx is running at in the /etc/nginx/nginx.conf file (could be different on your system, mine is Ubuntu 16.04.1) Change: user nginx; to: user www-data; then restart Nginx: service nginx restart
Consideration must also be given to your individual FPM pools, if any. I couldn't figure out why none of these answers was working for me today. This had been a set-and-forget scenario for me, where I had forgotten that listen.user and listen.group were duplicated on a per-pool basis. If you used pools for different user accounts like I did, where each user account owns their FPM processes and sockets, setting only the default listen.owner and listen.group configuration options to 'nginx' will simply not work. And obviously, letting 'nginx' own them all is not acceptable either. For each pool, make sure that listen.group = nginx Otherwise, you can leave the pool's ownership and such alone.
I just got this error again today as I updated my machine (with updates for PHP) running Ubuntu 14.04. The distribution config file /etc/php5/fpm/pool.d/www.conf is fine and doesn't require any changes currently. I found the following errors: dmesg | grep php [...] [ 4996.801789] traps: php5-fpm[23231] general protection ip:6c60d1 sp:7fff3f8c68f0 error:0 in php5-fpm[400000+800000] [ 6788.335355] traps: php5-fpm[9069] general protection ip:6c5d81 sp:7fff98dd9a00 error:0 in php5-fpm[400000+7ff000] The strange thing was that I have 2 sites running that utilize PHP-FPM on this machine one was running fine and the other (a Tiny Tiny RSS installation) gave me a 502, where both have been running fine before. I compared both configuration files and found that fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; was missing for the affected site. Both configuration files now contain the following block and are running fine again: location ~ \.php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; include /etc/nginx/snippets/fastcgi-php.conf; } Update It should be noted that Ubuntu ships two fastcgi related parameter files and also a configuration snippet which is available since Vivid and also in the PPA version. The solution was updated accordingly. Diff of the fastcgi parameter files: $ diff -up fastcgi_params fastcgi.conf --- fastcgi_params 2015-07-22 01:42:39.000000000 +0200 +++ fastcgi.conf 2015-07-22 01:42:39.000000000 +0200 ## -1,4 +1,5 ## +fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; Configuration snippet in /etc/nginx/snippets/fastcgi-php.conf # regex to split $uri to $fastcgi_script_name and $fastcgi_path fastcgi_split_path_info ^(.+\.php)(/.+)$; # Check that the PHP script exists before passing it try_files $fastcgi_script_name =404; # Bypass the fact that try_files resets $fastcgi_path_info # see: http://trac.nginx.org/nginx/ticket/321 set $path_info $fastcgi_path_info; fastcgi_param PATH_INFO $path_info; fastcgi_index index.php; include fastcgi.conf;
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/
Simple but works.. listen.owner = nginx listen.group = nginx chown nginx:nginx /var/run/php-fpm/php-fpm.sock
I have fixed same issue on Amazon Linux AMI 2016.09 (Centos 7) by taking following steps. Open your www.conf files (Example : sudo nano /etc/php-fpm.d/www.conf) Lastly, find the lines that set the listen.owner and listen.group and change their values from "nobody" to "nginx": listen.owner = nginx listen.group = nginx listen.mode = 0666 Lastly, find the lines that set the user and group and change their values from "apache" to "nginx": user = nginx group = nginx Restart php-fpm (sudo service php-fpm restart)
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
Just see /etc/php5/php-fpm.conf pid = /var/run/php5-fpm.pid IS PID file In file /etc/php5/fpm/pool.d/www.conf listen = /var/run/php5-fpm.sock IS SOCKET file if you pid equal listen (pid = /var/run/php5-fpm.sock and listen = /var/run/php5-fpm.sock) -> wrong settings and finish sett /etc/php5/fpm/pool.d/www.conf user = nginx group = nginx listen.owner = nginx listen.group = nginx listen.mode = 0660
Just to add, on CentOS (and probably Red Hat and Fedora) the file to change the permissions to is at: /etc/php-fpm.d/www.conf
If you have different pool per user make sure user and group are set correctly in configuration file. You can find nginx user in /etc/nginx/nginx.conf file. nginx group is same as nginx user. user = [pool-user] group = [pool-group] listen.owner = [nginx-user] listen.group = [nginx-group]
Also check SELINUX (/etc/selinux): # getenforce turn it off: # setenforce 0
In my case php-fpm wasn't running at all, so I just had to start the service 😂 service php7.3-fpm start #on ubuntu 18.04
After upgrading from Ubuntu 14.04 lts to Ubuntu 16.04 lts I found a yet another reason for this error that I haven't seen before. During the upgrading process I had somehow lost my php5-fpm executable altogether. All the config files were intact and it took me a while to realize that service php5-fpm start didn't really start a process, as it did not show any errors. My moment of awakening was when I noticed that there were no socket file in /var/run/php5-fpm.sock, as there should be, nor did netstat -an show processes listening on the port that I tried as an alternative while trying to solve this problem. Since the file /usr/sbin/php5-fpm was also non-existing, I was finally on the right track. In order to solve this problem I upgraded php from version 5.5 to 7.0. apt-get install php-fpm did the trick as a side effect. After that and installing other necessary packages everything was back to normal. This upgrading solution may have problems of its own, however. Since php has evolved quite a bit, it's possible that the software will break in unimaginable ways. So, even though I did go down that path, you may want to keep the version you're fond of just for a while longer. Luckily, there seems to be a neat way for that, as described on The Customize Windows site: add-apt-repository ppa:ondrej/php apt-get purge php5-common apt-get update apt-get install php5.6 Neater solution as it might be, I didn't try that. I expect the next couple of days will tell me whether I should have.
All right here: user = nginx group = nginx listen.owner = nginx listen.group = nginx listen.mode = 0660 But change here too: ; When set, listen.owner and listen.group are ignored listen.acl_users = apache ;listen.acl_groups = Comment this line: ; When set, listen.owner and listen.group are ignored ;listen.acl_users = apache ;listen.acl_groups =
I did change OS on my server quite a few times trying to get the most comfortable system. It used to work very well most of the time but lastly I got this 502 Gateway error. I use a php fpm socket for each account instead of keeping the same one for all. So if one crashes, at least the other applications keep running. I used to have user and group www-data. But this changed on my Debian 8 with latest Nginx 1.8 and php5-fpm. The default user is nginx and so is the group. To be sure of this, the best way is to check the /etc/group and /etc/passwd files. These can't lie. It is there I found that now I have nginx in both and no longer www-data. Maybe this can help some people still trying to find out why the error message keeps coming up. It worked for me.
To those who tried everything in this thread and still stuck: This solved my problem. I updated /usr/local/nginx/conf/nginx.conf Uncomment the line saying user make it www-data so it becomes: user www-data; Save it (root access required) Restart nginx
If you have declarations pid = /run/php-fpm.pid and listen = /run/php-fpm.pid in different configuration files, then root will owner of this file.
Please NOTICE (at least in centos 8) the user who you are assigning listen.owner and other stuff to it MUST be in the same POOL with the user for example given I am the foo user [www] # WRONG | IN MY CASE I WAS UNDER www POOL SO IT WASNT WORKING FOR ME. [foo] # CORRECT | THE POOL AND THE USER MATCHES. listen.owner = foo listen.group = foo listen.mode = 0660 user = foo group = foo I dont know if there's a global pool but after hours of searching I finlly did it.
For me it was unix: directive before PHP-FPM listen socket define /run/php-fpm/php-fpm.sock inside www.conf file my simple Nginx config file php block looks like this location ~ \.php$ { root /home/mysuser/www; fastcgi_pass unix:/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
"No input file specified" from php-fpm for 1-2 min after restart
Using php-fpm with nginx, everything works OK, except for about 1-2 minutes after php-fpm is restarted. During that period, "No input file specified" is returned for all .php URLs. After 1-2 minutes, everything goes back to normal without further action. Example entry in /var/log/nginx/error.log: 2014/03/04 09:21:22 [error] 1206#0: *55 FastCGI sent in stderr: "Unable to open primary script: /usr/share/nginx/html (Success)", client: xxx.aaa.bbb.ccc, server: www.example.com, request: "GET /xxx/index.php HTTP/1.1", host: "www.example.com" Here's the server block from server.conf: server { listen 443 ssl; server_name www.example.com; ssl_certificate /etc/nginx/ssl-bundle.crt; ssl_certificate_key /etc/nginx/cert.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; root /usr/share/nginx/html; index index.php index.html; location / { try_files $uri $uri/ $uri/index.php; } location ~ \.php { fastcgi_split_path_info ^(.+\.php)(.+)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_pass php; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } And /etc/nginx/nginx.conf: http { ... sendfile on; keepalive_timeout 65; charset utf-8; gzip on; gzip_static on; etag on; upstream php { server unix:/var/run/php5-fpm.sock; } open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; keepalive_requests 100000; expires max; include /etc/nginx/conf.d/*.conf; } Any ideas how to fix it? The fact that it is only happening for a brief period after restarting php-fpm seems to rule out some common problems (e.g. permissions) and seems more like caching? Thanks!
That's really weird. This probably isn't an answer as such, but here follows far too much text to fit in a little comment box. 1) The error is definitely coming from PHP-FPM, and isn't a cache problem. The error message is coming from: http://lxr.php.net/xref/PHP_5_5/sapi/fpm/fpm/fpm_main.c#1920 due to PHP not being able to open the script. Apparently zlog errors get written to the stderr PHP log - which you have set to /var/log/php-fpm/www-error.log. Is there anything in there? If not you may need to either recompile PHP, or tweak your log settings to find out what the error is. 2) Running PHP-FPM as nginx user is kind of weird. Nginx needs to run as a privileged process as it listens to port 80, PHP-FPM doesn't. I usually create a user per website, and then have the PHP-FPM pool for each site run under that user. Probably not related to your issue - but who knows. 3) Not being open a file is really weird and I don't understand it at all. You may need to run strace to figure out what is actually happening, like this: #!/bin/bash # Prevent strace from abbreviating arguments? # You want the -s strsize option, which specifies the maximum length of a string to display (the default is 32). rm -rf trc/*.trc additional_strace_args="$1" mkdir trc MASTER_PID=$(ps auwx | grep php-fpm | grep -v grep | grep 'master process' | cut -d ' ' -f 7) # ls -l /tmp/foobar | awk '{print $1"\t"$9}' # This code takes some input, such as this: # -rw-rw-rw- 1 root root 1 Jul 14 1997 tmpmsg # and generates output like this: # -rw-rw-rw- tmpmsg #shows total of calls summarise="" # summarise="-c" nohup strace -r $summarise -p $MASTER_PID -ff -o ./trc/master.follow.trc >"trc/master.$MASTER_PID.trc" 2>&1 & while read -r pid; do if [[ $pid != $MASTER_PID ]]; then #shows individual calls #nohup strace -r -p "$pid" $additional_strace_args >"trc/$pid.trc" 2>&1 & #shows total of calls nohup strace -r $summarise -p "$pid" $additional_strace_args >"trc/$pid.summary.trc" 2>&1 & fi done < <(pgrep php-fpm) read -p "Strace running - press [Enter] to stop" pkill strace btw I'm going to go ahead and guess that you have long running php-fpm scripts. The restart command you're using are doing a graceful restart. If one of the scripts is taking ages, that would probably explain the behaviour you're seeing.
I think I've solved the problem. It was necessary to add /usr/share/nginx/html (or more generally, $document_root) to the open_basedir path in either php.ini or the nginx config. This works: location ~ \.php { fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_param PHP_VALUE "open_basedir=$document_root"; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass php; fastcgi_index index.php; } Commenting out the PHP_VALUE causes the problem to return. I still don't understand why the problem occurred or why this solves it; whether it was something to do with php-fpm, APC, nginx, or whatever.
As per https://www.php.net/manual/en/ini.core.php Using open_basedir will disable the realpath cache.