191

I've just installed a nginx+php-fpm server. Everything seems fine except that PHP-FPM never writes error to its log.

fpm.conf

[default]
listen = /var/run/php-fpm/default.sock
listen.allowed_clients = 127.0.0.1
listen.owner = webusr
listen.group = webusr
listen.mode = 0666
user = webusr
group = webusr
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.status_path = /php/fpm/status
ping.path = /php/fpm/ping
request_terminate_timeout = 30s
request_slowlog_timeout = 10s
slowlog = /var/log/php-fpm/default/slow.log
chroot = /var/www/sites/webusr
catch_workers_output = yes
env[HOSTNAME] = mapsvr.mapking.com
php_flag[display_errors] = on
php_admin_value[error_log] = /var/log/php-fpm/default/error.log
php_admin_flag[log_errors] = on

nginx.conf

server
{
  listen        80 default_server;
  server_name   _;

  charset       utf-8;
  access_log    /var/log/nginx/access.log rest;

  include       conf.d/drops.conf.inc;

  location      /
  {
    root        /var/www/sites/webusr/htdocs;
    index       index.html index.htm index.php;
  }

  # pass the PHP scripts to FastCGI server listening on socket
  #
  location      ~ \.php$
  {
    root           /var/www/sites/webusr/htdocs;
    include        /etc/nginx/fastcgi_params;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME /htdocs/$fastcgi_script_name;
    if (-f $request_filename)
    {
      fastcgi_pass   unix:/var/run/php-fpm/default.sock;
    }
  }

  location      = /php/fpm/status
  {
    include        /etc/nginx/fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass   unix:/var/run/php-fpm/default.sock;
  }

  location      = /php/fpm/ping
  {
    include        /etc/nginx/fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass   unix:/var/run/php-fpm/default.sock;
  }

  # redirect server error pages to the static page /50x.html
  #
  error_page    500 502 503 504  /50x.html;
  location      = /50x.html
  {
    root        /usr/share/nginx/html;
  }
}

I've made an erroneous php script and run, and see error output on the web browser. Also nginx error log states stderr output from fpm with the same message. I've check that the user have write (I've even tried 777) permission to the appointed log folder. Even the appointed error.log file has be created successfully by php-fpm. However, the log file is always empty, no matter what outrageous error has been made from php script.

What's going on?

[Found the reason quite a while later]

It was permission. Changed the owner to the sites's users solved the problem.

3
  • It worth to mention that problematic php version is between v5.3.9 and v5.3.14 (as on today). It should work starting from v5.3.15 and v5.4.5. Commented Jul 22, 2012 at 14:41
  • if you're using hhvm + php-fpm with homestead the log is at tail /var/log/hhvm/error.log Commented Dec 23, 2015 at 6:27
  • Same problem here. It seems like the access and slow log need different permissions, thus the folder is created with root-755. Commented Mar 1, 2017 at 10:26

12 Answers 12

229

This worked for me in the pool configuration file (e.g. /etc/php-fpm.d/www.conf):

[www]
; Redirect worker stdout and stderr into main error log. If not set, stdout and
; stderr will be redirected to /dev/null according to FastCGI specs.
; Default Value: no
catch_workers_output = yes
Sign up to request clarification or add additional context in comments.

11 Comments

On Ubuntu 14 this file is located at /etc/php5/fpm/pool.d/www.conf
I guess the question then is where this elusive "main error log" is... Oh, I see, it's the value of php_admin_value[error_log] in that same config file - /var/log/php-fpm/www-error.log on CentOS 7, e.g.
sudo vi /etc/php/7.0/fpm/pool.d/www.conf for me
PHP is so confuse... there are a pre-created /var/log/php7.0-fpm.log so, php_admin_value[error_log] = /var/log/fpm-php.www.log will redirect to non-standard log??
@SudharshanNair , I don't think so. Server should be able to handle rotation
|
80

I struggled with this for a long time before finding my php-fpm logs were being written to /var/log/upstart/php5-fpm.log. It appears to be a bug between how upstart and php-fpm interact. See more here: https://bugs.launchpad.net/ubuntu/+source/php5/+bug/1319595

2 Comments

Thanks!! This was the key for me. I ended up symlinking the two files because I know I'm not going to remember this later: ln -sf /var/log/upstart/php5-fpm.log /var/log/php5-fpm.log
PHP is so confuse... there are a pre-created /var/log/php7.0-fpm.log so, php_admin_value[error_log] = /var/log/fpm-php.www.log will redirect to non-standard log??
69

I had a similar issue and had to do the following to the pool.d/www.conf file

php_admin_value[error_log] = /var/log/fpm-php.www.log
php_admin_flag[log_errors] = on

It still wasn't writing the log file so I actually had to create it by touch /var/log/fpm-php.www.log then setting the correct owner sudo chown www-data:www-data /var/log/fpm-php.www.log.

Once this was done, and php5-fpm restarted, logging was resumed.

7 Comments

that was actually the case! I even got some futher content appeared on site right after error :)
Thanks so much for this! This was the solution for me on Ubuntu 14.04.1 (Jessie), if it helps anyone else.
Some problem in a vagrant/debian wheezy environment.
Why?! This is still a problem on fpm-php7.
creating a subdirectory in /var/log, like "/var/log/fpm-php", then "chown www-data /var/log/fpm-php" (the user the nginx/php runs under), and setting "php_admin_value[error_log] = /var/log/fpm-php/error.log" to a file in that subdir should help when rebooting or log rotationg. As the directory permission are preserved and nginx/php can create the log file (and /var/log/ is cleaner...)
|
35

