Speed is one of the most important factors in hosting websites and web apps. With a fast server, you will have happier users, better SEO, and a smoother experience, and if you are running PHP sites, two tools make all the difference, including Nginx and PHP-FPM. This guide will teach you how to optimize Nginx and PHP-FPM on VPS for a faster experience.
If you are looking for optimized performance, our flexible and high-performance VPS hosting solutions provide fully managed environments with pre-tuned configurations, ensuring your PHP applications run ultra-fast from day one.
Requirements To Optimize Nginx and PHP-FPM on VPS
This guide provides a complete process to install a high-performance web server stack on an Ubuntu VPS.
You must access your server as a non-root user with sudo privileges and install the required packages, Nginx mainline, and PHP 8.3 from the Ondřej Surý PPA.
Run the system update and install the required packages with the following command:
You can use the following command to run a quick benchmark:
Bash
sudowrk-t2-c50-d30shttp://127.0.0.1/
Record the requests and latency; we will compare them after tuning.
System-Level VPS Optimization
In this step, you can perform system tuning that is safe for most VPSs. You can adjust the number of files and network connections the system can handle, and the kernel's TCP networking stack.
This prevents your server from becoming a bottleneck under load.
Tuning File Descriptors and Connection Limits
The default limit on open files and connections (file descriptors) on Linux is often too low for a busy web server. Nginx uses a file descriptor for each client connection, and if this limit is reached, the server will start refusing requests.
You can safely raise this global limit with the following command:
Bash
echo'* soft nofile 1048576*hardnofile1048576rootsoftnofile1048576roothardnofile1048576' | sudo tee /etc/security/limits.d/99-nofile.conf
Then, create the system configuration directory and set the system-wide file descriptor limit for services with the commands below:
Bash
sudomkdir-p/etc/systemd/system.conf.decho'[Manager]DefaultLimitNOFILE=1048576' | sudo tee /etc/systemd/system.conf.d/99-nofile.conf
Reload the system configuration to apply the changes:
Bash
sudosystemctldaemon-reexec
Optimizing Kernel TCP and Networking Parameters
The Linux kernel's default network settings are designed for general-purpose use, not high-performance web serving. You can optimize it for a higher number of concurrent connections by increasing connection backlogs, enabling socket reuse to reduce TIME-WAIT states, and tuning TCP buffer sizes for better throughput.
The settings we use are kept moderate intentionally, as they work well on most VPS providers and still provide a clear performance boost.
Bash
sudotee/etc/sysctl.d/99-tuned.conf>/dev/null<<'SYS'net.core.somaxconn=65535net.core.netdev_max_backlog=16384net.ipv4.tcp_max_syn_backlog=8192net.ipv4.tcp_syncookies=1net.ipv4.tcp_fin_timeout=15net.ipv4.tcp_tw_reuse=1net.ipv4.ip_local_port_range=1024065535# Keepalive to detect dead peersnet.ipv4.tcp_keepalive_time=120net.ipv4.tcp_keepalive_intvl=30net.ipv4.tcp_keepalive_probes=5# Reduce TIME-WAIT pressure without being too aggressivenet.ipv4.tcp_rmem=40961310728388608net.ipv4.tcp_wmem=40961310728388608SYS
Reload the system settings to apply the changes:
Bash
sudosysctl--system
PHP-FPM Performance Tuning
While system tuning helps your server run faster overall, most of the real performance boost happens when you optimize PHP-FPM. In this step, we’ll look at two key things:
Speeding up PHP execution with OPcache and JIT.
Adjusting the PHP-FPM process manager so it can handle your site’s traffic efficiently without wasting memory or CPU.
Setting these up correctly can make PHP run 2 to 5 times faster and keep your server’s performance much more stable.
PHP-FPM OPcache and JIT Configuration
In this step, you can set up OPcache to use memory efficiently and keep cached scripts longer, and also turn on the Just-In-Time (JIT) compiler for an extra speed boost by converting PHP code directly into machine code.
Open the PHP-FPM Ini file with the following command:
Bash
sudonano/etc/php/8.3/fpm/php.ini
Add the following OPcache and JIT settings at the end of the file:
Also, find the lines below and change their values to:
Bash
expose_php=0memory_limit=256M
PHP-FPM Process Manager Configuration
PHP-FPM’s process manager (pm) controls how PHP handles multiple requests at the same time. Picking the wrong mode can waste server resources or even cause crashes when traffic gets heavy.
In this step, we will explain three main process management methods, including on-demand, static, and dynamic. Additionally, you will learn an easy method to calculate the optimal number of PHP worker processes based on your server's memory.
Choose Process Manager Models
Open the PHP-FPM pool configuration file with the command below:
Bash
sudonano/etc/php/8.3/fpm/pool.d/www.conf
Choose one of the following process manager models and edit the file:
Option A: On-demand, which is good for sites with spiky traffic:
Once you are done, apply the changes by reloading PHP-FPM:
Bash
sudosystemctlreloadphp8.3-fpm
Nginx Global Performance Tuning
At this point, you can configure Nginx at a global level to handle high concurrency, implement smart caching strategies, and reduce latency through TCP optimizations and compression.
Open the Nginx configuration file with the command below:
Once you are done, check for syntax errors and reload Nginx to apply the changes:
Bash
sudonginx-tsudosystemctlreloadnginx
Maximizing PHP Performance: Safe Deployment with OPcache
Since you have set opcache.validate_timestamps=0 to get the best performance, PHP won’t automatically notice new code changes. That means you need a reliable way to deploy updates so new code runs right away.
This process safely clears OPcache and reloads PHP-FPM without losing the speed benefits:
At this step, you can use the same tools from the baseline test and measure your optimizations.
Warm cache with a quick run:
Bash
wrk-t2-c50-d15shttp://127.0.0.1/
Then measure with:
Bash
wrk-t4-c200-d60shttp://127.0.0.1/
After applying the optimizations, you should notice:
More requests per second, especially for cached GET requests
Lower p95 latency on cached pages or endpoints
CPU usage stays below 80% at your target request rate
Memory usage stays within expected limits; you can check with htop or ps.
That's it, you are done with optimizing Nginx and PHP-FPM on VPS for a faster experience.
Conclusion
Optimizing PHP-FPM and Nginx on VPS provides faster page loads, higher throughput, and stable resource usage. You just need to remember that every VPS and app is unique, you must start with safe and measured defaults, benchmark before and after changes.
With PerLod Hosting, you will get the best-tuned server stack and robust managed hosting.
We hope you enjoy this guide. Subscribe to our X and Facebook channels to get the latest articles on VPS performance optimization.
Dynamic: Best for steady workloads with variable traffic; automatically adjusts workers.
Static: Fixed number of workers; ideal for high, predictable load.
Ondemand: Excellent for low or spiky traffic, saving memory.
For Nginx, use stub_status or log headers like X-Cache to track hits/misses.
For PHP-FPM, enable pm.status_path and slow logs to track worker utilization and slow scripts. Combine with tools like htop, iotop, or wrk for real-time metrics.
After deploying new PHP code, reset OPcache (opcache_reset()) and reload PHP-FPM. This ensures the new scripts are loaded without needing to enable timestamp checks in production.
We use cookies to run PerLod and to understand how the site is used. You can accept everything, or open Manage to turn individual categories off. Cookie policy.