In order to rotate our access logs, first lets setup three filenames. $access_file should be the name of your actual Apache log access file. This is typically something along the lines of mywebsite.com-access_log, but double check with your configuration files first. We're going to be renaming this file every night with the current date, so let's create $access_archivefile using strftime to add the date. Finally, we define $access_archivezip in the same way (stamped with the date) adding the .tar.gz to the end.
$access_file = "mywebsite.com-access_log";Now it's time to get the job done using the system function. First we move the original file to the new archive name (which will temporarily stop the web server writing it's logs). Next step is to tar and zip the file and create the actual archive zip file.
$access_archivefile = strftime("$access_file.%Y-%m-%d", localtime);
$access_archivezip = "$access_archivefile.tar.gz";
system "mv $access_file $access_archivefile";Next, let's delete the un-zipped archive file and mv the zipped one on into the archives directory.
system "tar -czf $access_archivezip $access_archivefile";
system "rm $access_archivefile";Next: Rotating the error log
system "mv $access_archivezip archives/";

