Ubuntu + Nginx Performance Optimization Guide (Production Server) Print

  • Ubuntu, Nginx
  • 0

This guide is intended for Ubuntu servers running Nginx, PHP-FPM, and MySQL/MariaDB hosting multiple websites.

1. Check Server Health

CPU & Load

 
uptime
top
htop
 

Healthy values:

  • Load average should generally be below the number of CPU cores.
  • CPU idle should remain high during normal traffic.

Memory

 
free -h
 

Healthy values:

  • Swap usage = 0 (or minimal)
  • Available RAM should not be close to 0.

Disk Usage

 
df -h
 

Keep partitions below 80–85% usage where practical.


Disk Performance

Install:

 
sudo apt install sysstat -y
 

Check:

 
iostat -xz 1 5
 

Healthy values:

  • %util < 70%
  • await ideally < 10 ms on SSD/NVMe
  • Low aqu-sz

2. Nginx Optimization

Edit

 
sudo nano /etc/nginx/nginx.conf
 

Recommended Configuration

 
user www-data;

worker_processes auto;
worker_rlimit_nofile 65535;

events {
worker_connections 4096;
multi_accept on;
}

http {

sendfile on;
tcp_nopush on;
tcp_nodelay on;

keepalive_timeout 65;
keepalive_requests 1000;

types_hash_max_size 4096;

client_max_body_size 100M;

server_tokens off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

ssl_protocols TLSv1.2 TLSv1.3;

gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_min_length 1024;

gzip_types
text/plain
text/css
application/json
application/javascript
application/xml
text/xml
application/xml+rss
image/svg+xml;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
 

Test Configuration

 
sudo nginx -t
 

Reload:

 
sudo systemctl reload nginx
 

3. PHP-FPM Optimization

Edit:

 
sudo nano /etc/php/8.2/fpm/pool.d/www.conf
 

Recommended starting point for a server with ample RAM:

 
pm = dynamic

pm.max_children = 40
pm.start_servers = 8
pm.min_spare_servers = 8
pm.max_spare_servers = 16

pm.max_requests = 500
 

Restart:

 
sudo systemctl restart php8.2-fpm
 

Check for Worker Exhaustion

 
sudo grep "server reached pm.max_children" /var/log/php8.2-fpm.log
 

If you see:

 
server reached pm.max_children
 

increase pm.max_children.


4. PHP OPcache

Check:

 
php -i | grep opcache
 

Recommended:

 
opcache.enable=1

opcache.memory_consumption=256

opcache.interned_strings_buffer=32

opcache.max_accelerated_files=50000

opcache.validate_timestamps=1

opcache.revalidate_freq=2

opcache.save_comments=1
 

Restart PHP:

 
sudo systemctl restart php8.2-fpm
 

5. MySQL/MariaDB Optimization

Check running queries:

 
mysqladmin processlist
 

or

 
SHOW FULL PROCESSLIST;
 

Install MySQLTuner:

 
sudo apt install mysqltuner -y

mysqltuner
 

Enable slow query logging:

 
slow_query_log=1

long_query_time=2
 

6. Enable Redis

Use Redis for:

  • WordPress Object Cache
  • Laravel Cache
  • Sessions
  • Queues

Install:

 
sudo apt install redis-server
 

Verify:

 
redis-cli ping
 

Expected output:

 
PONG
 

7. Enable FastCGI Cache (Optional)

Suitable for:

  • WordPress
  • CMS
  • Mostly static PHP pages

Benefits:

  • Reduces PHP execution
  • Lowers database load
  • Improves Time To First Byte (TTFB)

8. Monitor Resource Usage

CPU:

 
htop
 

Disk:

 
iotop
 

Network:

 
iftop
 

Open files:

 
lsof
 

9. Monitor Nginx

Access log:

 
tail -f /var/log/nginx/access.log
 

Error log:

 
tail -f /var/log/nginx/error.log
 

10. Monitor PHP

Service status:

 
systemctl status php8.2-fpm
 

Workers:

 
ps aux | grep php-fpm
 

Logs:

 
tail -f /var/log/php8.2-fpm.log
 

11. SSL Optimization

Use only modern protocols:

 
ssl_protocols TLSv1.2 TLSv1.3;
 

12. Security

Hide Nginx version:

 
server_tokens off;
 

Limit upload size if required:

 
client_max_body_size 100M;
 

13. Useful Commands

Check listening ports:

 
ss -tulpn
 

Check services:

 
systemctl status nginx
systemctl status php8.2-fpm
systemctl status mysql
 

Restart services:

 
sudo systemctl restart nginx
sudo systemctl restart php8.2-fpm
sudo systemctl restart mysql
 

14. Performance Checklist

  • worker_processes auto
  • worker_connections 4096
  • worker_rlimit_nofile 65535
  • gzip enabled
  • sendfile enabled
  • tcp_nopush enabled
  • tcp_nodelay enabled
  • keepalive_timeout 65
  • ✅ PHP OPcache enabled
  • pm.max_children sized for workload
  • ✅ Redis enabled (if applicable)
  • ✅ Slow query log enabled
  • ✅ PHP-FPM logs monitored
  • ✅ Nginx configuration tested before reload
  • ✅ Regular system updates and log review

Notes from your server

Based on the diagnostics we performed on your Ubuntu server:

  • 30 vCPUs
  • 62 GB RAM
  • CPU usage was below 1%
  • Disk utilization was around 1–3%
  • No swap usage
  • The main bottleneck was PHP-FPM, with repeated server reached pm.max_children setting (5) warnings.

Increasing the PHP-FPM pool size was the most impactful optimization because the server had ample CPU and memory but was limiting concurrent PHP requests to five workers.

For a hosting environment with multiple websites, consider creating separate PHP-FPM pools for high-traffic sites so one busy application cannot consume all PHP workers and affect the others.

 
 
 

Was this answer helpful?

« Back