1

I am piping apache log to php file in order to insert later the log to the database.

in the virtual host I have the line:
ErrorLog "|/usr/bin/php /opt/waffy/log2db.php"

in php I have:

$stdin = fopen ('php://stdin', 'r');
ob_implicit_flush (true); // Use unbuffered output
$data = "";
while ($line = fgets ($stdin))
{
   $data .=  $line;
}
file_put_contents("/opt/waffy/log.log",$data);

The php is invoked but the file /opt/waffy/log.log is empty.

UPDATE1

I think i have a little progress when I did chmod +X log2db.php nothing hapend, but when I replaced X with x I stopped getting the ERR_CONNECTION_REFUSED.

but the file /opt/waffy/log.log is not created...

1 Answer 1

1

The script doesn't match what is expected for an Apache piped log script so what exactly is output is likely undefined. A minimal PHP example is:

#!/usr/bin/php

<?php
$stdin = fopen ('php://stdin', 'r');
ob_implicit_flush (true); // Use unbuffered output
while ($line = fgets ($stdin))
{
   print $line;
}

With the following enty in apache

ErrorLog "|/var/www/log.php >>/var/www/log.log"

Don't forget to give log.php the right permissions

chmod +x log.php

Apache needs the permission to write to the directory /var/www/ in my example.

See http://www.sudleyplace.com/pipederrorlogs.html for more details. First get it working so it simply outputs everything it receives and then modify the data and add features.

Important

You have to start apache via

/etc/init.d/apache2 restart 
/etc/init.d/apache2 start

and not via

service apache2 start
service apache2 restart
Sign up to request clarification or add additional context in comments.

7 Comments

Putting this ` ErrorLog "|/opt/waffy/log2db.php >> /opt/waffy/out.log" ` in my virtual host, causing ERR_CONNECTION_REFUSED when accessing the page, even though configtest went ok.... Thanks. I think its every time the >> is in my virtual host...
@SexyMF I tested it it works fine for me. You changed you script and added the line #!/usr/bin/php and looked at the right permissions
Yes, all dont, except my php is in #!/usr/bin/php5 (ubuntu)
@SexyMF In your question you had /usr/bin/php so when you just to /opt/waffy/log2db.php it doesn't gives you an error. Perhaps remove the space between >> and /var. so thats then `>>/opt/waffy/out.log' . And apache has the permissions to write to /opt/waffy/out.log
I am for sure if you following my example and create chmod 777 for the directory /opt/waffy/ it 100% will work. Apache just has not the right permission to write into the directory. I tested it on different server centos and debian. And my example always works, with the right permissions.
|

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.