Skip to content
Welcome To Charanjit Cheema Blog

Welcome To Charanjit Cheema Blog

An Open Source and Cloud Blog

Menu
  • Home
  • About Me!
  • Way to my Technical Blog
  • Contact me
  • Privacy Policy
Menu

How to manage the Apache logs with the help of Linux logrotation utility

Posted on September 18, 2022February 13, 2023 by Charanjit Cheema

Maintaining logs in busy Apache server is a very painful as frequent access of Apache webservice from clients can generate large size of logs example access_log and error_log which can consume the filesystem space but we should not stop generating these logs also as these logs are very helpful in troubleshooting the apache related issues when in case apache does not work correctly.

I had also encountered with same problem when Apache logs start consuming lot of filesystem space in one of my SAP Content RHEL 7 server therefore for remediating this problem I have chosen Linux logrotation utility. Logrotate utility can automatic rotate, compress, remove and mail the system and application log files. It can rotate the log file daily, weekly, monthly or when the log file gets to a certain size.  Normally, logrotate runs through daily cron job.

In this post I will guide you how to enable logrotation for Apache service by keeping relevant days log so that it can save Linux filesystem space consumption.

 

Installing logrotation utility in RHEL / CentOS server

By default, logrotation utility get install with the Base system installation but if in case logrotate utility is not available in OS you can install the logrotation separately with below command:

 yum install logrotate 

 

Configuration of Apache Logrotation

There are two ways to configure the logrotation either you can configure the Apache logrotation parameters directly in /etc/logrotate.conf or you can create separate logorate file under /etc/logrotate.d/ directory path. I preferred the second option since it felt me easy to manage the Apache logrotation configuration.  I created separate apache logrorate file and configured it as below:

Note: You can provide any name to Apache logrotation file as per your choice.

Reference to my above sample Apache logrotation file below is the definition of each logrotation configuration parameter:

  1. First line indicates the directives of Apache logs which need to rotate
  2. rotate 4 indicates that only 4 rotated logs should be kept. So, the oldest file will be removed on the fifth subsequent run.
  3. size 20M is a threshold size of each Apache log which means logs will not be rotated unless logs size will reach above the 20 MB of size
  4. sharedscript parameter allow us to define either command or script path which can be run before the logrotation as in example of my logrotation configuration file I have defined the command to perform the graceful restart of Apache service before logrotation rotate the logs since Apache won’t allow the logrotation to rotate the logs unless it release its hold from writing the logs, further sleep 600 wait the Apache service for some time for restarting it after the graceful stop before doing any processing on the new log files. But since I am using Apache graceful command so it will not interrupt the existing clients connectivity with the Apache Webservice.

 

Automate the logrotation with cron

One thing needs to understand, during logrotation utility installation either manually or through Base system installation Linux by default create a logrotation cron job, so you need not to create the cron job explicitly. You can find logrotation script under /etc/cron.daily path as shown in below screenshot:

 

Validation of Apache logrotation configuration

Command logrotation allow you to do dry run the logrotation configuration file so that logoration file can be validated without waiting for file to run through cron job. Below is an example of logrotation dry run command which I executed for checking Apache logrotation file configuration:

Below is the example of Apache rotated log files through daily cron job in my server:

Loading

  • Author
  • Recent Posts
Charanjit Cheema
Follow him
Charanjit Cheema
Charanjit is currently working as a Cloud Architect at Mphasis, with 17 years of experience in IT infrastructure projects, implementation, and support. While his main role is as a DevOps engineer, he holds a Cloud Architect position and has strong skills in cloud technologies and automation. His expertise includes Terraform, Ansible, AWS, Azure DevOps, Azure Cloud, VMware, and Linux systems.

Charanjit is passionate about automating tasks and improving processes. He uses tools like Terraform and Azure DevOps to build and manage cloud infrastructure and streamline deployment. He also enjoys using Shell scripts and Ansible playbooks to make systems run more efficiently.

In his free time, Charanjit enjoys learning about new technologies and sharing his knowledge through his blog. When he’s not working, he likes listening to music, having a cup of coffee, and relaxing in nature.

You can connect with Charanjit on Twitter, Facebook, LinkedIn, or email him at charanjit.cheema@cjcheema.com.
Charanjit Cheema
Follow him
Latest posts by Charanjit Cheema (see all)
  • How to optimize the performance of Ansible Automation Platform or Ansible Tower - July 16, 2023
  • How to fix code and text file linting errors with the help of Visual Studio Code - June 27, 2023
  • How to migrate existing WordPress blog from AWS EC2 instance to Amazon Lightsail - May 29, 2023

Like this:

Like Loading...

Related

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Tags

AWS Cloud Computing Dockers Networking Open Networking OpenSource RHEL-CentOS SDN Server Hardware SLES tcpdump Ubuntu WSL

Follow me @

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 2 other subscribers

Recent Posts

  • How to Deploy Docker Containers with NGINX on AWS EC2 Using Ansible and GitHub Actions
  • No More DynamoDB! Use Native S3 locking for Terraform State
  • How to Bring and Manage Manually Created AWS Resources Under Terraform Management
  • Iterating Cloud Resource Provisioning Using Terraform Count and For_Each Meta-Arguments
  • Terraform and Ansible Collaboration for AWS Cloud Deployment

Recent Comments

  1. Charanjit Singh on Terraform and Ansible Collaboration for AWS Cloud Deployment
  2. christinatodd2020aeaa798563 on Terraform and Ansible Collaboration for AWS Cloud Deployment
  3. Charanjit Singh on How to Set password policy in CentOS or RHEL system
  4. SAURABH on How to recover or rebuild initramfs in CentOS 7 Linux
  5. Sangita on How to Set password policy in CentOS or RHEL system

Archives

  • April 2025
  • February 2025
  • January 2025
  • August 2024
  • July 2024
  • June 2024
  • January 2024
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • September 2022
  • August 2022
  • July 2020
  • May 2020
  • February 2020
  • November 2019
  • June 2019
  • May 2019
  • March 2019
  • February 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • June 2018
  • May 2018
  • April 2018

Categories

  • Automation
  • Cloud Computing
  • Coding
  • CyberSecurity
  • Networking
  • OpenSource
  • RHEL-CentOS
  • Server Hardware
  • SLES
  • Technical Blog
  • Ubuntu
  • WSL

Blog Stats

  • 18,353 hits
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Home
  • About Me!
  • Way to my Technical Blog
  • Contact me
  • Privacy Policy
© 2025 Welcome To Charanjit Cheema Blog | Powered by Superbs Personal Blog theme
 

Loading Comments...
 

    %d