Ultimate Ubuntu 20.04 Optimization Guide for High-Performance WordPress Hosting

 

 

Ultimate Ubuntu 20.04 Optimization Guide 2

Ultimate Ubuntu 20.04 Optimization Guide 2

๐Ÿง  TL;DR for Experts

  • Use ZRAM + tmpfs overlays to minimize disk I/O
  • Switch to ondemand CPU governor only when on-demand spikes are critical, otherwise lock to performance
  • Use Tuned + CPU pinning for Nginx/MySQL affinity
  • Leverage FPM pools isolation per WP site
  • Replace EXT4 with XFS or tuned ZFS for transactional performance
  • Tune sysctl for network and inode acceleration
  • Employ nghttpx or hitch for aggressive TLS offloading
  • Set opcache.validate_timestamps=0 and preload opcache with handcrafted opcache.preload.php
  • Deploy Redis object caching with UNIX sockets, not TCP
  • Eliminate swap unless ZRAM is used in a compressed RAM zone
  • Trim autostart units, remove Snap packages, use prelink and anacron sparingly

๐Ÿงฐ 1. Base System Cleanup (Beyond apt autoremove)

๐Ÿงผ Remove Snap Completely

Snap introduces background daemons, auto-updates, and significant disk overhead:

sudo apt purge snapd
rm -rf ~/snap /snap /var/snap /var/lib/snapd

๐Ÿ› ๏ธ Replace with Flatpak or direct .deb when needed

WordPress needs minimal GUI or sandboxed apps โ€” skip Snap entirely.


โš™๏ธ 2. Kernel & CPU Optimizations

๐Ÿงฎ Switch to Low Latency Kernel (if not already installed)

For better I/O and scheduling for PHP+MySQL:

sudo apt install linux-lowlatency

๐Ÿšฆ Set CPU governor to performance (not powersave)

sudo apt install cpufrequtils
echo 'GOVERNOR="performance"' | sudo tee /etc/default/cpufrequtils
sudo systemctl disable ondemand
sudo systemctl restart cpufrequtils

๐Ÿ“Œ CPU Pinning for Process Affinity

Use taskset or tuned to pin Nginx/MySQL to separate cores:

sudo apt install tuned
sudo tuned-adm profile throughput-performance

Or manually:

taskset -c 0-1 /usr/sbin/nginx
taskset -c 2-3 /usr/sbin/mysqld

๐Ÿงฑ 3. Filesystem and I/O Layer

๐Ÿ”„ Replace EXT4 with XFS or tuned ZFS

XFS is superior under high-concurrency writes (logs, cache, db):

sudo apt install xfsprogs

Mount with:

UUID=XXXX-XXXX / ext4 noatime,nodiratime,discard,errors=remount-ro 0 1

Use noatime, nodiratime, and discard to reduce disk writes.


๐ŸงŠ Enable TRIM for SSDs (Weekly)

sudo systemctl enable fstrim.timer
sudo systemctl start fstrim.timer

๐Ÿš€ Enable ZRAM to reduce swap thrashing:

sudo apt install zram-config

Or custom config (for more control):

echo "zram" | sudo tee -a /etc/modules-load.d/zram.conf

๐Ÿ›œ 4. Network & TCP Stack Enhancements

๐Ÿ”ง sysctl Tweaks for Better Web Throughput

Create /etc/sysctl.d/99-tweaks.conf:

# Increase max file descriptors
fs.file-max = 2097152

# TCP buffer tuning
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216

# Enable TCP fast open
net.ipv4.tcp_fastopen = 3

# Reduce FIN_WAIT time
net.ipv4.tcp_fin_timeout = 15

# More simultaneous connections
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 8192

Apply:

sudo sysctl --system

๐ŸŒ 5. Web Stack Enhancements (Nginx + PHP-FPM + Redis)

๐Ÿงฌ Nginx Tweaks

In /etc/nginx/nginx.conf:

worker_processes auto;
worker_connections 65535;
use epoll;
multi_accept on;
keepalive_timeout 15;
sendfile on;
tcp_nopush on;
tcp_nodelay on;

๐Ÿงช PHP-FPM Tuning

In /etc/php/7.4/fpm/php-fpm.conf:

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

And /etc/php/7.4/fpm/php.ini:

opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.validate_timestamps=0
opcache.preload=/var/www/html/opcache.preload.php

Then create a preload file:

<?php
// opcache.preload.php
opcache_compile_file('/var/www/html/wp-includes/load.php');
// Add more core files here

๐Ÿง  Use Redis via Unix Socket

Install Redis and PHP Redis:

sudo apt install redis-server php-redis

Edit /etc/redis/redis.conf:

unixsocket /run/redis/redis.sock
unixsocketperm 770

Then in wp-config.php:

define('WP_REDIS_PATH', '/run/redis/redis.sock');
define('WP_REDIS_CLIENT', 'phpredis');

๐Ÿ“‚ 6. MySQL / MariaDB Enhancements

In /etc/mysql/mariadb.conf.d/50-server.cnf:

innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
max_connections = 500
query_cache_size = 64M
query_cache_type = 1
tmp_table_size = 128M
max_heap_table_size = 128M

Also, consider using Aria for temporary tables (aria_pagecache_buffer_size = 128M).


๐ŸŽฏ 7. WordPress-specific Enhancements

๐Ÿ”’ Disable WP-Cron and use system cron

Add to wp-config.php:

define('DISABLE_WP_CRON', true);

Then schedule in crontab:

*/5 * * * * www-data /usr/bin/php /var/www/html/wp-cron.php > /dev/null 2>&1

๐Ÿงฌ Use Object Caching Plugins

Use Redis Object Cache plugin, backed by Redis UNIX socket.


๐Ÿงช 8. Advanced Options (Optional but Powerful)

๐Ÿงฌ Use nghttpx for HTTP/2 Proxy

Handles TLS offload and multiplexed streams better than Nginx:

sudo apt install nghttp2-proxy

๐Ÿงช Use prelink for faster binary loading

sudo apt install prelink
sudo prelink -amR

๐Ÿ’ค Disable Unused Services

systemctl disable avahi-daemon.service
systemctl disable ModemManager.service
systemctl disable bluetooth.service

๐Ÿงพ Bonus: Systemd Service Audit

Check all services:

systemctl list-unit-files --state=enabled

Use systemd-analyze blame to diagnose slow services.


๐Ÿ”š Summary: The Performance Stack

LayerOptimization
Kernel/CPULow-latency kernel, tuned governor
FilesystemXFS/ZFS, noatime, fstrim
I/O/SwapZRAM, no swap, tmpfs for /tmp
PHPPreloading, opcache tuning, socket Redis
MySQLPool + buffer tuning
Nginxepoll, tuning worker/keepalive
WordPressRedis, cron, object cache, preload
ServicesSnapless, socket services, pinned cores

๐Ÿ” Next Steps

  1. Benchmark before and after with:
    ab -n 1000 -c 10 http://localhost/
    
  2. Monitor system performance using:
    • htop, iotop, iftop, netdata
  3. Export to an image snapshot or AMI once stable

Would you like a downloadable Bash script that applies the above optimizations automatically (with flags)? Or perhaps an Ansible playbook version for multi-node WP deployments?