Date: Fri, 29 Mar 2024 14:47:57 +0000 (UTC) Message-ID: <99371028.79.1711723677288@fa0ec5443aab> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_78_851469747.1711723677287" ------=_Part_78_851469747.1711723677287 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Apache logs are rotated periodically using = logrotate on Linux. By default logrotate was set to rotate my apache = logs weekly and this would often occur around ~0620hrs. I would prefe= r to rotate every night precisely at midnight. This way you separate = apache logs by a by day. Note, you may prefer weekly if you have less= traffic and/or audit on a weekly basis.
Apache logs rotate via the logrotate utility. Although the frequen= cy and parameters of rotating logs with logrotate can be configured to your= needs, I wanted to rotate logs precisely every night at midnight. Fu= rthermore, I only wanted this behaviour for my apache logs (and leave other= logs being rotated by logrotate). Below outlines changes to force ap= ache log rotation every night at midnight.
We will take the following steps:
Since we only want to force apache logs to be rotated every night at mid= night, we need to move the apache2 logrotate config files to another folder= so that the normal logrotate cron scripts still run as per usual (but not = apache log rotations).
To do this we will create a new folder in /etc
and move the=
apache logrotate conf file
sudo mk= dir /etc/logrotate.apache sudo mv /etc/logrotate.d/apache2 /etc/logrotate.apache/
Note that if you're using CentOS or Amazon linux the the file to move wi=
ll be /etc/logrotate.d/httpd
instead
Note that since we're moving our apache logrotation config (which we wil= l run directly) it won't inherit the default options defined in /etc/logrot= ate.conf. Hence we should make sure it contains all the logrotation o= ptions we would like. Below is an example of my logrotate.apache file= :
/var/lo= g/apache2/*.log { missingok dateext rotate 14 create 640 root adm sharedscripts lastaction /etc/init.d/apache2 reload > /dev/null; endscript }
Run man logrotat= e to see descriptions of these logrotate options.
Now, let's create a cron job to execute the apache log rotation. S= imply run:
sudo cr= ontab -e
This will open your crontab file in your system editor (probably vi or n= ano). If you're using vi, please see here for how to use it.
Below is an example of sudo's crontab after adding a line to execute apa= che log rotation (see last line):
# Edit = this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command # Rotate logs at midnight on sunday 0 0 * * * /usr/sbin/logrotate -f /etc/logrotate.apache/apache2
The 0 0 * * *
tells cron to execute it at midnight and=
the latter arguments tell logrotate to force rotate your apache logs.
Note that if you're using CentOS or Amazon linux you would use the follo=
wing arguments instead: logrotate -f /etc/logrotate.apache/httpd=
You can test logrotate by running it with the debug flag to see what wou= ld happen (in debug mode logs will not actually be rotated):
sudo /u= sr/sbin/logrotate -df /etc/logrotate.apache/apache2