Learn how to enable error logging on PHP-FPM 7 with Nginx. Our Nginx Support team is here to help you with your questions and concerns.
How to Enable Error Logging on PHP-FPM 7 with Nginx
Debugging PHP issues can be time-consuming, especially when error messages are missing or hidden behind server misconfigurations. One common issue developers face is PHP-FPM failing to write to the log file.
This blog will examine the causes, diagnosis, and step-by-step solutions to getting error reporting working when using Nginx with PHP-FPM.
Common Cause: PHP-FPM Lacks Write Permission
A frequent cause of silent failures is PHP-FPM not having permission to write to the log file. If the log file or its directory doesn’t exist or is owned by the wrong user, PHP won’t be able to output errors as expected.
To identify what’s going wrong, try printing the current PHP error logging configuration by adding this to the script:
print("\n\n<br><br>log_errors: ".ini_get('log_errors'));
print("\n\n<br><br>error_log: ".ini_get('error_log'));
This might return something like:
log_errors: 1
error_log: /usr/local/etc/php-fpm.d/fpm.log
Once we know the path to the log file, check whether it exists:
ls /usr/local/etc/php-fpm.d/fpm.log
ls /usr/local/etc/php-fpm.d/
If we see errors like:
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
This means that PHP-FPM is trying to log on to a non-existent path.
Solution
- First, create the missing directory and log file:
cd /usr/local/etc
sudo mkdir php-fpm.d
cd php-fpm.d
sudo touch fpm.log
- Then, set the correct ownership:
sudo chown -R www-data:www-data fpm.log
- If the file exists but is owned by the root, check ownership:
ls -l fpm.log
We might see:
-rw-r--r-- 1 root root 0 Mar 12 14:32 fpm.log
So, update ownership:
sudo chown -R www-data:www-data fpm.log
It should now look something like:
-rw-r--r-- 1 www-data www-data 12848 Mar 12 14:55 fpm.log
If you’re building a complex stack, it may help to learn more about how APC caching integrates with PHP-FPM and Nginx to improve performance.
Where Are the Errors?
We may not see errors in our browser even with proper PHP error settings. Nginx, by default, sends PHP errors to a log file. We can find that log here:
/var/log/nginx/error.log
This file typically contains all our PHP errors when we are running Nginx.
If errors aren’t visible in your browser, it’s likely due to PHP configuration. Nginx won’t automatically display PHP errors unless explicitly told to do so.
Here’s how to enable on-screen error reporting:
ini_set('display_errors', 1);
error_reporting(E_ALL);
Our experts suggest using these settings in development. Displaying errors in production can leak sensitive information.
Furthermore, to set these values globally, edit the `php.ini` file as shown below:
display_errors = On
error_reporting = E_ALL
We can also redirect errors to stdout or stderr:
display_errors = stdout
Additionally, setting only one of these won’t guarantee that errors are shown. We must set both `display_errors` and `error_reporting` to appropriate values.
If you’re setting up PHP-FPM on platforms like Drupal, check out this guide on configuring PHP-FPM with Nginx on Ubuntu for Drupal.
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
Getting error reporting to work with PHP-FPM and Nginx can be tricky, especially when permissions or paths are misconfigured. By checking PHP’s error settings, verifying log file paths, and ensuring correct permissions, we can quickly get our development environment behaving.
In brief, our Support Experts demonstrated how to enable error logging on PHP-FPM 7 with Nginx.
Want to explore more benefits of using PHP-FPM? Read our post on the advantages of PHP-FPM in modern web hosting environments.
0 Comments