About nixCraft

Topics

Compressing large Apache log files on a shared hosting server

Posted by Vivek Gite [Last updated: December 31, 2005]

If your site is quite popular then your apache log file could quickly becomes large. It could be anywhere between 200MB or 1 GB per month. Downloading such large apache log file not just take lot of time but bandwidth too. We have lots of shared hosting customer on apache web server. If log files are small, they you can download it easily using ftp program.

Nevertheless, when log file becomes larger, downloading them is a big problem. For security reason no hosting provider provides ssh access, further system(), exec() and other php functions are disabled in php.

How the hell customer will get his/her apache raw log file. With the help of php script you can compress file and then download it. Php provides function called gzwrite() which is binary safe gz-file writer.

Syntax:

int gzwrite ( resource zp, string string [, int length] )

Where,

PHP script:

<?
// How to use?
// http://mydomain.com/private/gzip?i=access.log&o=/access.log.gz
function gzip ($in, $out, $param="1")
{
if (!file_exists ($in) || !is_readable ($in))
 return false;
if ((!file_exists ($out) && !is_writable (dirname ($out)) ||
(file_exists($out) &amp;& !is_writable($out)) ))
 return false;

$in_file = fopen ($in, "rb");
if (!$out_file = gzopen ($out, "wb".$param)) {
 return false;
}

while (!feof ($in_file)) {
 $buffer = fgets ($in_file, 4096);
 gzwrite ($out_file, $buffer, 4096);
}

fclose ($in_file);
gzclose ($out_file);

return true;
}

$in=$_GET['i'];
$out=$_GET['o'];
echo "<html><head><title>Php Gzip program</title></head><body>\n";
if (
echo "Status:";
if ( (gzip($in,$out,"9")) == false ){
echo "<h1>Failed</h1>\n";
}
else {
echo "<h1>Done</h1>\n";
}
echo "</body></html>";
?>

Just upload script to your webserver directory in password-protected directory. Then run php script by typing http url:

http://mydomain/private/gzip.php?i=access.log&o=access.log.gz

Where,

Result

Make sure private directory is apache password protected, otherwise anyone can use your script. Please note that I am not a php expert or regular programmer, if you have a better solution or script please feel free to post it.

Reference:

It is now New Years Eve. Year 2005 was a good year for all of us at nixCraft. We are proud of our small community and shared learning that we do here. Thanks to everyone who has contributed to increase our knowledge of Linux and Open source by leaving a comment or links.

Happy New Year [Naye Varsha Ki Shubhkamanyen (hindi)]
We do appreciate you and hope that you will have a great New Year celebration.

Tell us how we're doing: Please answer a few questions about your experience to help us improve nixCraft.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Anonymous Says:

    Happy New Year to everyone at nixCraft. Thanks for writing all wonderful articles. Hope to see more in 2006.

  2. christopher Says:

    Thank you for all of the wonderful Linux and UNIX tips you guys provided! Best wishes for 2006!!!

    Christopher

  3. Anonymous Says:

    Best Wishes for 2006 and keep up the excellent work!

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.