Configuring PHP-FPM on Linux
We will not touch on the basic settings of Nginx server itself and its functionality, we already did this in our previous article. But some parameters need to be considered.
Define and specify the number of handlers and the number of connections per processor. For this purpose, in the file /etc/nginx/nginx.conf we will specify the values:
worker_processes 1; worker_connections 1024;
The basic idea in choosing these values is that the number of clients served is equal to the number of handlers multiplied by the number of connections for each processor. So in our case, it’s 1,024 clients. To avoid input-output problems, follow rule 1 of the processor per processor core. You can determine the number of kernels by command:
cat /proc/cpuinfo | grep processor processor : 0
My processor has one core. Therefore, the number of processors is one. The number of connections can be added depending on the need. This parameter is already selected in practice.
For safety reasons, the directive can be activated
server_tokens off
From the manual: «Allows or forbids issuing Nginx version in error messages and in the "Server" field of the response title.»
This directive is added to the http/server/local section of the configuration file.
Let's specify the size of the data transmitted by the server:
client_max_body_size 20m; client_body_buffer_size 128k;
This directive is added to the http/server/local section of the configuration file.
Configure cache for static files. This is to save server resources and bandwidth. Disable logging and set the header expiration date to 100 days for static files.
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { access_log off; log_not_found off; expires 360d; }
To interact with PHP-FPM, you can specify a Unix socket. To do this, we need to write in our configuration halo:
# Pass PHP scripts to PHP-FPM location ~* \.php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; }
Looking ahead, in the PHP-FPM settings, you should specify to listen to the IP and port specified in our configuration. This is done by the listening directive of the pool configuration file.
For security reasons, you can prevent access to hidden files.
location ~ /\. { access_log off; log_not_found off; deny all; }
The PHP-FPM configuration itself can be divided into 2 steps: setting the global parameters which is carried out in the file
/etc/php5/fpm/php-fpm.conf
and pool configuration. Pool configuration is specified in directory files
/etc/php5/fpm/pool.d/
Global parameters are not so interesting and can be read about in the documentation, but the main parameters of the configuration of pools we will consider.
Each pool is configured with a separate file. If you make a command
ls -l /etc/php5/fpm/pool. d/
we will see that the system already has a configuration file for the www pool. Let’s look at the configuration in its example.
An important point in the configuration is to choose the number of handlers that are used to execute PHP scripts. Their number must be chosen wisely. Too few handlers will not allow efficient and prompt processing of requests, but too many will exhaust the resources that the server requires for other tasks. The number of processors should be chosen from the consideration that even in peak requests to the server, the utilization of its resources should remain within reasonable limits and not lead to its overload.
Now let’s look a little at the configuration file and specify the following options. Oh yes, in the configuration file, the lines beginning with the symbol «;» are comments and are not considered directives.
Specify the dynamic mode of creation of processes, so the number of started PHP-FPM processes will depend directly on the load on the server.
pm = dynamic
Set the maximum number of child processes.
pm.max_children = 6
Next, you must specify the number of child processes that will run as soon as the server is loaded. By default, this value is 2. You can increase it if resources allow; let’s set it to 3.
pm.start_servers = 3
You should also specify the minimum and the maximum number of idle processes. Let’s make the minimum number equal to the number of processes running on the server boot. And we will set the maximum number on the basis that it should not exceed the maximum allowed number of child processes.
pm.min_spare_servers = 3 pm.max_spare_servers = 5
These values are given for example and explanation. But in each specific situation, they may vary. Optimum values depend on the server’s capabilities and available resources, the number and complexity of the PHP-code, the number of queries, and the load on the server. These parameters can be obtained only by practical application and testing of the created site.
Now consider the specific settings.
First, we will configure the logging of information about scripts that are running slowly. This information will be useful in clarifying the reasons for the delays at the site. First, we will set a time threshold, after which the execution of the script will be considered slow.
request_slowlog_timeout = 10s
The time units can be specified in seconds (s), minutes (m), hours (h) or days (d). We have specified a time of 10 seconds.
Now we will also specify the name of the file where the information will be sent.
slowlog = /var/log/slowphp.log
Now, this file can be studied for the study of «slow» scripts.
If you want to run scripts in an isolated environment or chroot environment. This option is usually used for security purposes. You must set the chroot option to enable this feature.
When setting this option, you should remember that this will entail changes in the processing of PHP scripts.
All file paths will now be viewed relative to the specified directory, and PHP scripts will not be able to access sockets located outside the specified directory.
To maintain multiple sites, you may need to add additional pools. This allows you to configure different settings for each site individually. To create an additional pool, copy our file
/etc/php5/fpm/pool.d/www.conf
under a new name, for example
/etc/php5/fpm/pool.d/www2.conf
Open a new file and change the name of the pool. To do this, at the top of the file, change the line [www] to [www2].
And change the line:
listen = /var/run/php5-fpm.sock
on
listen = /var/run/www2.sock
In case we want the handler to listen on the pair ip:port, the listen directive will look like
listen = 127.0.0.1:9000
Then we restart the service
/etc/init. d/php5-fpm restart