Php fpm логи ошибок

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.

asked Dec 30, 2011 at 8:14

eidng8's user avatar

eidng8eidng8

2,0772 gold badges13 silver badges11 bronze badges

3

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

answered May 11, 2012 at 5:57

michaelbn's user avatar

michaelbnmichaelbn

7,4033 gold badges33 silver badges46 bronze badges

14

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

answered Jan 22, 2015 at 17:31

Code Commander's user avatar

Code CommanderCode Commander

16.8k8 gold badges64 silver badges65 bronze badges

2

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.

ᴍᴇʜᴏᴠ's user avatar

ᴍᴇʜᴏᴠ

4,8244 gold badges44 silver badges57 bronze badges

answered Apr 22, 2014 at 15:18

adnans's user avatar

adnansadnans

2,2792 gold badges15 silver badges5 bronze badges

7

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.

answered Aug 6, 2012 at 17:05

vector's user avatar

vectorvector

4874 silver badges6 bronze badges

4

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.

Dave Smith's user avatar

answered Nov 1, 2015 at 18:34

Gezim's user avatar

GezimGezim

7,11210 gold badges62 silver badges99 bronze badges

7

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.

answered Jun 28, 2012 at 18:03

Drewx's user avatar

DrewxDrewx

1511 silver badge2 bronze badges

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

Uyghur Lives Matter's user avatar

answered Feb 19, 2012 at 20:18

khizar ansari's user avatar

khizar ansarikhizar ansari

1,4762 gold badges18 silver badges29 bronze badges

1

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.

answered Mar 4, 2020 at 12:17

Arsylum's user avatar

ArsylumArsylum

5244 silver badges14 bronze badges

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.

answered Dec 26, 2015 at 8:35

enRaiser's user avatar

enRaiserenRaiser

2,6062 gold badges21 silver badges39 bronze badges

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.

answered Apr 22, 2020 at 16:28

user3132194's user avatar

user3132194user3132194

2,38123 silver badges17 bronze badges

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

answered Oct 4, 2022 at 14:28

uber's user avatar

uberuber

1143 bronze badges

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/

answered Jul 25, 2017 at 21:11

kalculator's user avatar

1

to get php-fpm errors in nginx error log you need:

as for nginx: it s enough just basic config

# cat default  | grep -v "#"


server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name _;
 
        index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

}

as for php-fpm the main setting is

log_errors = On

basically it s enough. but to be on the safe side i would suggest
to put the following block in php-fpm config(/etc/php/7.0/fpm/php.ini) :

[PHP]
...
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
html_errors = On

...

after that all php stack traces will be in

# tail /var/log/nginx/error.log 
2023/06/20 23:37:06 [error] 20307#20307: *1 FastCGI sent in stderr: "PHP message: PHP Parse error:  syntax error, unexpected '"asd"' (T_CONSTANT_ENCAPSED_STRING) in /var/www/html/php1.php on line 2" while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /php1.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "localhost"
2023/06/20 23:48:40 [error] 20307#20307: *5 FastCGI sent in stderr: "PHP message: PHP Parse error:  syntax error, unexpected '"asd"' (T_CONSTANT_ENCAPSED_STRING) in /var/www/html/php1.php on line 2" while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /php1.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "localhost"
2023/06/21 00:18:17 [error] 20307#20307: *7 FastCGI sent in stderr: "PHP message: PHP Parse error:  syntax error, unexpected '"asd"' (T_CONSTANT_ENCAPSED_STRING) in /var/www/html/php1.php on line 2" while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /php1.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "localhost:4545"

if you want to see php stack traces not in nginx erorr file but
instead in a separate file you need to add error_log setting
that is

error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = On
log_errors = On
error_log = /var/log/php-errors.log
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
html_errors = On

also very important to create /var/log/php-errors.log manually and set proper
permisssions because php-fpm wont do that and will continue to transfer error
to nginx. so

# touch /var/log/php-errors.log
# chown www-data.www-data /var/log/php-errors.log
# systemctl restart php7.0-fpm

