PHP error logs

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I'm trying to setup PHP error log handling to save logs in the dir where the error script file is.

I have read about the error log settings in php.ini, and how to turn on and off for system logs, to-screen logs and per directory where the error script was.

But for the per-directory logs, all the info seems to state to put in a complete path of where to save the error logs, which isn't of course possible as I don't know in whose account or directory the error script will be.

So how do I go about making PHP save an error log in the directory where the script with the error is?

Details of the system:
vhosts by virtuozzo
apache 2.2
php 5.2.6 in CLI mode as a module
suphp is the handler for PHP
(mod_suphp.so installed and working)

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Just to clarify, do you mean the EXACT directory, or a directory for the virtual site?

One is going to be a lot tougher than the other, as you have way more user permission issues.

-Greg

They have: 17 posts

Joined: Jun 2008

Hi Greg K,

I explain with an example what we want to achieve.

We have a vhost here: /var/www/vhosts/example.com/
We have a php script here: /var/www/vhosts/example.com/httpdocs/example.php

When a PHP error or warning is reported, by default the error/warning is appended to a logfile situated here: /var/www/vhosts/example.com/statistics/logs/error_log
So if there's something fishy with a PHP script, we have to debug it by navigating to the statistics/logs/error_log file, which is uncool.

Instead of the above, we want an error_log to be created in the directory where the error happened, in the example.php's case in the /var/www/vhosts/example.com/httpdocs/ directory.
If we'd have a PHP script in the /var/www/vhosts/example.com/httpdocs/test/ directory, we'd want the error to be reported in that particular directory.

Even though am a certified server manager (well, MS not Unix), this configuration seemingly is beyond my knowledge because I never worked with Virtuozzo containers and with vhosts on the scale we work now.

Thanks for your reply Greg K.

They have: 121 posts

Joined: Dec 2008

The only thing that may work is placing this in each and every script:

ini_set ('error_log', '');

where = where you want the log file written.

I have a feeling it won't though, for the following reasons:
1) having to add something like this to each script will create a small maintenance nightmare
2) It sounds like you are using this for debugging, which sounds alright, but in production, you'll ultimately want to be checking one error file for your entire site to see if something is up... otherwise you'll be checking all kinds of error files, which wouldn't be fun either.
3) It would be more work to secure many log files than it would one log file. Exposing error logs will give 'the bad guys' a much better idea of the architecture of your system, which makes it easier for them to do the things you don't want them doing.

If it is just for debugging, you could set a development environment up where all errors are echoed into your pages at run time, and then in production, all errors are logged to a single error log.

Cheers,
Shaggy

They have: 17 posts

Joined: Jun 2008

Update:

error_logs now appears in the place where the errors happens.

Just for the record and what i have changed:
1. PHP now running in CGI mode, this i think is optional though, and can be "fixed"
2. each vhost has its own vhost.conf in /var/www/vhosts/example.com/conf/ and php.ini in /var/www/vhosts/example.com/etc/, the second is optional and can be locked with chown for example

2.a. Greg was right when he said error_log = error_log has to be set in the php.ini which is used by the vhost, without that error_log will not appear in the place where the error happens but in the default location.

-----
edit:
3. suphp is running in paranoid mode
-----

Shaggy, I don't want to offend anybody, but in UNIX-like OSes usually there's no "impossible to achieve". It's a matter of thinking and you figure it out.

Thanks for the thoughts.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

No offence taken, to each their own setup, just IMO makes the server messier with possible error messages all over. If I have a script in one directory, and it includes 2 or 3 other scripts from other locations, I wouldn't want to be going checking all those directories to find the one that the error occurred at, i prefer just to tail the domain's error_log

Thank you for sharing how you achieved it though

-Greg

