Introduction
When your PHP-FPM application misbehaves or runs into performance bottlenecks, logs are often your first clue. By default, PHP-FPM errors may appear in Nginx’s error log (depending on your configuration). In this guide, we’ll show you how to locate, view, and interpret these logs, and we’ll also explore best practices for logging in a PHP-FPM + Nginx environment.
1. Understanding Nginx Logs
Nginx maintains two primary log files by default:
- Error Log: Records issues encountered by the Nginx server, including PHP-FPM errors, configuration problems, and runtime exceptions.
- Access Log: Contains a record of every request to your server, including HTTP status codes, client IP addresses, and user-agents.
On many Linux distributions, these files are located in /var/log/nginx
:
/var/log/nginx/error.log
– The default error log./var/log/nginx/access.log
– The default access log.
2. Viewing the Logs
The simplest way to view recent log entries is with the tail
command. For example, to view the last 200 lines of the Nginx error log:
sudo tail -n 200 /var/log/nginx/error.log
To view the last 200 lines of the access log instead:
sudo tail -n 200 /var/log/nginx/access.log
If you want to watch new log entries in real time, use the -f
(follow) option:
sudo tail -f /var/log/nginx/error.log
3. PHP-FPM Logs
In many setups, PHP-FPM errors also appear in the Nginx error log. However, PHP-FPM can maintain a separate log file too. The location depends on your distro or custom configuration. Common defaults include:
/var/log/php7.4-fpm.log
(Ubuntu/Debian, for PHP 7.4 pools)/var/log/php-fpm/error.log
(CentOS/Fedora, generic PHP-FPM log)
Check your PHP-FPM pool configuration (often stored in /etc/php/*/fpm/pool.d/
or similar) for directives like:
php_admin_value[error_log] = /var/log/php-fpm/pool-error.log
php_admin_flag[log_errors] = on
This determines where errors for that specific PHP-FPM pool are written. By default, many distributions pipe these errors to the Nginx error log.
4. Common Log Entries and What They Mean
Reading logs can be intimidating if you’re not used to them. Here are a few common error types you might encounter:
- 502 Bad Gateway Errors: Often mean PHP-FPM is not running, crashed, or is misconfigured (socket or port mismatch).
- Permission Denied: Indicates PHP-FPM or Nginx doesn’t have the correct file or folder permissions (SELinux or file permissions can be the culprit).
- Timeout Errors: Occur if PHP-FPM scripts exceed max execution time or Nginx’s proxy_read_timeout is too low for long-running requests.
- Upstream Sent Too Big Header: Usually caused by sending large cookies or data, requiring an increase in
fastcgi_buffer_size
orfastcgi_buffers
.
5. Enabling More Detailed Logging
If basic logs aren’t revealing enough information, you can enable more verbose logging. Keep in mind that debug logging can be very noisy and may impact performance. Use it temporarily:
On Nginx: Set error_log /var/log/nginx/error.log debug;
in your server or http block:
http {
# ...
error_log /var/log/nginx/error.log debug;
# ...
}
Then reload Nginx:
sudo systemctl reload nginx
On PHP-FPM: Increase log_level
in php-fpm.conf
or your pool config:
; Possible values: alert, error, warning, notice, debug
log_level = debug
Then restart PHP-FPM:
sudo systemctl restart php7.4-fpm # or php-fpm, depending on your distro
6. Rotating and Managing Log Files
Logs can quickly grow large if your site is high-traffic or you’ve enabled debug logging. Tools like logrotate
are typically configured by default on most Linux distros to rotate logs and prevent them from consuming all available disk space. You can customize rotation schedules in /etc/logrotate.d/
.
7. Summary
Nginx and PHP-FPM logs provide valuable insights into your server’s health and performance. You can find your errors, warnings, and debugging messages in the following locations by default:
- Nginx Error Log:
/var/log/nginx/error.log
- Nginx Access Log:
/var/log/nginx/access.log
- PHP-FPM Log: Often
/var/log/php-fpm/error.log
or appended to the Nginx error log
By using tail
or watching logs in real time, you can quickly pinpoint issues, fix misconfigurations, or diagnose performance constraints. If basic logs aren’t enough, enable debug logs for deeper insights—then remember to revert to normal levels once troubleshooting is complete.
Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.
yes, it helped