In this article, we'll walk through the complete process of working with logrotate in a server environment. Understanding logs is essential for maintaining a reliable and performant infrastructure.
Prerequisites
- Root access to the server
- Basic familiarity with the Linux command line
- Root or sudo access to the server
- SSH client on your local machine
Initial Setup
Regular maintenance is essential for keeping your logrotate installation running smoothly. Schedule periodic reviews of log files, disk usage, and security updates to prevent issues before they occur.
# Systemd service management
sudo systemctl status nginx
sudo systemctl enable --now nginx
sudo systemctl restart nginx
# View service logs
sudo journalctl -u nginx -f --since "10 minutes ago"
# List all running services
systemctl list-units --type=service --state=running
Note that file paths may vary depending on your Linux distribution. The examples here are for Debian/Ubuntu; adjust paths accordingly for RHEL/CentOS-based systems.
Configuration Steps
If you encounter issues during setup, check the system logs first. Most problems can be diagnosed by examining the output of journalctl or the application-specific log files in /var/log/.
# Server resource monitoring
htop # Interactive process viewer
iostat -x 1 5 # Disk I/O stats (5 samples)
vmstat 1 5 # Virtual memory stats
ss -tlnp # Open listening ports
netstat -an | wc -l # Total connections
Each line in the configuration serves a specific purpose. The comments explain the reasoning behind each setting, making it easier to customize for your specific use case.
Automation and Scheduling
Security should be a primary consideration when configuring logrotate. Always use strong passwords, keep software updated, and restrict network access to only the necessary ports and IP addresses.
# Systemd service management
sudo systemctl status nginx
sudo systemctl enable --now nginx
sudo systemctl restart nginx
# View service logs
sudo journalctl -u nginx -f --since "10 minutes ago"
# List all running services
systemctl list-units --type=service --state=running
The configuration above sets the recommended values for a VPS with 2-4GB of RAM. Adjust the memory-related settings proportionally if your server has different specifications.
Monitoring and Alerts
After applying these changes, monitor the server's resource usage for at least 24 hours to ensure stability. Tools like htop, iostat, and vmstat can provide real-time insights into system performance.
# Server resource monitoring
htop # Interactive process viewer
iostat -x 1 5 # Disk I/O stats (5 samples)
vmstat 1 5 # Virtual memory stats
ss -tlnp # Open listening ports
netstat -an | wc -l # Total connections
These commands should be run as root or with sudo privileges. If you're using a non-root user, prefix each command with sudo.
Next Steps
With logrotate now set up and running, consider implementing monitoring to track performance metrics over time. Regularly review your configuration as your workload changes and scale resources accordingly.