php.ini configuration tuning

不言
Release: 2023-03-24 20:46:02
Original
1605 people have browsed it

The content of this article is about php.ini configuration tuning, which has certain reference value. Now I share it with you. Friends in need can refer to it.

The default installed PHP is like The average suit you buy in a department store may fit you well, but it's not perfect. Tuned PHP is like a custom-made suit that matches your exact measurements. However, it should be noted that tuning PHP is only a measure to improve PHP performance and efficiency, and cannot fix poor code and unresponsive API calls.

php.ini file

The PHP interpreter is configured and tuned in the php.ini file. The location of this file varies in different operating systems, and Generally, the php.ini corresponding to the command line and the php.ini file corresponding to PHP-FPM are 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 use the PHP Iniscan tool to scan php.ini to check that security best practices are used.

Memory

When running PHP, you need to care about how much memory each PHP process uses. memory_limit in php.ini is set using Used to set the maximum amount of system memory that a single PHP process can use.

The default value of this setting is 128M, which may be suitable for most small and medium-sized PHP applications. However, if you are running a micro-PHP application, you can lower this value to save money. System resources, on the other hand, if you are running a memory-intensive PHP application, you can increase this value. The size of this value is determined by the available system memory. Determining how much value to allocate to PHP is an art. When deciding how much memory to allocate to PHP and how many PHP-FPM processes it can afford, it can be judged based on the following dimensional information:


  • How much memory can be allocated to PHP? Take a VPS with 2G memory as an example. This device may also run other processes, 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 the process, you can use the command line command top, or you can call the memory_get_peak_usage() function in a PHP script. No matter which method is used, the same script must be run multiple times, and then Take the average memory consumption.

  • How many PHP-FPM processes can you afford? Suppose I allocate 512M memory to PHP and each PHP process consumes an average of 15M memory, then I can afford 34 PHP-FPM processes.

  • Are there enough system resources? Finally, you need to confirm that there are 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 conditions similar to the production environment to determine whether the production environment has sufficient resources. Available.

Zend OPcache

After determining how much memory to allocate, you can configure PHP’s Zend OPcache extension. For detailed information about this extension, please refer to this article: http://laravelacademy .org/post/4396.html.

PHP 5.5.0 has this extension built-in. The following 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 (unit is MB). The allocated memory 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.

  • opcache.interned_strings_buffer = 16: The amount of memory used to store resident strings (unit is MB). What is a resident string? Behind the scenes, the PHP interpreter will find multiple instances of the same string and save the string in memory. If the same string is used again, the PHP interpreter will use a pointer. The purpose of this is to save memory. By default, PHP resident strings will be isolated in each PHP process. This setting allows the PHP-FPM process pool to store all process-resident strings in a shared buffer so that they can be processed in the PHP-FPM process pool. Resident strings are referenced between multiple processes, which 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 that in PHP applications. The number of files is large.

  • 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 checking time interval is set by opcache.revalidate_freq specified. 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. The meaning of setting to 0 seconds is that only when opcache.validate_timestamps is set to 1, the PHP file will be re-validated on every request. Therefore, in the development environment, the PHP file will be re-validated every time, but not in the production environment. verify.

  • opcache.fast_shutdown = 1: This setting allows the opcode to use a faster shutdown step, leaving object destruction and memory release to Zend Engine's memory manager.

File upload

If your application allows file upload, 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 = 1upload_max_filesize = 10Mmax_file_uploads = 3
Copy after login
  • 1

  • 2

  • 3

#By default, PHP allows 20 files to be uploaded in a single request. The maximum uploaded file is 2MB. Here I set it to a single request. Only 3 files can be uploaded at most, and each file can be up to 10MB. Do not set this value too large, otherwise a timeout will occur.

Note: If you have to upload large files, the configuration of the Web server must 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

max_execution_time in the php.ini file is used to set the maximum time a single PHP process can run before being terminated. The default setting of this setting is 30 seconds. It is recommended to set it to 5 seconds:

max_execution_time = 5

Note: set_limit_time() can be called in PHP scripts function overrides this setting.

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 for 10 minutes. We should write a separate PHP file, let it execute in a separate background process, the web application can spawn a separate background process in just a few milliseconds, and then return the HTTP response:

<?phpexec(&#39;echo "create-report.php" | at now&#39;);echo &#39;report pending...&#39;;
Copy after login
  • 1

  • 2

  • 3

##create-report.php In a separate backend Run in the process, after the operation is completed, the database can be updated, or the report can be sent to the recipient via email. However, this usage is rare. More often, we implement similar functions through asynchronous consumption queues. In terms of security, scalability, and maintainability, the effect is better. Related components include lightweight message queues. PHPResque et al.

Handling Sessions

PHP's default session handler can slow down large applications because this handler stores session data on the hard disk, creating unnecessary disk I/O and wasteful time. We should keep session data in memory, for example using Memcached or Redis. This has the added benefit of making it easier to scale later. If the session data is stored on 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 configure the following:

session.save_handler = &#39;memcached&#39;session.save_path = &#39;127.0.0.1:11211&#39;2
Copy after login

Buffered output

If you are sending more data in fewer blocks, and Instead of sending less data in more chunks, the network will be more efficient; that is, delivering content to the visitor's browser in fewer fragments can reduce the total number of HTTP requests.

Therefore, we need to let PHP buffer the output. By default, PHP has enabled the output buffering function. PHP buffers 4096 bytes of output before sending the content to the Web server. The recommended configuration is as follows:

output_buffering = 4096implicit_flush = false2
Copy after login

Note: If you want to modify the output buffer size, make 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 path used by the application, so that every time a file is included or imported, there is no need to constantly search for the include path. This cache is called the real path cache ( realpath cache), if you are running large PHP files (such as Composer components) and use a large number of files, increasing the size of the PHP real path cache can get 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 you can use a little trick: First, increase the size of the real path cache and set it to a particularly large value. For example, 256K. Then, 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
Copy after login
Related recommendations:

In-depth introduction to the main session configuration in PHP.ini

Detailed explanation of common configurations in php.ini

The above is the detailed content of php.ini configuration tuning. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!