php.ini configuration tuning

The default installation of PHP is like a normal set bought at a department store, which fits but isn't perfect. Tuned PHP is like a custom-made suit, exactly to your size. It's important to note, though, that tuning PHP is just a move to improve PHP performance and efficiency, not bad code and unresponsive API calls.

php.ini file

The PHP interpreter php.iniis configured and tuned in the file, the location of this file is different in different operating systems, and php.inithe php.inifiles are generally separate. Here we assume that the php.ini corresponding to PHP-FPM is configured, but the optimization measures described below apply to all php.ini.

Note: We should first scan php.ini with the PHP Iniscan tool to check that security best practices are used.

RAM

When running PHP, you need to be concerned about how much memory each PHP process uses php.ini. The memory_limitsetting in is used to set the maximum system memory that a single PHP process can use.

The default value of this setting is 128Mthat this is probably fine for most small to medium PHP applications, however, if you are running a tiny PHP application, you can lower this value to save system resources, whereas if you are running a memory-intensive PHP application application, you can increase this value. The size of this value is determined by the available system memory. It is an art to determine how much value to allocate to PHP. When deciding how much memory to allocate to PHP, and how many PHP-FPM processes can be afforded, you can judge based on the following dimensional information:


  • How much memory can be allocated to PHP in total? Taking a VPS with 2G memory as an example, there may be other processes running in this device, such as MySQL, Nginx, etc., so it is appropriate to leave 512M for PHP.
  • How much memory does each PHP process consume on average? To monitor the memory usage of a process, you can use the command top command, or you can call a memory_get_peak_usage()function in a PHP script. Either way, run the same script multiple times, and then take the average memory consumption.
  • How many PHP-FPM processes can I afford? Assuming that I allocate 512M of memory to PHP, and each PHP process consumes an average of 15M of memory, I can afford 34 PHP-FPM processes.
  • Are there sufficient system resources? Finally, make sure you have enough system resources to run the PHP application and handle the expected traffic.

Note: We should use Apache Bench or Siege to stress test the PHP application under production-like conditions to determine if sufficient resources are available in the production environment.

Send OPcache

Once you've determined how much memory to allocate, you can configure PHP's Zend OPcache extension, which is detailed in this article: http://laravelacademy.org/post/4396.html .

PHP 5.5.0+ has this extension built in, here are the settings used to configure and optimize the Zend OPcache extension in the php.ini file:

  • opcache.memory_consumption = 64: The memory allocated for the opcode cache (in MB). The amount of memory allocated should be able to save the opcodes compiled by all PHP scripts in the application. This value can be set to different sizes according to the size of the application. value.
  • opcache.interned_strings_buffer = 16: The amount of memory (in MB) used to store interned strings. What are interned strings? The PHP interpreter will find multiple instances of the same string behind the scenes, save the string in memory, and if the same string is used again, the PHP interpreter will use the pointer, the purpose of which is to save memory. By default, PHP resident strings are isolated in each PHP process. This setting allows the PHP-FPM process pool to store all process resident strings in a shared buffer for Referencing resident strings across multiple processes saves more memory.
  • opcache.max_accelerated_files = 4000: The maximum number of PHP scripts that can be stored in the opcode cache. The range of this value is between 2000 and 100000. This value must be larger than the number of files in the PHP application.
  • opcache.validate_timestamps = 1: When the value of this setting is 1, PHP will check whether the content of the PHP script has changed after a period of time. The check interval is specified by the opcache.revalidate_freq setting. If the value of this setting is 0, PHP will not check whether the content of the PHP script has changed, and we must clear the cached opcodes ourselves. It is recommended to set it to 1 in the development environment and 0 in the production environment.
  • opcache.revalidate_freq = 0: Set how often (in seconds) to check whether the content of the PHP script has changed. Setting it to 0 seconds means that the PHP file will be revalidated on every request only if opcache.validate_timestamps is set to 1, so in a development environment the PHP file will be revalidated every time, but not in a production environment verify.
  • opcache.fast_shutdown = 1: This setting allows the opcode to use a faster shutdown step, handing over object destruction and memory release to Zend Engine's memory manager.

File Upload

If your application allows uploading files, it is best to set the maximum file size that can be uploaded. In addition, it is best to set the maximum number of files that can be uploaded at the same time:

file_uploads = 1
upload_max_filesize = 10M
max_file_uploads = 3
  • 1
  • 2
  • 3

By default, PHP allows 20 files to be uploaded in a single request, and the maximum upload file is 2MB. Here I set it to only upload a maximum of 3 files in a single request, and each file has a maximum of 10MB. Do not set this value too much large, otherwise a timeout will occur.

Note: If you have to upload large files, the configuration of the Web server should also be adjusted accordingly. In addition to setting it in php.ini, adjust the client_max_body_size setting in the Nginx virtual host configuration.

maximum execution time

The max_execution_time in the php.ini file is used to set the maximum time a single PHP process can run before it is terminated. This setting defaults to 30 seconds, it is recommended to set it to 5 seconds:

max_execution_time = 5

Note: This setting can be overridden by calling the set_limit_time() function in a PHP script.

Suppose we want to generate a report and make the results into a PDF file, this task may take 10 minutes to complete, and we definitely don't want to make the PHP request wait 10 minutes, we should write a separate PHP file, let it in a separate background In-process execution, a web application can spawn a separate background process in just a few milliseconds, and then return an HTTP response:

<?php
exec('echo "create-report.php" | at now');
echo 'report pending...';
  • 1
  • 2
  • 3

create-report.phpRuns in a separate background process, which can then update the database or send reports to recipients via email. However, this usage is rare. More often, we use asynchronous consumption queues to achieve similar functions. In terms of security, scalability, and maintainability, the effect is better. The related components have lightweight message queues. PHPResque et al.

handle session

PHP's default session handler can slow down large applications because it stores session data on disk, creating unnecessary disk I/O and wasting time. We should keep session data in memory, eg Memcached or Redis can be used. This has the added benefit of making it easier to scale later. If the session data is stored in the hard disk, it is inconvenient to add additional servers. If the session data is stored in Memcached or Redis, any distributed PHP-FPM server can access the session data.

If you want to save session data in Memcached, you need to do the following configuration:

session.save_handler = 'memcached'
session.save_path = '127.0.0.1:11211'2

buffered output

The network is more efficient if more data is sent in fewer chunks, rather than less data in more chunks, that is, the content is delivered to the visitor in fewer fragments browser, which reduces the total number of HTTP requests.

Therefore, we need to let PHP buffer the output. By default, PHP has enabled the output buffering function. After PHP buffers the output of 4096 bytes, it will send the content to the web server. The recommended configuration is as follows:

output_buffering = 4096
implicit_flush = false2

Note: If you want to modify the size of the output buffer, be sure to use a value that is a multiple of 4 (32-bit systems) or 8 (64-bit systems).

real path cache

PHP will cache the file paths used by the application, so that every time a file is included or imported, there is no need to constantly search the include path. This cache is called the realpath cache (realpath cache). If you are running a large PHP file (such as a Composer component), Using a large number of files, increasing the size of PHP's real path cache can result in better performance.

The default size of the real path cache is 16K. The exact size required for this cache is not easy to determine, but a little trick can be used: first, increase the size of the real path cache, set it to a particularly large value, such as 256K, and then, in Add print_r(realpath_cache_size()); at the end of a PHP script to output the real size of the real path cache. Finally, change the size of the real path cache to this real value. We can set the size of the real path cache in the php.ini file:

realpath_cache_size = 64K

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324960969&siteId=291194637