Debugging and fixing PHP and Drupal errors

Fixing and debugging PHP and Drupal errors can sometimes be hard, especially if you don't know where to look for information.

On a properly configured server, a php error only result in a general message that an error has occured without additional information. To find a solution, you first need more information. For this, you normally need server access, to be able to access the logs for the webserver and php.

The first place to look is the webserver logs. On linux, depending on your configuration, your webserver will put logs in /var/logs. Often those logs are split per application/domain name. If nginx is used as webserver, then look in /var/logs/nginx. In there you will find files with <domainname>.error.log and <domainname>.access.log. It is the error log you need normally. For the wiki for instance the file will be /var/log/nginx/

You can view this file with the command tail, which will show the last lines of the file. Log files always start with the oldest log message and the newest ones are at the end, so that is usually where you want to look. With "tail -n 20 /var/log/nginx/" you will show the last 20 lines of that file. With "tail -n 20 /var/log/nginx/|less" you'll see the same last 20 lines, but now you can scroll up and down with the arrow keys. Exit by pressing q.

Errors often start with the date and time of the error and contain a full traceback of what error occurred where in the code. It is good to look at the top and the bottom. For instance, the following error occurred after updating drupal on the acceptance server:

2019/03/13 13:00:30 [error] 27350#0: *49 FastCGI sent in stderr: "PHP message: Uncaught PHP Exception Twig\Error\SyntaxError: "An exception has been thrown during the compilation of a template ("Attribute "name" does not exist for Node "Twig\Node\CheckToStringNode".") in "themes/contrib/fuwiki_theme/templates/node/node.html.twig"." at /srv/www/vhosts/wikiacc_nightowl_lan/vendor/twig/twig/src/Environment.php line 797" while reading response header from upstream, client:, server: wikiacc.nightowl.lan, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm/php-fpm.sock:", host: "wikiacc.nightowl.lan"

Here we see the date and time, the fact that it was a PHP error caused by TWIG (the templating language used by drupal), and the template themes/contrib/fuwiki_theme/templates/node/node.html.twig that apparently tries to use the Name attribute of Twig\Node\CheckToStringNode. So this is your start point for googling. Often it helps to just copy paste your error into google. But take into account that ip addresses, domain names, file locations, etc might be different. Others definitely don't use our fuwiki_theme for their website, so you won't find the literal same error. After googling the error I found that this was an issue between twig 1.38 and Drupal, so downgrading twig was a temporary workaround until Drupal and Twig solved the issue between them.

Additionally, for Drupal, you can also check the drupal logs in /admin/reports/dblog. There you can see errors that are more specifically related to Drupal.

Lastly, linux has it's own logging system for applications called the "journal". You can check those errors with "journalctl -xe" which will show you the most recent errors and allows you to scroll back up to older errors. (Exist with q).

Relevant Specialties

This page is subject to our Disclaimer.