Optimizing Nginx and PHP-FPM
The default settings of Nginx and PHP-FPM are not enough after installing it. You need to tweak some settings in order to get the highest performance of your server.
I will not cover everything in this article; instead I will focus only on some of the important configuration that you need to change.
Nginx
Nginx is the most important application running in your server as it is the one who serves the request on your server. Nginx settings can be found by running the following command:
nano /etc/nginx/nginx.conf
The two most important variables that you need to change are:
worker_processes
worker_connections
worker_processes value is based on the number of processor cores you may have in your server. To know how many cores you have, issue the following command:
cat /proc/cpuinfo |grep processor
A similar output would look like the following:
processor : 0
processor : 1
processor : 2
processor : 3
This means that you have four (4) cores. So you need to change the value of worker_processes to:
worker_processes 4;
Please take note that if your server supports hyperthreading, the number of cores will double. So, instead of four (4) it will now become (8). I still recommend you use only the real number of core your processor has.
worker_connections is the number of connection each worker_processes may handle. Maximum number of clients is handled by worker_processes times worker_connections. This is the formula:
max clients = worker_processes * worker_connections
Max clients is equivalent to max_clients in Apache web server.
Another important setting that you need to change is the “client_max_body_size”.
This variable is very important if you are serving downloadable files like here in sourcecodester.com. In order to upload large file, you need to set the client_max_body_size to its highest possible value. In our case, we allow 50MB each upload.
You may also choose between unix socket or tcp/ip connection via fastcgi_pass. If you are just using one server, you may not need the tpc/ip connection. Unix socket is far faster compare with tcp/ip. You may argue with me, but as far as my experience is concerned, unix socket may suffice.
PHP-FPM
As for the PHP-FPM, the important settings that you need to tweak are the following variables:
pm = dynamic
pm.max_children = 64
pm.start_servers = 25
pm.min_spare_servers = 20
pm.max_spare_servers = 64
pm.max_requests = 500
Depending on your physical memory, the above settings is best for 8GB to 32GB RAM.
As for PHP-FPM process manager, I use dynamic. This means that the process is started only when it is needed.
If you are sharing a lot of websites on the same server and using a different PHP-FPM pool, I recommend you use ondemand process. As it will release the memory used after the connection is closed.
pm.max_requests is also important when setting this variable as it will control how your PHP script works. It will spawn a connection every 500 requests. So, even if you have a bad PHP script that runs infinitely, it will still be spawn if it reaches 500 requests.
If you still run out of memory after setting the value above, make sure you monitor the inactive memory used by your system by following my previous article on “How to Clear Inactive Memory in Linux”.
Note: This article is intended only if you are on a dedicated server.
Add new comment
- 44 views