5 min read

Linux Server Optimization: Best Practices for Web Hosting

Learn essential techniques for optimizing Linux servers in web hosting environments, including performance tuning, security hardening, and monitoring setup.

Linux Server Optimization: Best Practices for Web Hosting

Optimizing Linux servers for web hosting environments requires a comprehensive approach that balances performance, security, and reliability. In this article, I’ll share proven techniques that have helped me maintain high-performing web hosting infrastructure.

Note: Always test configuration changes in a staging environment before applying them to production servers.

Performance Optimization

1. Kernel Parameter Tuning

One of the first steps in server optimization is tuning kernel parameters for web hosting workloads. Key parameters to adjust include:

  • Network optimizations: Increase buffer sizes for better network performance
  • File descriptor limits: Set appropriate limits for concurrent connections
  • Memory management: Optimize swap usage and dirty page ratios

Here’s a sample configuration for /etc/sysctl.conf:

Terminal window
# Network optimizations
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 65536 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
# File descriptor limits
fs.file-max = 2097152
fs.nr_open = 2097152
# Memory management
vm.swappiness = 10
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5

2. Web Server Configuration

For Apache servers, optimize the configuration based on your server’s resources:

ParameterRecommended ValuePurpose
ServerLimit16Maximum number of child processes
MaxRequestWorkers400Maximum simultaneous connections
ThreadsPerChild25Threads per child process
MaxConnectionsPerChild10000Restart workers after N requests

Security Hardening

SSH Configuration

Secure SSH access with these recommended settings:

  1. Use default port 22 → Change to non-standard port (e.g., 2222)
  2. Disable root login
  3. Use key-based authentication only
  4. Set appropriate timeout values
  5. Limit authentication attempts

Example /etc/ssh/sshd_config:

Terminal window
Port 2222
Protocol 2
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3

Warning: Make sure you have SSH key access configured before disabling password authentication!

Firewall Setup

Implement a robust firewall using UFW or iptables:

Terminal window
# Basic UFW configuration
ufw default deny incoming
ufw default allow outgoing
ufw allow 2222/tcp # SSH
ufw allow 80/tcp # HTTP
ufw allow 443/tcp # HTTPS
ufw enable

Monitoring and Alerting

System Monitoring Checklist

Track these key metrics for optimal performance:

  • CPU usage monitoring
  • Memory utilization tracking
  • Disk space monitoring
  • Network traffic analysis
  • Service status checks
  • Log file size monitoring
  • SSL certificate expiration alerts

Log Management

Implement centralized log management with proper rotation:

Click to see logrotate configuration example
Terminal window
/var/log/apache2/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 www-data adm
postrotate
/etc/init.d/apache2 reload > /dev/null
endscript
}

Automation Scripts

Automated Backups

Create automated backup scripts for:

  1. Website files backup - Daily incremental, weekly full
  2. Database backup and restoration - Hourly transaction logs
  3. Configuration files backup - After any change
  4. Automated cleanup of old backups (30-day retention)

Sample backup script:

#!/bin/bash
# backup.sh - Automated backup script
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/backups"
SITES_DIR="/var/www"
# Create backup directory
mkdir -p $BACKUP_DIR/$DATE
# Backup websites
tar -czf $BACKUP_DIR/$DATE/websites.tar.gz $SITES_DIR
# Backup databases
mysqldump --all-databases > $BACKUP_DIR/$DATE/databases.sql
# Clean old backups (keep 30 days)
find $BACKUP_DIR -type d -mtime +30 -exec rm -rf {} \;
echo "✓ Backup completed: $DATE"

Health Check Script

Monitor server health with automated checks:

health_check.sh
#!/bin/bash
ALERT_EMAIL="[email protected]"
# Check disk space
DISK_USAGE=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')
if [ $DISK_USAGE -gt 80 ]; then
echo "⚠ Disk usage is ${DISK_USAGE}%" | mail -s "Disk Space Alert" $ALERT_EMAIL
fi

Performance Testing

Load Testing Tools

Use these tools to validate your optimizations:

ToolPurposeCommand Example
Apache BenchHTTP load testingab -n 1000 -c 10 http://example.com/
wrkModern HTTP benchmarkingwrk -t12 -c400 -d30s http://example.com/
siegeStress testingsiege -c 100 -t 60s http://example.com/

Monitoring Tools

Implement real-time monitoring:

  • htop - Process monitoring with color output
  • iotop - I/O usage by process
  • iftop - Network bandwidth monitoring
  • nethogs - Network usage per process

Conclusion

Server optimization is an ongoing process that requires regular monitoring and adjustment. By implementing these best practices, you can significantly improve your server’s:

  1. Performance metrics
  2. Security posture
  3. System reliability
  4. Resource utilization

Remember to:

Test changes in staging first → Monitor metrics → Document modifications → Review regularly

The key to successful server optimization is understanding your specific workload requirements and continuously monitoring performance metrics to ensure your optimizations are effective.


Written by Dzubayyan Ahmad | System Administrator & SRE

Found this article helpful? Share it