Home > Backend Development > PHP Tutorial > How to configure php.ini and perform PHP performance tuning?

How to configure php.ini and perform PHP performance tuning?

Release: 2023-04-10 22:44:02
forward
5504 people have browsed it

How to perform PHP performance tuning? The following article will introduce you to some methods of configuring php.ini to see how to maximize PHP performance? I hope to be helpful!

How to configure php.ini and perform PHP performance tuning?

PHP used in production environments needs to be optimized to allow PHP itself to perform better. In addition to writing PHP code, you must also configure php.ini . Below we will explain the configuration tuning of php.ini from the aspects of memory, file upload, session buffer output, and real path cache.

Memory

Default setting

memory_limit = 128M
Copy after login

The maximum memory that can be used by a single process. This value can be set from Consider the following points:

  • Type of application. If it is a memory-intensive application, you can increase this value;

  • The average memory consumed by a single PHP process. This value can be averaged by running the same script multiple times;

  • How many php-fpm processes can it afford; this value is equal to the total allocated memory divided by the average memory consumed by a single PHP process

File upload

Default settings

file_uploads = On
max_file_uploads = 20
upload_max_filesize = 2M
max_execution_time = 30 值 为 0 代表没有限制
Copy after login
  • Set max_file_uploads to determine how many files are allowed to be uploaded at the same time;

  • Set upload_max_filesize to determine the maximum value for each file upload;

  • If it is a long-term task, try to use a queue to process it, so the value of max_execution_time can be shortened appropriately;

Note that the web server can also set the file upload size and timeout, not just the php.ini settings;

Session

PHP's session is saved on the hard disk by default

session.save_handler = files
Copy after login

In actual applications, the session should be saved in memory. You can use Memcached or Redis. There are two main benefits of doing this:

  • Increase speed;

  • It helps with later expansion. If the session data is stored on the hard disk, It is not convenient to add additional servers. If the session data is stored in Memcached or Redis, any distributed PHP-FPM server can access the session data.

You can install the memcached extension through PECL and set the default save_handler to memcached

session.save_handler = 'memcached'
session.save_path = '127.0.0.1:11211'
Copy after login

Buffered output

Default value

output_buffering = 4096
Copy after login

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.

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

Default value

realpath_cache_size = 4M
realpath_cache_ttl = 120
Copy after login

PHP will cache the file path used by the application, so that each When you include or import a file, you no longer need to constantly search for the include path. This cache is called the real path cache. If you are running a large PHP file (such as the Composer component) and use a large number of files, increase the number of PHP real path caches. size for better performance.

Recommended: "PHP Video Tutorial"

The above is the detailed content of How to configure php.ini and perform PHP performance tuning?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:微信公众号-PHP自学中心
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