PHP Log All Errors to a Log File to Get Detailed Information

See all PHP related FAQ
PPHP offers a simple but effective solution to log all errors to a log file. On all production web server, you must turn off displaying an error to end users via a web browser. Remember PHP gives out lots of information about the path, database schema and all other sorts of sensitive information. You are strongly advised to use error logging in place of error displaying on production web sites. The idea is quite simple only developer should able to see PHP error log.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux or Unix terminal
Category Web Server
OS compatibility BSD Linux Unix
Est. reading time 3 minutes

Find out path to php.ini

The default paths are as follows for popular unix like system:

  • CentOS Linux/RHEL v5.x/6.x/7.x/8.x/9.x : /etc/php.ini
  • Ubuntu / Debian Linux (PHP 7 fpm) : /etc/php/7.0/fpm/php.ini
  • Ubuntu / Debian Linux (PHP 5.6 fpm) : /etc/php/5.6/fpm/php.ini
  • Ubuntu / Debian Linux (PHP 8.1 fpm) : /etc/php/8.1/fpm/php.ini
  • Ubuntu / Debian Linux (PHP 7 with Apache 2) : /etc/php/5.6/apache2/php.ini
  • Ubuntu / Debian Linux (PHP 5.6 with Apache 2) : /etc/php/7/apache2/php.ini
  • FreeBSD unix server v9.x/10.x/11.x/12.x/13.x+: /usr/local/etc/php.ini

You can use the following command to find out path to php.ini. For example:
# php5-cgi -i | grep php.ini
Sample outputs:

<tr><td class="e">Loaded Configuration File </td><td class="v">/etc/php5/cgi/php.ini </td></tr>

# php-fpm5.0 -i | grep php.ini
# php-fpm7.0 -i | grep php.ini
# php-fpm8.1 -i | grep php.ini

Sample outputs from my PHP version 8.1 on Ubuntu LTS 22.04 server:

Configuration File (php.ini) Path => /etc/php/8.1/fpm
Loaded Configuration File => /etc/php/8.1/fpm/php.ini

How do I log all php errors to a log file?

Just add following line to /etc/php.ini to log errors to specified file, say /var/log/php-scripts.log or /var/log/httpd/php-error.log. For example, use a text editor such as vi/vim or nano:
# vi /etc/php.ini
Find and modify the error_log directive as follows:
error_log = /var/log/php-scripts.log
Make sure display_errors set to Off (no errors to end users):
display_errors = Off
Save and close the file. Restart the Apache web server or restart php7-fpm/php5-fpm service as follows as per your distors variant:
# /etc/init.d/httpd restart
# systemctl restart httpd
$ sudo systemctl restart php7.0-fpm.service
$ sudo systemctl restart php-fpm

How do I log errors to syslog or Windows Server Event Log?

Modify error_log as follows :
error_log = syslog

How do I see logs?

Login using ssh or download a log file /var/log/php-scripts.log using sftp client. For example, use the tail command as follows:
$ sudo tail -f /var/log/php-scripts.log
Sample outputs:

Fig.01: PHP error log output

Fig.01: PHP error log output

Use the du command or df command to disk space usage on your Linux and Unix server for log file. For example:
$ df -h /var/log/
$ sudo du -chs /var/log

A note about php-fpm users

You need to edit the php-fpm config file such as /etc/php81/php-fpm.d/www.conf. Append the following lines:

php_flag[display_errors] = off                                                    
php_admin_value[error_log] = /var/log/nginx/php.error.log                         
php_admin_flag[log_errors] = on
Restart the php-fpm service. For example, here is how I restarted it under Alpine Linux VM or Docker images:
# /etc/init.d/php-fpm81 restart
## OR use the service command ##
# service php-fpm81 restart

 * Stopping PHP FastCGI Process Manager ...  [ ok ]
 * Checking /etc/php81/php-fpm.conf ...
 * Starting PHP FastCGI Process Manager ...  [ ok ]

Summing up

And that is how you log all error messages to a log file when using PHP server-side programming language. This is useful for troubleshooting issues with your web apps created in PHP under Linux, BSD and Unix-like cloud servers.

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

33 comments… add one
  • Pavarti Izdelava Jul 19, 2012 @ 7:52

    Thank you for that post, it is working on my server.
    I have additional question, how do you display friendly error to user, when fatal error occur.

  • Divya Oct 4, 2012 @ 9:58

    What can we do in case we need to delete log file entries older than 60 days or clean up logs older than 60 days in php?

  • Dmitry Pashkevich Apr 26, 2013 @ 22:33

    If you redirect error output to a custom file, you’d have to care about log rotation yourself or it’ll grow infinitely.
    If error logging is handled by the web server (default), then your web server will likely take care of that.

  • Ashish Kumar Laxkar Jul 26, 2013 @ 10:05

    Really helpful i have that problem from past 2 days now i resolve that… Thanks 4 support.. any update please mail….

  • Heidi Apr 2, 2014 @ 14:36

    No matter if some one searches for his vital thing, thus he/she needs to be available that in detail, therefore that thing is maintained over here.

  • Neeke Jun 6, 2014 @ 15:57

    error_log : no errors, no fixed format.

    My advice is SeasLog.

  • Josh Feb 3, 2015 @ 16:01

    How do I set proper ownership, etc? The file is not being created, it is non-existant

  • henryvalencia608 Sep 27, 2015 @ 2:23

    I have a problem with my download because the server is not open always error why

  • neeraj Jan 18, 2016 @ 5:49

    I I have a live website, indianglassart . com in this site not connect database, and return automatic error_log file. my question for it why reason for not connect database and return error_log fie, please help me…..!

    Thanks !

  • sabyasachi Sep 2, 2016 @ 10:12

    I have downloaded xampp 5.6.21 on my machine which has windows 7 ultimate . When tried running phpinfo() , I was able to get the path of the error log file in php as C:xamppphplogsphp_error_log . But when i checked out the actual folder through my browser , the logs folder itself was not visible .Is there any way of accessing my error log file ?

  • Ales Bobek Aug 24, 2017 @ 8:52

    Other useful directive is mail.log, see description:
    “The path to a log file that will log all mail() calls. Log entries include the full path of the script, line number, To address and headers.”

  • Taras Grescoe Oct 16, 2022 @ 9:01

    For anyone reading this and using the Debian and Ubuntu latest versions, edit the /etc/php/8.1/fpm/pool.d/www.conf file when using PHP-FPM with Nginx web server. Just my little contribution to this beautiful community.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.