after that nginx error log /var/log/nginx/error.log will be empty
but all php-errors will be in /var/log/php-errors.log

Docker

PHP FPM

First of all we need to enable option catch_workers_output for fpm.

catch_workers_output boolean
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.

/usr/local/etc/php-fpm.d/www.conf (in my configuration)

  • catch_workers_output = yes
sed -i '/^;catch_workers_output/ccatch_workers_output = yes' "/usr/local/etc/php-fpm.d/www.conf"

Or simply edit and save the file manually to uncomment line starting with ;catch_workers_output.

Then we need to configure log file names and locations.

Access log

If you want or need to activate access log at php level:

access.log string
The access log file. Default value: not set

  • access.log = /var/log/php/fpm-access.log
sed -i '/^;access.log/caccess.log = /var/log/php/fpm-access.log' "/usr/local/etc/php-fpm.d/www.conf"

Or simply edit and save the file manually to uncomment line starting with ;access.log.

You will have this kind of output:

$ tailf var/logs/php/fpm-access.log
172.18.0.5 -  20/Feb/2017:13:07:39 +0100 "GET /app_dev.php" 200
172.18.0.5 -  20/Feb/2017:13:07:47 +0100 "POST /app_dev.php" 302
172.18.0.5 -  20/Feb/2017:13:07:47 +0100 "POST /app_dev.php" 302
172.18.0.5 -  20/Feb/2017:13:07:47 +0100 "GET /app_dev.php" 200
172.18.0.5 -  20/Feb/2017:13:07:48 +0100 "GET /app_dev.php" 302
172.18.0.5 -  20/Feb/2017:13:07:48 +0100 "GET /app_dev.php" 200

Error log

Of course in production we do not want to display errors to users:

  • php_flag[display_errors] = off
sed -i '/^;php_flag\[display_errors\]/cphp_flag[display_errors] = off' "/usr/local/etc/php-fpm.d/www.conf"

Or simply edit and save the file manually to uncomment line starting with ;php_flag[display_errors].

Then we must enable error log and define the error log file location :

  • php_admin_value[error_log] = /var/log/php/fpm-error.log
  • php_admin_flag[log_errors] = on
sed -i '/^;php_admin_value\[error_log\]/cphp_admin_value[error_log] = /var/log/php/fpm-error.log' "/usr/local/etc/php-fpm.d/www.conf"
sed -i '/^;php_admin_flag\[log_errors\]/cphp_admin_flag[log_errors] = on' "/usr/local/etc/php-fpm.d/www.conf"

Or simply edit and save the file manually to uncomment lines starting with ;php_admin_value[error_log] and ;php_admin_flag[log_errors].

You will have this kind of output:

$ tailf var/logs/php/fpm-error.log
[20-Feb-2017 13:33:46 Europe/Paris] PHP Parse error:  syntax error, unexpected '8' (T_LNUMBER), expecting variable (T_VARIABLE) or '{' or '$' in /var/www/html/web/app_dev.php on line 26

You also could change log level:

log_level string
Error log level. Possible values: alert, error, warning, notice, debug. Default value: notice.

sed -i '/^;log_level/clog_level = error' "/usr/local/etc/php-fpm.d/www.conf"

Important

Log files must have correct access rights (owner) and must exist:

mkdir -p /var/log/php
touch /var/log/php/fpm-access.log
touch /var/log/php/fpm-error.log
chown -R www-data:www-data /var/log/php

PHP CLI

To enable php CLI errors, we need to add these lines into the (cli) php.ini file.

This configuration is for production not for debug or development.

error_reporting = E_ALL
display_startup_errors = Off
ignore_repeated_errors = Off
ignore_repeated_source = Off
html_errors = Off
track_errors = Off
display_errors = Off
log_errors = On
error_log = /var/log/php/cli-error.log

Conclusion

Using the given configuration you should have those logs:

