Piping apache logs to a PHP file in httpd.conf

Discussion in 'Server Operation' started by asyadiqin, Feb 5, 2007.

  1. asyadiqin

    asyadiqin New Member

    I would like to pipe apache logs directly to a php file in httpd.conf using CustomLog

    Can this be done and how do I get the php file to read and parse the apache logs.

    I know there is an apache module called mod_log_sql, but to use that module, it require me to recompiled apache, which is something I am not really keen to do at this point of time.

    What I actually require is to pipe the logs to a php file, which will then insert the logs into a MySQL table.

    Anyone have any idea if there is any such script available. I know that some would be advise me to get a log parser or reader and parse the log files, but I would prefer to do this in httpd.conf using the CustomLog directive.

  2. martinfst

    martinfst ISPConfig Developer ISPConfig Developer

    I guess you need the php cli version installed; specify "|<path_to_phpcli_bin> full_path_of _your_script" in the CustomLog directive.
  3. asyadiqin

    asyadiqin New Member

    I'm not sure what you meant by PHP CLI version. I've tested the php script by running it on the command line and it works fine and inserted a empty row into the MySQL table.

    However, when I added the following line in httpd.conf, nothing happened, ie. no MySQL record was inserted into the table.

    Is there any way you can give me a sample php script that actually can read and parse the logs piped into the php script. Is there any guide as to how php read and parse logs piped to it.

    Thanks again.
  4. asyadiqin

    asyadiqin New Member

    I've just looked into the apache log and found the following lines that kept repeating until I stopped apache and removed the CustomLog line I added.

    Any ideas what could cause this error?
  5. martinfst

    martinfst ISPConfig Developer ISPConfig Developer

  6. asyadiqin

    asyadiqin New Member

    Thanks martinfs.

    That guide is only if I wish to parse the log files, which is not what I really require. I wanted the log details to be added into the MySQL table in realtime, which is why I wanted to pipe to the php script using CustomLog directive in httpd.conf.

    I am tempted to use the Apache mod_log_sql, but I don't really like to recompiled Apache just for that purpose.

    Any other ideas? Maybe some Apache and PHP gurus out there can give a tip or two on how this could be done. FYI, I am running Apache 2.2, MySQL 5.0.27 and PHP 5. Thanks again.
  7. falko

    falko Super Moderator ISPConfig Developer

    What's in /var/www/web1/log_test.php? Did you include the path to your PHP interpreter at the beginning (like #!/usr/bin/php)? Is it exectuable? Did you save it with Unix linebreaks?
  8. asyadiqin

    asyadiqin New Member

    This is what is in the file log_test.php

    #!/usr/bin/php -q
    $junk fopen('php://stdin''r');
    $handle fopen("/var/www/web1/log/log.txt""wb");
    $db = new ezSQL_mysql("sqluser""sqlpwd""dbname""dbhost");
    $sql "INSERT INTO logfile(id, junk, dateAdded) VALUES (NULL, '".$junk."', NOW())";
    Any ideas what I did wrong?
  9. falko

    falko Super Moderator ISPConfig Developer

    Is it executable?
    Do you get syntax errors when you run the script on the shell?
  10. asyadiqin

    asyadiqin New Member


    I ran the file on the commandline and it does everything, ie. create the log file and insert a row in the table.

    So I have no idea why it won't work in the httpd.conf file.
  11. falko

    falko Super Moderator ISPConfig Developer

    Is it executable by your Apache user as well?
    Please switch to your Apache user:
    su -m [I]apache_user[/I]
    and try to run the script again.
  12. asyadiqin

    asyadiqin New Member

    Hi again,

    The file permission was set to 777 when I first had problem as I did suspect that it might be a permission issue. I've even change the owner to apache.

    Any other ideas?
  13. falko

    falko Super Moderator ISPConfig Developer

    Instead of
    CustomLog "|/var/www/web1/log_test.php" combined
    you could try
    CustomLog "|/usr/bin/php /var/www/web1/log_test.php" combined
    and remove the #!/usr/bin/php -q line from your script.

Share This Page