4.1 Monitoring VPS Performance
Keeping track of your VPS performance helps ensure it runs smoothly. Here are essential tools:
Check System Resource Usage
- CPU & Memory Usage:
top
orhtop # More user-friendly, install with `apt install htop`
- Disk Usage:
df -h
- Check Active Processes:
ps aux --sort=-%mem | head -10 # Top 10 memory-consuming processes
Set Up Real-Time Monitoring
- Glances – A comprehensive system monitoring tool:
apt install glances -y glances
- Netdata – A web-based real-time monitoring tool:
bash <(curl -Ss https://my-netdata.io/kickstart.sh)
4.2 Optimizing VPS Performance
1️⃣ Enable Swap Space (For Low RAM VPS)
If your VPS has low RAM, enable swap space to prevent crashes.
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo "/swapfile none swap sw 0 0" >> /etc/fstab
2️⃣ Optimize Apache & Nginx
For Apache, enable compression:
a2enmod deflate
systemctl restart apache2
For Nginx, add gzip compression in /etc/nginx/nginx.conf
:
gzip on;
gzip_types text/plain application/json text/css;
Restart Nginx:
systemctl restart nginx
3️⃣ Optimize MySQL Performance
Use MySQLTuner to analyze and suggest optimizations:
apt install mysqltuner -y
mysqltuner
4.3 Automating VPS Management
1️⃣ Set Up Automatic Updates
For Debian/Ubuntu:
apt install unattended-upgrades -y
dpkg-reconfigure unattended-upgrades
For CentOS/Rocky Linux:
yum install dnf-automatic -y
systemctl enable --now dnf-automatic.timer
2️⃣ Set Up Cron Jobs for Maintenance
To schedule daily updates, edit the crontab:
crontab -e
Add:
0 3 * * * apt update && apt upgrade -y
This runs system updates at 3 AM daily.
4.4 Backup & Disaster Recovery
1️⃣ Automatic Backups with Rsync
Sync important files to a backup server:
rsync -avz /var/www/ user@backup-server:/backups/
2️⃣ Backup MySQL Databases
Create a daily database backup:
mysqldump -u root -p --all-databases > /backups/db-$(date +\%F).sql
Add to cron for daily backup:
0 2 * * * mysqldump -u root -p'yourpassword' --all-databases > /backups/db-$(date +\%F).sql
4.5 Securing Your VPS Further
1️⃣ Set Up 2FA for SSH (Google Authenticator)
apt install libpam-google-authenticator -y
google-authenticator
Enable in /etc/ssh/sshd_config
:
ChallengeResponseAuthentication yes
Restart SSH:
systemctl restart sshd
2️⃣ Enable Automatic Ban for Failed SSH Attempts (Fail2Ban)
nano /etc/fail2ban/jail.local
Add:
[sshd]
enabled = true
maxretry = 3
bantime = 3600
Restart Fail2Ban:
systemctl restart fail2ban
Conclusion
With these optimizations, your VPS will run faster, more securely, and efficiently.
In the next chapter, we’ll cover Deploying Applications on VPS, including setting up WordPress, Node.js, and Laravel.