
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 tunedZFS
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
andanacron
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
Layer | Optimization |
---|---|
Kernel/CPU | Low-latency kernel, tuned governor |
Filesystem | XFS/ZFS, noatime, fstrim |
I/O/Swap | ZRAM, no swap, tmpfs for /tmp |
PHP | Preloading, opcache tuning, socket Redis |
MySQL | Pool + buffer tuning |
Nginx | epoll, tuning worker/keepalive |
WordPress | Redis, cron, object cache, preload |
Services | Snapless, socket services, pinned cores |
๐ Next Steps
- Benchmark before and after with:
ab -n 1000 -c 10 http://localhost/
- Monitor system performance using:
htop
,iotop
,iftop
,netdata
- 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?