$ ll var/logs/php              
total 256K
-rw-r--r-- 1 82 82    0 févr. 17 09:35 cli-error.log
-rw-r--r-- 1 82 82  64K févr. 20 13:34 fpm-access.log
-rw-r--r-- 1 82 82  186 févr. 20 13:33 fpm-error.log

Do NOT forget to enable log rotation, you will have:

$ ll var/logs/php              
total 256K
-rw-r--r-- 1 82 82    0 févr. 17 09:35 cli-error.log
-rw-r--r-- 1 82 82  390 févr. 17 09:35 cli-error.log-20170217
-rw-r--r-- 1 82 82  64K févr. 20 13:34 fpm-access.log
-rw-r--r-- 1 82 82  100 févr. 17 09:35 fpm-access.log-20170217.gz
-rw-r--r-- 1 82 82 172K févr. 18 02:00 fpm-access.log-20170218
-rw-r--r-- 1 82 82  186 févr. 20 13:33 fpm-error.log
-rw-r--r-- 1 82 82  374 févr. 17 09:35 fpm-error.log-20170217

PHP FPM handler can boost the website performance even on high traffic.

Sometimes, websites using PHP FPM show unexpected errors. And, the users may not have a clue on the underlying reason.

Fortunately, PHP fpm error reporting gives real-time details on the causes of the error. But, to make use of this we need to turn on error reporting for the website.

At Bobcares, we often get requests to fix PHP FPM errors as part of our Server Management Services.

Today, we’ll see how our Support Engineers set up PHP FPM error reporting and fix the related errors.

Importance of PHP FPM error reporting

PHP FPM error logging provides a simple but efficient solution for logging all errors into a log file. Also, the information they contain will give exact details of errors and the amount of time spent on tracking the root causes of errors.

For instance,

The entries in the PHP FPM error logs look like.

[22-Mar-2019 16:52:18] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 9 idle, and 89 total children

Luckily, it is very easy to troubleshoot after analyzing the PHP-FPM error log and our Support Engineers found that this error occurs due to the values of pm.min/max_spare_servers and pm.start_servers. Then we increased these two values in /etc/php-fpm.d/www.conf.

Similarly, another example of entries in the PHP FPM error logs.

[25-Apr-2019 16:28:20] WARNING: [pool www] server reached max_children setting (80), consider raising it

From the error log, we could identify that this error happens when pm.max_children setting reached its threshold value and our Support Engineers increased this value of pm.max_children in the PHP-FPM configuration file.

However, only a proper configuration of PHP FPM error reporting will provide useful entries to help fix the root cause of errors.

How we set up PHP FPM error reporting.

Now, let’s see how our Support Engineers set up PHP FPM error logging on servers.

To configure error logging for PHP-FPM,

1. Firstly, we need to configure log file names and location.

By default, we’re using www.conf pool config file or find php-fpm.conf or www.conf depending on what version of PHP-FPM you have installed. Here, we took /etc/php/7.0/fpm/pool.d/www.conf as an example.

2. So, we edit /etc/php/7.0/fpm/pool.d/www.conf file and uncomment “catch_workers_output=yes"

3. At last, we restart  php-fpm service.

How we nailed the errors related to PHP FPM error reporting

From our experience in managing server, we’ve seen that many customers had issues related to  PHP FPM error logging. Let’s take a look at the top problems and how we fix them.

Improper configuration settings

Recently, one of our customers had an issue PHP FPM error logging. He couldn’t find anything in the error log after enabled FPM error reporting.

Then, our Support Engineers found that he set up PHP-FPM error log in the wrong configuration file.

Therefore, we found the exact file location by using the following command.

ps aux | grep fpm

root 1508 0.0 1.5 367260 31380 ? Ss Nov05 0:11 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
www-data 10231 0.0 2.7 453420 55540 ? S 15:10 0:03 php-fpm: pool www
www-data 13266 0.0 2.4 449892 50900 ? S 22:13 0:00 php-fpm: pool www
www-data 13572 0.0 1.8 372468 37740 ? S 23:14 0:00 php-fpm: pool www
user+ 13721 0.0 0.0 14512 980 pts/0 R+ 23:30 0:00 grep --color=auto fpm

