Building DevSecOps solutions using AWS, Terraform and Kubernetes

Magento Tips - Log Rotate

  • 12th September 2021
Video walk-through setting up log rotate on a magento 2 server

What is log rotate?

On smaller magento stores that are running on a single server, log rotation helps stop the magento logs consuming all of the space on the server.

How to configure log rotate

If you're on ubuntu, then it is installed by default. If you're on another varation then you may need to install it manually.

Confirm logrotate is installed

Check if you have it installed by running:

logrotate --version

Install logrotate

If you're on debian, you can install it with:

sudo apt-get update && sudo apt-get install logrotate

Configure logrotate

Then to configure it, adjust this bash script as needed:

#!/bin/bash
DEPLOY_USER=ubuntu
DEPLOY_GROUP=ubuntu
DEPLOY_DIR=/var/www/html
sudo tee -a /etc/logrotate.d/magento2 > /dev/null <<EOF
$DEPLOY_DIR/var/log/*.log {
    su $DEPLOY_USER $DEPLOY_USER
    daily
    missingok
    rotate 31
    compress
    notifempty
    dateext
    dateformat -%Y-%m-%d
    create 664 $DEPLOY_USER $DEPLOY_USER
}
EOF

Confirm it worked

Then to confirm it works we can force it:

sudo logrotate -f /etc/logrotate.d/magento2
ls -l /var/www/html/var/log

You should see an output like this after it has been run:

Rhuaridh

Please get in touch through my socials if you would like to ask any questions - I am always happy to speak tech!