How to configure php.ini and perform PHP performance tuning?
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!
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
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 代表没有限制
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
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'
Buffered output
Default value
output_buffering = 4096
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
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!

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
