Log Errors on PHP Linux

How to log errors with vanilla PHP on Linux

html

<?php
ini_set("display_errors", 0);
ini_set("log_errors", 1);
ini_set('error_log', '/opt/lampp/htdocs/myproject/log/error.log');
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <?php echo $undefined_variable; ?>
</body>

</html>

bash

=============================================================================================
1. Create your log folder and file.

cmd: mkdir -p /opt/lampp/htdocs/myproject/logs
cmd: touch /opt/lampp/htdocs/myproject/logs/error.log

=============================================================================================
2. Set ownership to Apache user (daemon for XAMPP).

cmd: sudo chown -R daemon:daemon /opt/lampp/htdocs/myproject/logs

note: Apache runs as daemon, so it needs ownership to write logs.

=============================================================================================
3. Set secure permissions.

cmd: sudo chmod 750 /opt/lampp/htdocs/myproject/logs
cmd: sudo chmod 640 /opt/lampp/htdocs/myproject/logs/error.log

| Path        | Permission | Meaning                                     |
| ----------- | ---------- | ------------------------------------------- |
| `logs/`     | `750`      | `daemon` can read/write/enter, others cant |
| `error.log` | `640`      | `daemon` can write, others can’t access it  |

=============================================================================================
4. Set up PHP error logging in your script.

<?php
ini_set("display_errors", 0); // Hide errors from the screen
ini_set("log_errors", 1);     // Enable logging
ini_set("error_log", "/opt/lampp/htdocs/myproject/logs/error.log"); // Custom log path
?>

=============================================================================================
5. Restart Apache.

cmd: sudo /opt/lampp/lampp restart

Apache needs to reload configuration and file permissions.

=============================================================================================
6. Test logging.

Add this to your PHP page to trigger a warning:

<?php echo $undefined_variable; ?>

=============================================================================================
7. Optional: Give yourself read access.

Check if your are in the groud.
cmd: groups

Example of what your should see.
yourusername daemon adm cdrom sudo dip www-data plugdev lpadmin sambashare

If your user isn’t in the daemon group, but you still want to read the log:

cmd: sudo chmod g+r /opt/lampp/htdocs/myproject/logs/error.log

Or add yourself to the daemon group (requires logout/login):

cmd: sudo usermod -aG daemon yourusername

=============================================================================================
Note.

If daemon dont work, check what apache is using.
cmd: ps aux | grep httpd

Example of what you should see:
root       14778  0.4  0.7 345472 27508 ?        Ss   13:56   0:00 /opt/lampp/bin/httpd -k start -E /opt/lampp/logs/error_log -DSSL -DPHP
daemon     14785  0.0  0.3 339700 11444 ?        S    13:56   0:00 /opt/lampp/bin/httpd -k start -E /opt/lampp/logs/error_log -DSSL -DPHP
daemon     14820  0.0  0.4 346008 15748 ?        S    13:56   0:00 /opt/lampp/bin/httpd -k start -E /opt/lampp/logs/error_log -DSSL -DPHP

In my case apache run with daemon, But if you see.
root       14778  0.4  0.7 345472 27508 ?        Ss   13:56   0:00 /opt/lampp/bin/httpd -k start -E /opt/lampp/logs/error_log -DSSL -DPHP
www-data     14785  0.0  0.3 339700 11444 ?        S    13:56   0:00 /opt/lampp/bin/httpd -k start -E /opt/lampp/logs/error_log -DSSL -DPHP
www-data     14820  0.0  0.4 346008 15748 ?        S    13:56   0:00 /opt/lampp/bin/httpd -k start -E /opt/lampp/logs/error_log -DSSL -DPHP

Change daemon for www-data.
cmd: sudo chown -R www-data:www-data /opt/lampp/htdocs/myproject/logs
cmd: sudo usermod -aG www-data yourusername

Remember to close session.

add this: (Require all denied) to your .htaccess in your log directory