You can also do it from your project root index file. Put the following file on your project index.php.

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

To configure PHP on Ubuntu to show errors and enable error reporting in Apache, follow these steps:

1. Locate the php.ini File

The php.ini file contains configuration settings for PHP. The location depends on the PHP version and your server setup. Common locations include:

  • /etc/php/<version>/apache2/php.ini (for Apache)
  • /etc/php/<version>/cli/php.ini (for the CLI)

Replace <version> with your PHP version, such as 8.2.

To find the exact location:

bash
php --ini

2. Edit the php.ini File

Use a text editor like nano to edit the file:

bash
sudo nano /etc/php/<version>/apache2/php.ini

3. Enable Error Reporting

Search for the following settings and modify them accordingly:

  • Display Errors
    ini
    display_errors = On
  • Error Reporting Level For all errors (including warnings, notices, and strict standards):
    ini
    error_reporting = E_ALL

Save and exit the editor (Ctrl + O, then Ctrl + X in nano).

4. Enable Error Reporting for Development (Optional)

To display errors directly in your scripts for development purposes, you can add this code at the start of your PHP files:

php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

5. Restart Apache

Apply the changes by restarting the Apache server:

bash
sudo systemctl restart apache2

6. Verify Error Reporting

Create a PHP script (e.g., test.php) with the following content to test error reporting:

php
<?php
echo $undefined_variable; // This will generate a notice
?>

Access the script in your browser (http://your-domain/test.php). You should see the error message.

Notes:

  • Security Consideration: Avoid enabling error display on production servers, as it might expose sensitive information. Instead, log errors to a file using:
    ini
    log_errors = On
    error_log = /var/log/php_errors.log
  • Advanced Error Reporting: You can also use debugging tools like Xdebug for more detailed error analysis.