21

The PHP error_log method does not write errors to the custom error log file. It will only write to /var/log/apache2/php_error.log

Here are the logging settings in my php.ini:

error_reporting = E_ALL & ~E_DEPRECATED & ~E_NOTICE
display_errors = Off
log_errors = On
log_errors_max_len = 0 ; also tried with 9999999
error_log = /var/log/apache2/php_errors.log

PHP writes its errors to the regular Apache error log (/var/log/apache2/error.log) rather than the one I specified above.

Things I already tried:

  1. I stopped and restarted apache after changing the php.ini file
  2. The file /var/log/apache2/php_errors.log is 777 permissions and the file exists.
  3. There are no other overriding php.ini files. (There is /etc/php5/cli/php.ini but I'm not using cli).
  4. There are no other error_log = xxx settings further down the php.ini file which could overrule the first one
  5. phpinfo() says error_log = /var/log/apache2/php_errors.log (i.e. the correct file), both under Local Value and Master Value
  6. The test script I'm using to generate errors doesn't contain any ini_set calls

(FYI: I'm using Apache/2.2.17 and PHP/5.3.5-1ubuntu7.2 on Ubuntu 11.04)

What am I doing wrong?

10
  • 1
    have you tried to change filename to some else? Or try some other folder? Commented Sep 21, 2011 at 13:30
  • 1
    Check what php.ini file is loaded (look at the output of phpinfo() function) Commented Sep 21, 2011 at 13:35
  • Thanks Olli, I changed it to /usr/php_errors.log and now it works! Commented Sep 21, 2011 at 13:55
  • 2
    In all likelyhood, the permissions for the log file you specified or the folder it was contained in were not set properly to allow PHP to access the file. Commented May 30, 2013 at 17:08
  • 1
    I haven't seen the PHP code but a couple of possibilities: 1. that device was full. 2. The enclosing directory was missing the execute bit, thus if PHP was checking for the file to exist; that would fail. Simple solution: [make sure your webserver user can rwx the directory, and rw the file]. Commented May 23, 2016 at 10:59

8 Answers 8

17

I'd like to answer this as it's a high result on Google even though it's an old question.

I solved this by adding write permissions for all users on the log directory.

In my case, the user 'http' needed to be able to write to /var/log/httpd/ so I ran

# chmod a+w /var/log/httpd

If the file already exists, it may help to delete it first and allow Apache to create it.

My php.ini file included the entire path, also.

error_log = /var/log/httpd/php_errors.log
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I was running into this exact issue on an IIS server. The problem was that it was a section of the site that required basic authentication, which causes the service to run as a different user with different permissions.
5

You could try specifying the filename like this:

I'm using Apache 2.2.22 on Ubuntu.

Use this PHP command:

error_log("eric", 3, "/home/el/error.log");

The first parameter is the message to be sent to the error log. The 2nd parameter 3 means "expect a filename destination. The third parameter is the destination.

Create the file /home/el/error.log and set its ownership to this:

el@apollo:~$ ll error.log
-rwxrwxr-x 1 www-data www-data 7 Dec 13 14:30 error.log

When the PHP interprets the error_log method, it appends the message to your file.

Source: http://www.php.net/manual/en/function.error-log.php

Comments

2

I've got that problem and then I gave the right permission to the apache log folder and then restarted my apache with this commands:

sudo chmod a+w /var/log/apache2/
sudo service apache2 restart

Comments

1

You can set the error log file for a site in the VirtualHost site configuration file, e.g: /etc/apache2/sites-enabled/example.com.conf:

<VirtualHost *:80>

  ....

  ErrorLog ${APACHE_LOG_DIR}/www.example.com-error.log
  LogLevel warn
  CustomLog ${APACHE_LOG_DIR}/www.example.com-access.log combined

</VirtualHost>

In unix system ${APACHE_LOG_DIR} is usually /var/log/apache2 The errors and access will saved in ${APACHE_LOG_DIR}/www.example.com-error.log and ${APACHE_LOG_DIR}/www.example.com-access.log.

Comments

1

Make sure that the Apache user has at least EXECUTION permission on any parent folders in the path to the logfile. Without it, Apache cannot list anything in the folder and thus not see the logfile.

ls
drwxr-x--- root adm folder

chmod o+x folder

ls
drwxr-x--x root adm folder

That one is a real gotcha! ;-)

After that, also check that the Apache user does have read and write permission to the actual logfile.

3 Comments

Amazing gotcha :-) You saved me !
You have an interesting profile ! Finally someone with a profile. And a human one as that. I should fix mine too then. Nice to see someone who started on the web at roughly the same time I did :-) For me it was 1999 at Ericsson in Stockholm doing Javascript and Java and discovering Linux. I'm the old grunt ! Stephane Eybert
@Stephane, I know the feeling. It's always nice to meet an actual human on the internet ;-)
-1

remove php_errors.log and restart apache2, the server itself will create a new php_errors.log file with proper permission and owner.

rm -f /var/log/apache2/php_errors.log
service apache2 restart

ll /var/log/apache2/php_errors.log

Comments

-1

If you don't want to chown anything, use $_SERVER['DOCUMENT_ROOT'] (i.e. /var/www/html for Apache2 on Linux) where the Apache user has certainly write access:

ini_set('log_errors', 1);
ini_set('error_log', $_SERVER['DOCUMENT_ROOT'] . '/logs/php_errors.log');

To be 100% sure it works fine, manually create the directory logs

mkdir /var/www/html/logs

and the file inside:

touch php_errors.log

finally:

sudo chmod 777 php_errors.log

1 Comment

This approach worked fine for me.
-3

I spent several hours today trying to figure out why this wasn't working and trying to debug code working around the fact that I could log anything to the php error log. Needless to say, I was going at a snails pace until I got the logging working.

I would guess that more than 90% of the time it is a permission issue. In my case daemon was the owner and group for the log file and I had to sudo su to root and then change the permissions of the log file. I changed to:

chmod 777 myphperror.log

Thank God for stackoverflow.com

2 Comments

The OP said they had checked the permissions. Your answer doesn't add anything.
when your answer is chmod 777, there is a huge probability that it's not the correct one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.