How to Enable Error Logging on PHP-FPM 7 with Nginx

Posted on

How to Enable Error Logging on PHP-FPM 7 with Nginx

This guide outlines the steps to enable error logging for PHP-FPM 7 when used in conjunction with Nginx web server. Proper error logging is crucial for debugging and identifying issues within your PHP applications.

Steps to Enable PHP-FPM Error Logging:

  1. Locate the PHP-FPM Configuration File:

    The PHP-FPM configuration file is typically named php-fpm.conf or php-fpm.ini and resides in a directory such as /etc/php/7.x/fpm/ or /usr/local/etc/php/7.x/fpm/. Replace 7.x with your specific PHP version.

    Common locations:

    • /etc/php/7.x/fpm/php-fpm.conf
    • /etc/php/7.x/fpm/php.ini
    • /usr/local/etc/php/7.x/fpm/php-fpm.conf
    • /usr/local/etc/php/7.x/fpm/php.ini
  2. Configure Error Logging in php.ini:

    Edit the php.ini file (often referenced from within php-fpm.conf) and set the following directives:

    error_reporting = E_ALL
    log_errors = On
    error_log = /path/to/your/php-fpm-errors.log
    

    Replace /path/to/your/php-fpm-errors.log with the desired path to your log file. Ensure the user running PHP-FPM (usually www-data or nginx) has write access to this file and its directory.

    • error_reporting = E_ALL: This sets the error reporting level to report all errors, warnings, and notices.
    • log_errors = On: This enables error logging.
    • error_log = /path/to/your/php-fpm-errors.log: This specifies the path to the error log file.
  3. Verify Pool Configuration (www.conf or similar):

    Navigate to the pool configuration file (usually www.conf or similar, located in /etc/php/7.x/fpm/pool.d/ or a similar directory). Ensure error logging is configured within the pool. Check for specific overrides that might disable or redirect the logs configured in php.ini.

    ; Example www.conf snippet
    [www]
    ; ... other settings ...
    php_flag[display_errors] = off
    php_admin_value[error_log] = /path/to/your/php-fpm-pool-errors.log
    php_admin_flag[log_errors] = on
    php_admin_value[error_reporting] = E_ALL
    

    Again, ensure the appropriate user has write access to the log file.

  4. Restart PHP-FPM:

    Restart the PHP-FPM service to apply the changes.

    sudo systemctl restart php7.x-fpm  # Replace 7.x with your version
    

    Or, depending on your system:

    sudo service php7.x-fpm restart # Replace 7.x with your version
    
  5. Configure Nginx:

    While PHP-FPM handles PHP execution errors, Nginx can also log errors related to serving the requests. Check your Nginx virtual host configuration (usually in /etc/nginx/sites-available/) and ensure you have error logging enabled.

    server {
        # ... other settings ...
        error_log /path/to/your/nginx-error.log error;
        # ...
    }
    

    Replace /path/to/your/nginx-error.log with the desired path.

  6. Restart Nginx:

    Restart Nginx to apply the changes.

    sudo systemctl restart nginx
    

    Or, depending on your system:

    sudo service nginx restart
    
  7. Test and Verify:

    Create a simple PHP script with an intentional error and access it through your web browser. Check both the PHP-FPM error log (/path/to/your/php-fpm-errors.log) and the Nginx error log (/path/to/your/nginx-error.log) to confirm that the error is being logged correctly.

Important Considerations:

  • Permissions: Ensure the user running PHP-FPM has write access to the error log file and its directory. This is often the www-data or nginx user.
  • Security: Do *not* display errors directly to the user in a production environment. This can expose sensitive information. Use error logging for debugging only.
  • Log Rotation: Implement a log rotation mechanism (e.g., logrotate) to prevent your error logs from consuming excessive disk space.
  • Debugging: Use the error logs to identify and resolve issues within your PHP applications.

Leave a Reply

Your email address will not be published. Required fields are marked *