When configuring a CentOS server with 16GB RAM for PHP/MySQL workloads, the SWAP allocation depends on several critical factors:
- Expected memory utilization patterns
- MySQL configuration and buffer pool settings
- PHP-FPM or mod_php memory limits
- System monitoring capabilities
The traditional "swap = RAM size" rule doesn't always apply to modern servers. For your 16GB Xeon system:
# Basic calculation based on Red Hat recommendations
if [ $RAM -le 2 ]; then
SWAP=$((RAM * 2))
elif [ $RAM -le 8 ]; then
SWAP=$((RAM * 1.5))
elif [ $RAM -le 16 ]; then
SWAP=$RAM
else
SWAP=16
fi
However, for a database-backed web server, consider these adjustments:
Your MySQL configuration significantly impacts SWAP needs. Check your current settings:
SHOW VARIABLES LIKE 'innodb_buffer_pool_size';
SHOW VARIABLES LIKE 'key_buffer_size';
Example configuration for 16GB server:
[mysqld]
innodb_buffer_pool_size = 8G
key_buffer_size = 256M
query_cache_size = 256M
tmp_table_size = 64M
max_heap_table_size = 64M
For PHP applications, implement proper memory controls:
; php.ini settings
memory_limit = 256M
max_execution_time = 120
realpath_cache_size = 256k
opcache.memory_consumption = 128
Set up monitoring to evaluate your actual needs:
# Create daily swap usage report
cat > /etc/cron.daily/swap_report < /var/log/swap_report
free -h >> /var/log/swap_report
echo "" >> /var/log/swap_report
vmstat 1 5 >> /var/log/swap_report
EOF
chmod +x /etc/cron.daily/swap_report
For your 16GB CentOS server running PHP/MySQL under medium-heavy load:
- Start with 8GB SWAP (50% of RAM)
- Monitor using
vmstat
andfree
- Consider adding swap files if physical swap becomes insufficient:
# Add 4GB swap file if needed
fallocate -l 4G /swapfile2
chmod 600 /swapfile2
mkswap /swapfile2
swapon /swapfile2
echo '/swapfile2 none swap sw 0 0' >> /etc/fstab
Remember to adjust kernel swappiness for database servers:
echo 'vm.swappiness = 10' >> /etc/sysctl.conf
sysctl -p
html
When configuring a CentOS server with 16GB RAM running PHP/MySQL under medium-heavy load, SWAP allocation becomes critical for system stability. The traditional "double your RAM" rule is outdated for modern systems. Here's a data-driven approach:
Based on Red Hat's guidelines and real-world benchmarks:
- Minimum: 4GB (25% of RAM) - For systems with excellent memory monitoring
- Recommended: 8GB (50% of RAM) - Balanced approach for most production environments
- Conservative: 16GB (100% of RAM) - For systems running critical financial transactions
Here's how to create and optimize an 8GB SWAP file:
# Create swap file sudo fallocate -l 8G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile # Make permanent echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab # Optimize swappiness (recommended: 10 for servers) echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf sudo sysctl -p
For MySQL servers, consider these my.cnf adjustments to reduce SWAP usage:
[mysqld] innodb_buffer_pool_size = 12G # 75% of 16GB innodb_log_file_size = 2G innodb_flush_method = O_DIRECT query_cache_size = 0 # Disable for PHP workloads
Implement these monitoring solutions:
# Basic monitoring free -h vmstat 1 # Advanced tracking (install sysstat first) sar -W 1 3 # Track swap in/out sar -B 1 3 # Page statistics
Consider scaling up SWAP when:
- Regular OOM killer activity appears in /var/log/messages
- sar -B shows >5% page scanning
- MySQL experiences sudden performance drops during backups
For newer CentOS versions (7+), ZRAM can be more efficient:
# Install and configure sudo yum install zram-generator echo '[zram0]' | sudo tee /etc/systemd/zram-generator.conf echo 'zram-size = min(ram, 8192)' | sudo tee -a /etc/systemd/zram-generator.conf sudo systemctl daemon-reload sudo systemctl start /dev/zram0