I really enjoy optimizing the speed of my
VPS. There is something rewarding about a website loading quickly, isn't there? This is why I like to experiment with
FastCGI settings. A simple change can speed up
PHP, and when I first started doing this, I felt like I had discovered a speed hack.
Why FastCGI Settings are Important
FastCGI acts as an intermediary for your
webserver and
PHP. FastCGI holds
PHP in the background rather than making your server initiate PHP over and over. Choosing the right
FastCGI settings will load your site faster, lighten your
CPU, and provide a smoother experience.
Key FastCGI Settings to Speed Up PHP
1. pm.max_children
This tells your server how many PHP workers it can execute simultaneously.
If this value is too low your site will slow down.
If this value is too high, your
VPS will run out of
RAM.
Easy to startup recommendations:
- 1GB RAM VPS - 5–10 workers
- 2GB RAM VPS - 15–20 workers
Sometimes a single small change will resolve a major slowdown.
2. pm.max_requests
This is how many requests a single PHP worker gets before restarting.
I typically keep this around
500–1000.
This also helps to avoid memory leaks and keeps PHP clean.
3. pm.start_servers, pm.min_spare_servers, pm.max_spare_servers
These settings determine how many idle PHP workers are prepared to respond. If these setting values are set too low, the website will slow down during traffic spikes.
Simple suggested values:
- start_servers: 3-5
- min_spare_servers: 2-4
- max_spare_servers: 6-10
Easy FastCGI Tips
Here are some quick tips that always help things run a little faster:
• Use
PHP-FPM instead of the legacy
PHP methods.
• Enable
OPcache so that your
PHP scripts load into memory instead of the file system.
• Set worker limits that are based on your VPS level of RAM.
• Check the logs to find errors or slow requests.
• Use
htop or similar tools to see what percentage your PHP workers are being used.
By checking your logs sometimes, you can see what is actually the issue causing your site to be slow.