Next, we edited /etc/php/7.0/fpm/php-fpm.conf and properly configured it.

That fixed the problem.

Incorrect permission/missing log file

Sometimes, PHP FPM error logging may not work as we expect even if we set up the right configuration. This can happen due to missing log files or lack of write permissions for a webserver on the log file.

For example, if the permission of error log fpm-php.www.log is incorrect, the error logging will not work properly. The same thing happens in the case of missing log files.

When a customer reported problems with error logging we executed the following command to verify the files. But, files were missing as per the logs below.

ls /usr/local/etc/php-fpm.d/fpm.log
ls: cannot access '/usr/local/etc/php-fpm.d/fpm.log': No such file or directory
ls: cannot access '/usr/local/etc/php-fpm.d': No such file or directory

Then our Support Engineers created the directory and log file fpm.log. And, then set up the right ownership.

This is how we fixed the error.

[Having trouble while setting up PHP FPM error reporting? We’ll fix it for you.]

Conclusion

In short, PHP FPM error logging provides a simple but efficient solution for logging all errors into a log file. Today, we saw how our Support Engineers set up PHP FPM error reporting and solved related issues.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = «owonCMyG5nEQ0aD71QM»;

In this blog i will explain how to configure the access, error and slow logs of php-fpm and to modify the format in which the logs are received.

Prerequisites:

Lets have an application that uses the php5-fpm module, in this case, we will use the WordPress Application running on a LEMP stack.

To enable the logs , two files are relevant, namely

i) /etc/php5/fpm/php-fpm.conf
ii) /etc/php5/fpm/pool.d/www.conf

Firstly, open the first file for editing

[js]vi /etc/php5/fpm/php-fpm.conf[/js]

Set the desired location for getting error logs. The line should look like:
error_log = /var/log/php5/error.log
Then save and exit

Next open the second file for editing

[js] vi /etc/php5/fpm/pool.d/www.conf [/js]

Look for the line containing the location of access.log and set it to the desired path.
ex: access.log = /var/log/php5/access.log

Understanding the access log format:

access.format = %R – %u %t \”%m %r%Q%q\” %s %f %{mili}d %{kilo}M %C%%”

%R : remote IP address
%u : remote user (you dont see these two in output of tail -f)

%t : server time of receiving request
%m : method
%r : request uri {Addidtional : %q:if query string exists, %Q: the ? character is query
string exists }
%s: status response
%f : script filename
%d : time taken to serve request (in mili seconds)
%M : peak memory allocated by php (in kilobytes)
%C : CPU used by request

Example:

[js]
%t %m %r %s %f %d %M %C
17/May/2015:09:41:04 +0000 «GET /wp-admin/install.php» 200 /var/www/wp-admin/install.php 218.177 2816 87.09%
[/js]

The format of the access log can be altered as per desire.

The path for slow log is defined is defined just beneath the block of access logs. Uncomment the line and set the desired path for getting slow logs.
slowlog = /var/log/php5/slow.log
Make sure to set the request_slowlog_timeout to the desired value. The line should look like :

Ex: request_slowlog_timeout = 10

At the bottom of the file set
php_admin_value[error_log] = /var/log/php5/error.log ##path of error.log
php_admin_flag[log_errors] = on

Create the directory(if it does not exist) where the logs are to be created.

[js]mkdir -p /var/log/php5[/js]

Restart the php5-fpm service

[js]service php5-fpm restart[/js]

Henceforth you will start getting the logs at the set path.

Понравилась статья? Поделить с друзьями:

Интересное по теме:

  • Php включить сообщение об ошибках
  • Php file get contents ошибки
  • Php exec ошибка 1
  • Php nginx отображение ошибок
  • Php включить отображение всех ошибок

  • Добавить комментарий

    ;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: