188

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
    – astroanu
    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.
    – scones
    Commented Mar 1, 2017 at 10:26

12 Answers 12

225

This worked for me:

; 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

Edit:

The file to edit is the file that configure your desired pool. By default its: /etc/php-fpm.d/www.conf

13
  • 7
    <strike>In what config file? php.ini? php-fpm.conf?</strike>. I uncommented it inside my /etc/php-fpm.d/www.conf
    – Swivel
    Commented Jul 9, 2013 at 23:36
  • 19
    On Ubuntu 14 this file is located at /etc/php5/fpm/pool.d/www.conf Commented Jun 22, 2015 at 16:05
  • 2
    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. Commented Aug 1, 2017 at 0:55
  • 9
    sudo vi /etc/php/7.0/fpm/pool.d/www.conf for me Commented Aug 17, 2017 at 18:55
  • This was the right way for me, on Debian 9.1 and PHP-FPM 7.0
    – Antwane
    Commented Aug 30, 2017 at 8:19
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
  • 11
    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
    – njbair
    Commented Feb 15, 2015 at 23:15
  • 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?? Commented Feb 18, 2018 at 13:57
68

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
  • that was actually the case! I even got some futher content appeared on site right after error :)
    – holms
    Commented Jun 24, 2014 at 11:42
  • Thanks so much for this! This was the solution for me on Ubuntu 14.04.1 (Jessie), if it helps anyone else. Commented Feb 16, 2015 at 19:34
  • Some problem in a vagrant/debian wheezy environment.
    – Xosofox
    Commented Mar 8, 2016 at 14:05
  • 1
    Why?! This is still a problem on fpm-php7. Commented Jan 19, 2018 at 13:59
  • 1
    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...)
    – A. Rabus
    Commented Jul 7, 2018 at 17:05
35

There are multiple php config files, but 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
  • 6
    For those using docker your config files are located: /usr/local/etc/php-fpm.d/
    – Edward
    Commented Jun 27, 2017 at 10:27
  • With this conf, where goes the log, at /var/log/php7.0-fpm.log, at /var/log/fpm-php.www.log or other? Commented Feb 18, 2018 at 13:58
  • Where does it log to? Commented Aug 18, 2019 at 9:34
  • 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
    – Mike
    Commented Jul 1, 2021 at 8:45
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
  • 3
    I tried this, Still "fpm-php.www.log" is empty. Any idea? Commented Aug 24, 2018 at 5:39
  • Print your phpinfo() and see if these settings actually got picked up, @SudharshanNair.
    – Gezim
    Commented Aug 27, 2018 at 18:14
  • @Gezim. thanks for your reply. my error log path is /var/log/fpm-php.www.log. Still this file is empty Commented Aug 28, 2018 at 6:31
  • @SudharshanNair, If log_errors errors is turned on the only other thing to check is permission and ownership of the file (step 4).
    – Gezim
    Commented Aug 29, 2018 at 12:54
  • 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().
    – Gezim
    Commented Aug 29, 2018 at 13:00
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.

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
  • isn't the default /usr/local/var/php instead of /usr/local/php/var? just speculating.
    – n611x007
    Commented Aug 24, 2015 at 10:01
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

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.

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
-1

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.

-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
  • 7
    never set /var/log/php-fpm to 777, you just made your server vulnerable to a symlink attack (if not worse) Commented Apr 10, 2018 at 8:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.