November 18, 2020 / Nirav Shah
Server log files are a raw, unfiltered look at traffic to your site. They are text files stored on your web server. Every time any browser or user-agent, Google included, requests any resource-pages, images, javascript file, whatever-from your server, the server adds a line in the log.
11.222.333.44 – – [14/SEP/2020:11:01:28 –0600] “GET /blog/page-address.htm HTTP/1.1” 200 182 “-” “Mozilla/5.0 Chrome/60.0.3112.113”
Many times these log files occupy a considerable space in the server disk drive so we need to clean the log files periodically.
Step 1
Navigate to /var/log there you will see many compressed file named as .tar.gz as an extension we need to clean this file
This will delete all the compressed files in the log folder
This is about how to clean apach2/nginx log
Step 2
Navigate to the service folder whichever you are using apache2 or Nginx
This script will automatically delete the log files from the server at the specified time so that you need not do it manually every time you find that space is getting full.
Step 1
Create a scripts directory
sudo mkdir /scripts
Step 2
Navigate to the scripts directory
cd /scripts
Step 3
Create a new file inside the folder
sudo touch log-clean.sh
Step 4
Edit the log-clean file
sudo vim log-clean.sh
Copy the below content in the log-clean file
#!/bin/bash sudo rm -rf /var/log/*.gz sudo rm -rf /var/log/apache2/*.gz
Step 5
Now give the file executable permission
sudo chmod a+x log-clean.sh
Step 6
You have to specify the time a which the log-clean should execute you can get the time via https://crontab.guru/
Step 7
Edit the crontab file to schedule it
sudo crontab -e
Step 8
In our case, we have scheduled it to every day at the start of the day
0 0 * * * sh /scripts/log-clean.sh
Save the file with Ctrl+X and press Y
Conclusion
By this, you can automate the log clean process which does free the huge space occupied by the log files and decreases the associated cost.
As a Director of Eternal Web Private Ltd an AWS consulting partner company, Nirav is responsible for its operations. AWS, cloud-computing and digital transformation are some of his favorite topics to talk about. His key focus is to help enterprises adopt technology, to solve their business problem with the right cloud solutions.
Have queries about your project idea or concept? Please drop in your project details to discuss with our AWS Global Cloud Infrastructure service specialists and consultants.