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

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.

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 : /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 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+: /usr/local/etc/php.ini

You can use the following command to find out path to php.ini:
# 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

Sample outputs:

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

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

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:
# 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:
# /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:
$ sudo tail -f /var/log/php-scripts.log
Sample outputs:

Fig.01: PHP error log output

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 32 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
32 comments… add one
  • Anjanesh Jul 30, 2007 @ 3:22

    It would be much better to have a single config php-file and define them there instead.

    ini_set("log_errors" , "1");
    ini_set("error_log" , "Errors.log.txt");
    ini_set("display_errors" , "0");

    • rajesh Apr 13, 2011 @ 7:49

      thats excellent

  • Philipp Jul 30, 2007 @ 7:26

    You have to watch, that the logfile is writeable by the web-server process!

  • 🐧 nixCraft Jul 30, 2007 @ 21:27

    @Anjanesh, good point, user can set those from a php script itself.

    @Philipp, yes I did assumed that Apache or lighttpd has a write permissions

    Appreciate your posts!

  • Alekc Jan 22, 2008 @ 11:48

    Small misspelling in title: “log fiie”

  • 🐧 nixCraft Jan 22, 2008 @ 12:04

    Thanks for the heads up!

  • China Landscape May 9, 2008 @ 9:19

    I have ligttpd (PHP FastCGI) and I cannot see the log.

    How can I get it in this case ?

  • Carl Oct 9, 2008 @ 15:06

    “yes I did assumed that Apache or lighttpd has a write permissions”

    In the case that it doesn’t, how can this be changed?

  • 🐧 nixCraft Oct 9, 2008 @ 15:13


    Use chmod and chown command to set permissions.

  • Kurt Oct 24, 2008 @ 7:31


    Is it possible to log errors of diffrent websites in different errorfiles?

    I have a server that’s hosting different websites and I want that all of the php-errors are logged in different file. For each website a file.

    Is that possible and how do I do it?

    tnx in advance

  • 🐧 nixCraft Oct 24, 2008 @ 11:28


    You need to setup php as FastCGI which allows the -c option. With this option you can provide custom php.ini file and error log file for each user. See this article for more info. This is lighttpd specific but you should able to setup with Apache or any other server with mod_fastcgi.


  • Carl Oct 24, 2008 @ 19:37

    You might try ini_set() as well if you have trouble getting custom php.ini files to work

  • Svetainiu Kurimas Oct 19, 2009 @ 16:24

    Hi, does the init_set solution also work with Apache installed on Windows? It doe snot seem to be working for me.

  • Liju Dec 23, 2009 @ 5:08

    I think it would need to enable this tag (log_errors = On) php.ini to get this complete.

    But I suggest it’s not a good Idea to enable on the main configuration file where a server hosted multiple sites. Just add this entries on .htaccess make sense on such case to trouble shoot.

  • lcd Jan 25, 2010 @ 16:09

    You have great blog and this post is good!

  • Dhanesh Mane Jun 14, 2010 @ 7:12

    Hey ,

    I think if you are error_log directive is set to syslog, then logs are save at /var/log/message.
    On my server its set to syslog and the file /var/log/message is also updated but I am not able to find any php log data in this file.

    any hint wat’s happening?

  • pawel Aug 12, 2010 @ 10:21

    what i read was enough to solve my issue, why im writing is to see if i get one nice picture with no.16 on the right 🙂

  • yuri Dec 30, 2010 @ 1:31

    I followed the steps above and its still not working, what should I do?

  • jyoti Feb 13, 2012 @ 9:33

    i am using php 5.3 with xamp server and get error ‘cannot write to file(../debug.log)’ please help me becaz i am new for php…thanks in advance

  • jyoti Feb 13, 2012 @ 9:34

    i am using php 5.3 with xamp server and get error ‘cannot write to file(../debug.log)’ please help me becaz i am new for php ..thanks in advance

  • Alexander Apr 10, 2012 @ 7:10

    It seems, user, under which you run php, does not have write permissions on specified file/directory. Check parent directory permissions, if is it writable.

    I also had the same issue. I had path and settings for logging error file, but saw no file created till I set normal permissions.

  • 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.”

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum