Server optimization for VPS and dedicated servers
Improve Linux server performance with swap configuration, web server tuning, PHP-FPM, MySQL optimization, and caching.
A freshly provisioned VPS often needs tuning to handle real traffic. These are practical, commonly-applied changes for Linux servers running a web stack. Apply changes one at a time and monitor the effect.
Swap space
Swap prevents out-of-memory crashes by using disk as overflow RAM. Slow but better than crashing. Add a 1 GB swap file on a VPS with 1–2 GB RAM:
fallocate -l 1G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab
Set swappiness to a low value (use RAM first):
echo 'vm.swappiness=10' >> /etc/sysctl.conf
sysctl -p
PHP-FPM tuning
PHP-FPM runs PHP processes as a pool. Key pool settings in /etc/php/8.x/fpm/pool.d/www.conf:
pm = dynamic
pm.max_children = 10 ; Max simultaneous PHP processes
pm.start_servers = 2 ; Processes started on launch
pm.min_spare_servers = 1 ; Min idle processes
pm.max_spare_servers = 3 ; Max idle processes
pm.max_requests = 500 ; Restart process after N requests (prevents memory leaks)
Rule of thumb: max_children = available RAM / average PHP process size. Check average process size with ps aux | grep php.
MySQL optimization
Key settings in /etc/mysql/mysql.conf.d/mysqld.cnf for a 2 GB VPS:
innodb_buffer_pool_size = 512M ; Main InnoDB cache-50-70% of available RAM
query_cache_type = 0 ; Disable query cache (deprecated in MySQL 8)
max_connections = 100 ; Limit connections to prevent overload
tmp_table_size = 64M
max_heap_table_size = 64M
Restart MySQL after changes: systemctl restart mysql
Nginx optimizations
In /etc/nginx/nginx.conf:
worker_processes auto; ; Match CPU core count
worker_connections 1024; ; Per-worker connections
gzip on; ; Enable gzip compression
gzip_types text/plain text/css application/json application/javascript text/xml;
client_max_body_size 64M; ; Allow larger file uploads
keepalive_timeout 65;
sendfile on;
tcp_nopush on;
Caching layers
| Cache type | Tool | Benefit |
|---|---|---|
| Opcode cache | OPcache (built into PHP) | Caches compiled PHP bytecode-major speedup for all PHP sites |
| Object cache | Redis or Memcached | Caches database query results-reduces MySQL load |
| Page cache | Nginx FastCGI cache or WordPress plugin | Serves pre-rendered HTML-eliminates PHP execution for cached pages |
Enable OPcache in /etc/php/8.x/fpm/php.ini: set opcache.enable=1 and opcache.memory_consumption=128.
Need optimization help?
Our NOC team can help tune your server configuration for peak performance.





















