Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Performance tuning of php.ini file configuration under php

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "php.ini file configuration performance tuning under php". In daily operation, I believe that many people have doubts about php.ini file configuration performance tuning under php. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "php.ini file configuration performance tuning under php". Next, please follow the editor to study!

Php.ini file

The PHP interpreter is configured and tuned in the php.ini file, which has different locations in different operating systems, and the general command line corresponding php.ini and PHP-FPM corresponding php.ini files 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 scan the php.ini using the PHP Iniscan tool to check for security best practices.

Memory

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

The default value for this setting is 128m, which may be appropriate for most small and medium-sized PHP applications, but if you are running mini PHP applications, you can lower this value to save system resources, and vice versa, if you are running memory-intensive PHP applications, 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 is allocated to PHP. When determining how much memory is allocated to PHP and how many PHP-FPM processes can afford, you can judge based on the following dimensional information:

How much memory can be allocated to PHP in total? Take a VPS with 2G memory as an example, and 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 is consumed per PHP process 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 the PHP script. Either way, you have to run the same script multiple times and then take the average of the memory consumption.

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

Do you have enough system resources? Finally, you need to make sure that there are sufficient system resources to run the PHP application and handle the expected traffic.

Note: we should use Apache Bench or Siege to stress test PHP applications under conditions similar to production environments to determine whether sufficient resources are available in the production environment.

Zend OPcache

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

This extension is built into PHP 5.5.0 +. 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 hold the opcodes compiled by all PHP scripts in the application, which can be set to different sizes depending on the volume of the application.

Opcache.interned_strings_buffer = 16: the amount of memory used to store the resident string (in MB). What is the resident string? The PHP interpreter will find multiple instances of the same string behind it and keep the string in memory. If you use the same string again, the PHP interpreter will use pointers to save memory. By default, PHP resident strings are isolated in individual PHP processes, and this setting allows the PHP-FPM process pool to store all process resident strings in a shared buffer so that resident strings are referenced among multiple processes in the PHP-FPM process pool, saving more memory.

Opcache.max_accelerated_files = 4000: the maximum number of PHP scripts that can be stored in the opcode cache is between 2000 and 100000, which must be larger than the number of files in the PHP application.

Opcache.validate_timestamps = 1: if the value of this setting is 1, PHP will check whether the content of the PHP script has changed after a period of time, and the check interval is specified by the opcache.revalidate_freq setting. If the value of this setting is 0 PHP PHP will not check if the content of the PHP script has changed, we must clear the cached opcode ourselves. It is recommended that you 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. A setting of 0 seconds means that the PHP file is revalidated on each request only if opcache.validate_timestamps is set to 1, so the PHP file is revalidated every time in the development environment and not in the production environment.

Opcache.fast_shutdown = 1: this setting allows the opcode to use faster downtime steps, leaving object deconstruction and memory release to Zend Engine's memory manager.

File upload

If your application allows files to be uploaded, 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

By default, PHP allows 20 files to be uploaded in a single request, with a maximum of 2MB. Here, I set a maximum of 3 files to be uploaded in a single request, with a maximum of 10MB for each file. This value should not be set too high, otherwise a timeout will occur.

Note: if you have to upload large files, the configuration of the Web server should 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 runnable time for a single PHP process before it is terminated. This setting defaults to 30 seconds, and it is recommended to set it to 5 seconds:

Max_execution_time = 5

Note: the set_limit_time () function can be called in the PHP script to override this setting.

Suppose we want to generate a report and make the result into a PDF file. This task may take 10 minutes to complete, but we certainly do not want to keep the PHP request waiting for 10 minutes. We should write a separate PHP file and let it be executed in a separate background process. The Web application can generate a separate background process in a few milliseconds and then return the HTTP response:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report