There are multiple php config files, but in the version of Ubuntu that I was running when I wrote this answer, THIS is the one you need to edit:

/etc/php(version)?/fpm/pool.d/www.conf

uncomment the line that says:

catch_workers_output

That will allow PHPs stderr to go to php-fpm's error log instead of /dev/null.

4 Comments

For those using docker your config files are located: /usr/local/etc/php-fpm.d/
With this conf, where goes the log, at /var/log/php7.0-fpm.log, at /var/log/fpm-php.www.log or other?
Where does it log to?
Scroll to the bottom of the www.conf file - the value of php_admin_value[error_log] shows where the log will be written to
30

I gathered insights from a bunch of answers here and I present a comprehensive solution:

So, if you setup nginx with php5-fpm and log a message using error_log() you can see it in /var/log/nginx/error.log by default.

A problem can arise if you want to log a lot of data (say an array) using error_log(print_r($myArr, true));. If an array is large enough, it seems that nginx will truncate your log entry.

To get around this you can configure fpm (php.net fpm config) to manage logs. Here are the steps to do so.

  1. Open /etc/php5/fpm/pool.d/www.conf:

    $ sudo nano /etc/php5/fpm/pool.d/www.conf

  2. Uncomment the following two lines by removing ; at the beginning of the line: (error_log is defined here: php.net)

    ;php_admin_value[error_log] = /var/log/fpm-php.www.log ;php_admin_flag[log_errors] = on

  3. Create /var/log/fpm-php.www.log:

    $ sudo touch /var/log/fpm-php.www.log;

  4. Change ownership of /var/log/fpm-php.www.log so that php5-fpm can edit it:

    $ sudo chown vagrant /var/log/fpm-php.www.log

    Note: vagrant is the user that I need to give ownership to. You can see what user this should be for you by running $ ps aux | grep php.*www and looking at first column.

  5. Restart php5-fpm:

    $ sudo service php5-fpm restart

Now your logs will be in /var/log/fpm-php.www.log.

7 Comments

I tried this, Still "fpm-php.www.log" is empty. Any idea?
Print your phpinfo() and see if these settings actually got picked up, @SudharshanNair.
@Gezim. thanks for your reply. my error log path is /var/log/fpm-php.www.log. Still this file is empty
@SudharshanNair, If log_errors errors is turned on the only other thing to check is permission and ownership of the file (step 4).
Ensure www-data is the user that the fpm process is running under: ps aux | grep php.*www. I don't have any other ideas. Comb through your output of phpinfo().
|
15

There is a bug https://bugs.php.net/bug.php?id=61045 in php-fpm from v5.3.9 and till now (5.3.14 and 5.4.4). Developer promised fix will go live in next release. If you don't want to wait - use patch on that page and re-build or rollback to 5.3.8.

Comments

8

In your fpm.conf file you haven't set 2 variable which are only for error logging.

The variables are error_log (file path of your error log file) and log_level (error logging level).

; Error log file
; Note: the default prefix is /usr/local/php/var
; Default Value: log/php-fpm.log

error_log = log/php-fpm.log

; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice

log_level = notice

1 Comment

isn't the default /usr/local/var/php instead of /usr/local/php/var? just speculating.
5

I'd like to add another tip to the existing answers because they did not solve my problem.

Watch out for the following nginx directive in your php location block:

fastcgi_intercept_errors on;

Removing this line has brought an end to many hours of struggling and pulling hair.

It could be hidden in some included conf directory like /etc/nginx/default.d/php.conf in my fedora.

2 Comments

I also have set fastcgi_intercept_errors to on. is that a bad thing then?? Why wouldn't you want this? Please explain!
I am sorry that I could not provide an explanation, since honestly I never fully understood myself why this directive prevented my error logging from working properly. Reading the docs, I wouldn't expect such an effect. In my case, it solved my problem on my dev machine. The bottom line is, if your configuration works for you as intended, all is good. Intercepting errors (not handing them to the client), because they may leak sensitive information, is something that you probably do want on a production server in most cases.
2

in my case I show that the error log was going to /var/log/php-fpm/www-error.log . so I commented this line in /etc/php-fpm.d/www.conf

php_flag[display_errors]   is commented
php_flag[display_errors] = on  log will be at /var/log/php-fpm/www-error.log

and as said above I also uncommented this line

catch_workers_output = yes

Now I can see logs in the file specified by nginx.

Comments

0

On alpine 3.15 with php8 i found on /var/log/php8/error.log

/var/log/php8 # cat error.log
 16:10:52] NOTICE: fpm is running, pid 14
 16:10:52] NOTICE: ready to handle connections

i also have this :

catch_workers_output = yes

Comments

-2

In my case php-fpm outputs 500 error without any logging because of missing php-mysql module. I moved joomla installation to another server and forgot about it. So apt-get install php-mysql and service restart solved it.

I started with trying to fix broken logging without success. Finally with strace i found fail message after db-related system calls. Though my case is not directly related to op's question, I hope it could be useful.

Comments

-6

Check the Owner directory of "PHP-FPM"

You can do:

ls -lah /var/log/php-fpm/
chown -R webusr:webusr /var/log/php-fpm/
chmod -R 777 /var/log/php-fpm/

1 Comment

never set /var/log/php-fpm to 777, you just made your server vulnerable to a symlink attack (if not worse)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.