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

memory_limit = 128M

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

file_uploads = On

max_file_uploads = 20

upload_max_filesize = 2M

max_execution_time = 30 A value of 0 means no limit

  • 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

session.save_handler = files

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

session.save_handler = 'memcached'

session.save_path = '127.0.0.1:11211'

Buffered output

Default Value

1

output_buffering = 4096

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

realpath_cache_size = 4M

realpath_cache_ttl = 120

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.

Related articles

Six design patterns commonly used in PHP

Singleton mode private static variables (to store instances), private constructor methods (to prevent creation of instances), private clone methods (to prevent cloning of objects), public static methods (to provide instances to the outside world)

16 PHP magic methods

In PHP, methods named starting with a double underscore (__) are called magic methods in PHP, and they play a very important role in PHP. Magic methods include

How to split a string into an array in php

In php, you can use the str_split() function to split a string into an array, which splits the string into several substrings of the desired length and combines them into an array; syntax "str_split(string,length)".

php crop image steps

php crop image steps: 1, create a PHP sample file; 2, use "function imageCropper(){...}" method to achieve the image does not change the cropping; 3, through the "function imageZoom (){...}" method to achieve proportional cropping of the image.

How to replace alt with php regular?

php regular to achieve the replacement alt method: 1, create a PHP sample file; 2, get the content to be replaced; 3, through "<img.*?src=[\"|\'](. *?) [\"|\']. *? >" can be replaced by the regular implementation.

How to convert svg to svg in php?

php svg to jpg method: 1, create a PHP sample file; 2, by "public function svgtojpg(){$image =...}" method to achieve the conversion.

Two basic output methods for php

The two basic output methods of php are "echo" and "print". echo is used to output one or more strings, which can take multiple arguments and have no return value, with the syntax "echo($ str)"; and print is used to output a string that can only take one

How to display a mess of php images?

php image display a mess of solutions: 1, open the corresponding PHP code file; 2, in the page header plus "header ("Content-Type: image/jpg");" code to declare the image type can be.

php can't load js css

php can not load js css solution: 1, check the correct path to load; 2, prohibit the use of absolute path; 3, use relative path to load the file can be.

php how to check user ip

php query user ip method: 1, get user ip by "$_SERVER["REMOTE_ADDR"]" way; 2, get user ip by "($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];" to get the user ip and so on.