Compiling and Tuning a LEMP Stack (Linux, Nginx, MySQL, PHP-FPM)
A standard LEMP installation provides a functional environment, but for high-traffic applications on Hovixa NVMe infrastructure, manual tuning of the inter-process communication (IPC) and resource limits is mandatory. This guide focuses on the technical adjustments required to maximize throughput and minimize latency across the stack.
1. Nginx: Worker Processes and Connections
Nginx uses an event-driven architecture. To optimize performance, the number of worker processes should generally match the number of vCPU cores allocated to your Hovixa VPS.
# Edit /etc/nginx/nginx.conf
worker_processes auto; # Automatically detects vCPU cores
worker_connections 1024; # Max connections per worker
multi_accept on; # Accept multiple connections at once
use epoll; # Efficient connection handling for Linux
2. PHP-FPM: Pool Management
The pm (process manager) setting determines how PHP children are spawned. For production, pm = static is often preferred to eliminate the overhead of dynamically spawning processes, provided you have sufficient RAM.
# Edit /etc/php/8.x/fpm/pool.d/www.conf
pm = static
pm.max_children = 50 # Adjusted based on (Total RAM - System RAM) / PHP process size
pm.max_requests = 500 # Prevents memory leaks by restarting children
3. MySQL/MariaDB: Memory and Buffers
On a Hovixa VPS, the most impactful MySQL setting is the InnoDB Buffer Pool. This should ideally be large enough to hold your entire dataset in RAM to avoid disk I/O.
| Parameter | Recommended Value | Description |
|---|---|---|
| `innodb_buffer_pool_size` | 50-70% of Total RAM | Caches data and indexes. |
| `innodb_flush_log_at_trx_commit` | 2 (for performance) | Reduces disk flushes for ACID compliance. |
| `query_cache_type` | 0 (Off) | Deprecated; causes locks in high-concurrency. |
4. The Unix Socket Optimization
By default, components often communicate via TCP (127.0.0.1:9000). On a single-server LEMP stack, switching to Unix Domain Sockets reduces CPU overhead by bypassing the network stack.
# In Nginx:
fastcgi_pass unix:/var/run/php/php8.x-fpm.sock;
# In PHP-FPM:
listen = /var/run/php/php8.x-fpm.sock
5. Technical Implementation Details
- Open File Limits: Ensure your OS allows enough open files. Check
ulimit -nand increase it in/etc/security/limits.confto prevent "Too many open files" errors under load. - Gzip/Brotli Compression: Offload CPU-intensive compression tasks to Nginx. While Gzip is standard, Brotli offers better compression ratios for modern browsers.
- Keepalive: Increase
keepalive_timeoutto 65 to allow browsers to reuse connections, reducing the latency of the initial SSL handshake.
Sysadmin Advice: After making these changes, use a tool like **wrk** or **ApacheBench (ab)** to benchmark your server. Monitor the impact on RAM and CPU using **htop** to ensure you haven't over-allocated resources.