PHP used in production environment needs to be optimized to make PHP itself perform better, besides writing good PHP code, we also need to configure php.ini. below we will explain the configuration tuning of php.ini from the aspects of memory, file upload, session buffer output, real path cache.
Memory
Default Settings
1 |
|
The maximum amount of memory that can be used by a single process, this value can be set by considering the following points:
-
The type of the application. This value may be increased if it is a memory-centric application.
-
The average amount of memory consumed by a single PHP process, which can be averaged by running the same script multiple times.
-
How many php-fpm processes can be burdened; this value is equal to the total memory allocated divided by the average memory consumed by a single PHP process
File Upload
Default Settings
1 2 3 4 |
|
-
setting max_file_uploads to determine how many file uploads are allowed at the same time.
-
Set upload_max_filesize to determine the maximum value per file upload.
-
In case of long tasks, try to use queues for processing, 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.
Sessions
PHP sessions are saved to the hard drive by default
1 |
|
In practice, sessions should be stored in memory. This has two main advantages:
-
Increasing speed.
-
Useful for later scaling, if the session data is stored on the hard disk, it is not easy 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 via PECL and set the default save_handler to memcached
1 2 |
|
Buffered output
Default Value
1 |
|
Delivering content to the visitor's browser in fewer fragments reduces the total number of HTTP requests. Therefore, we want to have PHP buffer the output, which by default is already enabled, and PHP buffers 4096 bytes of output before sending the content to the web server.
Note: If you want to modify the size of the output buffer, make sure to use a value that is a multiple of 4 (32-bit systems) or 8 (64-bit systems).
Real path caching
Default Value
1 2 |
|
PHP caches the file paths used by the application so that each time you include or import a file you don't have to keep searching for the include path. This cache is called the realpath cache, and if you are running a large PHP file (such as a Composer component) that uses a lot of files, increasing the size of the PHP realpath cache will give you better performance.