Displaying PHP Errors
The software installation instructions configured a LAMP (Linux, Apache, MySQL, and PHP) stack that you can use for web development, including server-side scripting and database connectivity. Regrettably, PHP’s default configuration disables error reporting, which is recommended for production deployment but not for development. In fact, error reporting is essential when learning PHP because otherwise syntax errors simply result in a blank web page!
To enable PHP error reporting for Apache, you must do the following:
-
Modify the PHP configuration (
php.ini
)Open
/etc/php/7.2/apache2/php.ini
using a text editor (e.g.,gedit
, which is text editor that you’ve probably been using by default, orvi
). Modifying this file requires administrative privileges so prefix your comment usingsudo
– e.g.,sudo gedit /etc/php/7.2/apache2/php.ini
You will be prompted for your password before the file is opened.
Make the following change to that file:
--- /etc/php/7.2/apache2/php.ini 2021-03-25 08:03:27.521707388 -0600 +++ /etc/php/7.2/apache2/php.ini 2021-03-25 08:03:49.814555381 -0600 @@ -471,7 +471,7 @@ ; Development Value: On ; Production Value: Off ; http://php.net/display-errors -display_errors = Off +display_errors = On ; The display of errors which occur during PHP's startup sequence are handled ; separately from display_errors. PHP's default behavior is to suppress those
That is, you will change “Off” to “On” and then save the file. (You may want to open the file again to make sure that your change was saved.)
-
Reload the Apache web server using the following command:
sudo service apache2 reload
-
Verify that errors are being reported. Create a PHP file named
error.php
in yourpublic_html
directory with the following contents:<?php echo "Error!';
The syntax error (i.e., terminating the string with a single quote instead of a double quote) is intentional. Use your web browser to access that file by entering
http://localhost/~student/error.php
(and remember to replace “student” with your username!).You should see the following error message:
Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in /home/student/public_html/error.php on line 5
While it may not seem like the most informative error message, at least it’s clear that the script contains a syntax error.
If you only see a white screen (i.e., a blank web page), then error reporting is not configured correctly. Contact the instructor for assistance.
Ideally this configuration should have occurred when installing PHP so if anyone wants to contribute a pull request with the change (see corresponding issue), it would certainly qualify for extra credit.