How to enable PHP error logging in Nginx with PHP-FPM?

To enable PHP error logging in Nginx with PHP-FPM, you need to follow these steps:

  1. Open the php.ini file for editing. Usually, it can be found in the /etc/php//fpm directory. For example, for PHP version 7.4, the file would be /etc/php/7.4/fpm/php.ini.

  2. Uncomment or add the following line in the php.ini file to enable error logging:

    error_log = /var/log/php/error.log

    This line specifies the file location where PHP errors will be logged. You can modify the file location and name according to your preference.

  3. Create the error.log file and give it appropriate permissions by running the following commands:

    sudo touch /var/log/php/error.log sudo chown www-data:www-data /var/log/php/error.log

    Replace www-data:www-data with your web server user and group if different.

  4. Restart the PHP-FPM service using the appropriate command depending on your system:

    sudo service php7.4-fpm restart

    Replace php7.4-fpm with the correct PHP-FPM service name for your PHP version.

  5. Reload or restart Nginx for the changes to take effect:

    sudo service nginx reload

    Alternatively, you can use the restart command instead of reload:

    sudo service nginx restart
  6. Verify that PHP error logging is working by intentionally causing a PHP error in your application. The error should now be logged in the specified error.log file.

Note: Make sure you have the necessary permissions to write to the error log file and that the file location is accessible by the PHP-FPM process.