PS. what permissions are on the error logs? If i go to your site and see /members/login.php Would I be able to just browser to /members/error_log and see errors that were generated in the past? (potentially showing info you wouldn't want people to see.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

The error logs are viewable, but htaccess resolves that:

<files error_log>
order allow,deny
deny from all
</files>

I find directory based error logs are easier for development. When coding, the FTP client is generally open in the dir for the files being worked on, allowing quick access to the error log for errors specific to the files you are currently working on.

I very rarely get errors, warnings or notices anyway.
On the rare occasions it tends to be that PHP halts with a fatal error for something stupid like I miss of a final brace to a bunch of if/elses, or the last parenthesis to a function within an IF.
So a quick glance at the error log is easier than logging into plesk/cpanel and browsing to the error log section IMO.
Also, I delete error logs after reading them, and when I save the file I'm working on, the dir in ftp reloads automatically, revealing an error log if there was something I missed.

As you said though, each to their own, we all work differently.
As long as live sites don't have output to the screen generally things are ok. Although a Google search shows plenty of people don't turn screen error reporting off. Doh!

Cheers for the help!

They have: 17 posts

Joined: Jun 2008

Greg K, not you Smiling, that was directed towards Shaggy. I really don't like the "impossible" word when we talk about Unix-like or Unix systems, because even if seemingly something is impossible, you touch a c script, open it up with vi and when you're finished gcc does the rest, and your issue is solved.

I code rarely, but Greg usually messes with PHP all day long, and if he got used to error reporting to the dir where the error happens, why forcing him to get used to a new system? Instead I try to create an environment exactly or at least very similar to the one he is used to.
When you have to get used to new environments, you loose efficiency and that's not good.

They have: 17 posts

Joined: Jun 2008

Step-By-Step: How to enable PHP error logging to the directory where the error happens, on Virtuozzo containers, on per vhost basis

  1. Apache and PHP are already running so installing them is unnecessary
  2. Install SuPHP in force or paranoic mode. The safest way is to configure and compile it by your own, because RPM/Yum will install it in a way you will not be able to continue. Also, in Yum's case you will also have to enable Atomic package support.
    • Building manually. Download suphp from its official site using wget for example, to a directory where you have building permissions. I usually use the /usr/src/ directory for this purpose:
      cd /usr/src/
      wget http://www.suphp.org/download/suphp-0.7.1.tar.gz
    • "untar" the object, then navigate in the directory which has been created
    • Configure to prepare installing:
      ./configure --prefix=/usr --sysconfdir=/etc --with-apr=/usr/bin/apr-1-config --with-apxs=/usr/sbin/apxs --with-apache-user=apache --with-setid-mode=paranoid --with-php=/usr/bin/php-cgi --with-logfile=/var/log/httpd/suphp_log --enable-SUPHP_USE_USERGROUP=yes
      The "paranoid" mode is quite important, as well as the "apr" directive
    • If there wasn't any issue, finish installation:
      make && make install
  3. At this point suPHP is installed on your server but no one makes use of it since apache doesn't know about it. Fixing this is easy as 123: navigate to apache's conf.d directory, create a file named mod_suphp.conf and place the below line in it:
    LoadModule suphp_module modules/mod_suphp.so Alternatively, you can put this line in any *.conf you want, even in httpd.conf in the appropriate place (where the modules are loaded to not create a mess)
  4. Usually, when you install suPHP a file named suphp.conf is also created in the place where you have specified when you configured the script:
    sysconfdir=/etc
    open it up, and make adjustments if you want/need, I use this config:
    [global]
    logfile=/var/log/httpd/suphp_log
    loglevel=warn
    webserver_user=apache
    docroot=/var/www/vhosts
    env_path=/bin:/usr/bin
    umask=0077
    min_uid=500
    min_gid=500

    ; Security options
    allow_file_group_writeable=false
    allow_file_others_writeable=false
    allow_directory_group_writeable=false
    allow_directory_others_writeable=false

    ;Check wheter script is within DOCUMENT_ROOT
    check_vhost_docroot=true

    ;Send minor error messages to browser
    errors_to_browser=false

    [handlers]
    ;Handler for php-scripts
    x-httpd-php="php:/usr/bin/php-cgi"

    ;Handler for CGI-scripts
    x-suphp-cgi="execute:!self"
  5. Now the vhost you want to enable suPHP on. Navigate to it, enter the directory named "conf", inside that create a file named "vhost.conf":
    cd /var/www/vhosts/example.com/
    cd conf
    touch vhost.conf

    Put the following inside the vhost.conf:
    <IfModule mod_suphp.c>
    <Directory "/var/www/vhosts/example.com/httpdocs/">
    php_admin_flag engine on
    suPHP_Engine On
    suPHP_ConfigPath "/var/www/vhosts/example.com/etc/"
    AddHandler x-httpd-php .php
    AddHandler php5-script .php
    AddHandler x-httpd-php .php .php5 .php4 .php3 .phtml
    suPHP_AddHandler x-httpd-php
    suPHP_AddHandler php5-script .php

    suPHP_UserGroup YOUR_FTP_USER psacln

    php_value open_basedir "/tmp/"
    php_value upload_tmp_dir "/var/www/vhosts/example.com/tmp/"

    <Files php.ini>
    order allow,deny
    deny from all
    php_value open_basedir "/tmp/"
    php_value upload_tmp_dir "/var/www/vhosts/example.com/tmp/"
    </Files>
    </Directory>
    </IfModule>
  6. Let's create a php.ini in your vhost's etc directory. If you don't wish to create one, PHP will use the default one; in this case you will have to modify the original php.ini.
    Since there's already a working php.ini, we can safely copy that to the vhost's etc directory and use that:
    cp /etc/php.ini /var/www/vhosts/example.com/etc/php.ini
  7. Open up the php.ini you will gonna use and enter the following line in it:
    error_log = error_log
  8. And finally, restart the webserver:
    httpd -k restart

Definitely not the best guide, but should work.

They have: 4 posts

Joined: Mar 2011

I think the error based on phone records are easier for development. When coding, the FTP client is generally open in the directory for the files you are working, allowing rapid access to the error log for errors specific to the files you are currently working.

They have: 10 posts

Joined: Apr 2011

I don't know php error logs so,Thanks for sharing the information.

They have: 3 posts

Joined: Jul 2011

I dont know php. will learn. hope the notes posted above will help in future.
Thanks in advance who all have posted

They have: 4 posts

Joined: Jul 2011

Very rarely do you see errors, warnings and notices anyway. On the rare occasions they tend to stop PHP with a fatal mistake for something stupid like miss a brace end a lot of if / elses, or the last parenthesis of a function within an IS. So a quick look at the error log is easier to log into Plesk / Cpanel and searching the error log section IMO.

They have: 59 posts

Joined: Aug 2011

Hi, newbie here with a question about the location of the PHP error log.

In my php.ini file, the "error_log" value was blank, but the error log which became (according to some other forum) means that the errors that have occurred in the system log file. However, I can not find where you are. I looked in var / logs, but have not found anything related to PHP-info.

Does anyone know where PHP errors are logged when no log file to set the location of PHP? Any help would be greatly appreciated. Thanks!

* EDIT: Gah, it seems I was wrong - errors are saved in the system log if you set "error_log = syslog" in php.ini. I guess this means that PHP errors are not recorded anywhere. Can anyone confirm this?

They have: 11 posts

Joined: Nov 2011

What is PHP? Sorry I am really trying to learn about all this stuff with no success.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.