Linux VPS: Performance Tweaks

These optimizations apply to Debian/Ubuntu distributions. Many parameters are useful for any use (web server, gaming server, database), with specific sections for use cases.

02

1. Kernel optimizations (sysctl)

Modify /etc/sysctl.conf or create /etc/sysctl.d/99-custom.conf:

bash
sudo nano /etc/sysctl.d/99-vps-tweaks.conf

Add:

ini
# ── General Network ──
# Increase TCP buffers (useful for high-latency connections or broadband)
net.core.rmem_max = 134217728
net.core.wmem_max = 134217728
net.ipv4.tcp_rmem = 4096 87380 134217728
net.ipv4.tcp_wmem = 4096 65536 134217728

# Connection backlog (important for high-traffic web servers)
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65536

# SYN backlog (resistance to SYN flood)
net.ipv4.tcp_max_syn_backlog = 65536

# Reuse TIME_WAIT ports (important for many short connections)
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15

# TCP keepalive
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 5

# ── Memory ──
# Limit aggressive swap (0 = use swap only if necessary)
vm.swappiness = 10

# Dirty pages: more aggressive disk writes (server with frequent writes)
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5

# ── File descriptor ──
fs.file-max = 2097152

# ── IPv4 hardening + performance ──
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_slow_start_after_idle = 0

Apply:

bash
sudo sysctl -p /etc/sysctl.d/99-vps-tweaks.conf
03

2. Increase file descriptors (ulimit)

Necessary for web servers, databases and applications with many simultaneous connections.

bash
sudo nano /etc/security/limits.conf

Add at the end:

root soft nofile 1048576 root hard nofile 1048576

To apply it immediately to the current session:

bash
ulimit -n 1048576

Verify:

bash
ulimit -n
cat /proc/sys/fs/file-nr
  • soft nofile 1048576
  • hard nofile 1048576
04

3. I/O Scheduler

For SSDs (most modern VPS), use none or mq-deadline:

bash
# Check current scheduler
cat /sys/block/vda/queue/scheduler

# Set none (optimal for SSD/NVMe)
echo none | sudo tee /sys/block/vda/queue/scheduler

# Make permanent (create udev rule)
echo 'ACTION=="add|change", KERNEL=="vd[a-z]", ATTR{queue/scheduler}="none"' | \
  sudo tee /etc/udev/rules.d/60-scheduler.conf

Replace vda with your disk (check with lsblk).

05

4. Transparent Huge Pages (THP)

Useful for databases (MySQL, PostgreSQL, Redis). Some workloads prefer THP disabled.

bash
# Check current status
cat /sys/kernel/mm/transparent_hugepage/enabled

# Disable for database (recommended for MySQL/Redis)
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
echo never | sudo tee /sys/kernel/mm/transparent_hugepage/defrag

# Make permanent (add to /etc/rc.local or create systemd service)
06

5. Optimize Nginx for high performance

nginx
# /etc/nginx/nginx.conf
worker_processes auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 65535;
    use epoll;
    multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    keepalive_requests 100;

    # Gzip
    gzip on;
    gzip_comp_level 5;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_vary on;

    # Buffer
    client_body_buffer_size 16k;
    client_max_body_size 8m;
    large_client_header_buffers 4 16k;
}
07

6. Optimize MySQL/MariaDB

ini
# /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
# Buffer pool: 70-80% of available RAM
innodb_buffer_pool_size = 1G

# Less conservative writes (slightly less safe, much faster)
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT

# Connections
max_connections = 200
thread_cache_size = 50

# Query cache (disable in MySQL 8+, no longer exists)
# query_cache_type = 0
08

7. CPU power plan

On some KVM hypervisors the default CPU governor is powersave:

bash
# Check current governor
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor 2>/dev/null | head -1

# Set performance
echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

# With cpufrequtils
sudo apt install cpufrequtils -y
sudo cpufreq-set -g performance

# Make permanent
echo 'GOVERNOR="performance"' | sudo tee /etc/default/cpufrequtils
09

8. Swap: zram (alternative to disk swap)

On VPS with little RAM, zram is much faster than disk swap:

bash
sudo apt install zram-tools -y

# Configure: /etc/default/zramswap
echo "ALGO=lz4" | sudo tee -a /etc/default/zramswap
echo "PERCENT=25" | sudo tee -a /etc/default/zramswap  # 25% of RAM as zram

sudo systemctl restart zramswap
# Verify
zramctl
10

9. Performance monitoring tools

bash
# Recommended installation
sudo apt install htop iotop iftop sysstat nethogs -y

# Detailed CPU and memory
htop

# I/O per process
sudo iotop -o

# Bandwidth per process
sudo nethogs eth0

# Disk statistics
iostat -x 1

# Network over time
sar -n DEV 1 10
11

Quick summary (all-in-one script)

bash
#!/bin/bash
# Quick tweaks: run as root

# sysctl
sysctl -w net.core.somaxconn=65535
sysctl -w net.ipv4.tcp_tw_reuse=1
sysctl -w vm.swappiness=10
sysctl -w fs.file-max=2097152

# ulimit
ulimit -n 1048576

# I/O scheduler on SSD
DISK=$(lsblk -d -o NAME | tail -1)
echo none > /sys/block/${DISK}/queue/scheduler

echo "Tweaks applied. Add permanent modifications to sysctl.conf"

DeluxHost, fondata nel 2023, offre soluzioni di hosting di alta qualità per diverse esigenze digitali. Forniamo hosting condiviso, VPS e server dedicati con sicurezza avanzata e datacenter globali.

© DeluxHost, Tutti i diritti riservati. | Partita IVA: IT17734661006
Tutti i sistemi operativi