5
头图
This article has included programming study notes . Covers PHP, JavaScript, Linux, Golang, MySQL, Redis, open source tools and more.

PHP used in the production environment needs to be optimized to make PHP itself perform better. In addition to writing PHP code, php-fpm and php.ini should be configured for tuning. This article explains the configuration and tuning of php.ini from the aspects of memory, OPcache, upload, session, and security.

Compared with other compiled languages, the biggest disadvantage of PHP is that each request will do some module parsing, and the real execution is the work process. The opening of the work process consumes more resources. At the same time, a request will re-parse some code, resulting in repeated parsing.

For the optimization of PHP, you can focus on this aspect to consider optimization.

memory optimization

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

The default value of this setting is 128M, which may be suitable for most small and medium PHP applications, however, if you are running a tiny PHP application, you can lower this value to save system resources, conversely, if you are running a memory-intensive type 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 to allocate to PHP. When deciding how much memory to allocate to PHP, and how many PHP-FPM processes can be afforded, you can judge based on the following dimensional information:

  1. How much memory can be allocated to PHP in total? Taking a VPS with 2G memory as an example, there may be other processes running in this device, such as MySQL, Nginx, etc., so it is appropriate to leave 512M for PHP.
  2. How much memory does each PHP process consume 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 a PHP script. No matter which method is used, you must run the same function multiple times. a script and then take the average of the memory consumption.
  3. How many PHP-FPM processes can I afford? Assuming that I allocate 512M of memory to PHP, and each PHP process consumes an average of 15M of memory, I can afford 34 PHP-FPM processes.

Are there sufficient system resources? Finally, make sure you have enough system resources to run the PHP application and handle the expected traffic. For specific PHP configuration information, please refer to the php-fpm.config configuration file.

 ; Time limit for child processes to wait for a reaction on signals from master.
; Available units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
;process_control_timeout = 0

; The maximum number of processes FPM will fork. This has been designed to control
; the global number of processes when using dynamic PM within a lot of pools.
; Use it with caution.
; Note: A value of 0 indicates no limit
; Default Value: 0
; process.max = 128

; Specify the nice(2) priority to apply to the master process (only if set)
; The value can vary from -19 (highest priority) to 20 (lowest priority)
; Note: - It will only work if the FPM master process is launched as root
;       - The pool process will inherit the master process priority
;         unless specified otherwise
; Default Value: no set
; process.priority = -19

; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
; Default Value: yes
daemonize = no

php-fpm has three operating modes, namely fixed number of processes, number of on-demand processes, and fully dynamic number of processes.

  1. The number of processes on demand is initialized by default. If the input volume is too large, some new process numbers are dynamically created. After the request ends, the newly created process numbers are destroyed.
  2. The number of processes is fixed, and several processes are fixed by default. If the number of processes is not enough, the new request is waiting, and the new request will not be processed until other processes are finished.
  3. The number of completely dynamic processes means that it is completely controlled by the amount of requests. A request is created to create a process, and it is destroyed after processing.

Enable Zend OPcache performance acceleration

Once you've determined how much memory to allocate, you can configure PHP's Zend OPcache extension. OPcache mainly parses some code into bytecode, and there is no need to parse and compile this part of the code repeatedly in subsequent requests. Reducing the process of compiling and parsing can also improve the processing speed of PHP.

PHP5.5.0+ has this extension built-in, and the necessary configuration information is listed below:

opcache.memory_consumption = 64 : The memory allocated for the opcode cache (unit is MB). The allocated memory should be able to save the opcodes compiled by all PHP scripts in the application. This value can be set to different values according to the size of the application. size value.

opcache.interned_strings_buffer = 16 : The amount of memory (in MB) used to store resident strings. What is a resident string? The PHP interpreter will find multiple instances of the same string behind the scenes, save the string in memory, and if the same string is used again, the PHP interpreter will use the pointer, the purpose of which is to save memory. By default, PHP resident strings are isolated in each PHP process. This setting allows the PHP-FPM process pool to store all process resident strings in a shared buffer for Referencing resident strings across multiple processes saves more memory.

opcache.max_accelerated_files = 4000 : The maximum number of PHP scripts that can be stored in the opcode cache. This value ranges from 2000 to 100000. This value must be larger than the number of files in the PHP application.

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

opcache.fast_shutdown = 1 : This setting allows the opcode to use a faster shutdown step, leaving the object destruction and memory release to the Zend Engine's memory manager.

File Upload

If your application allows uploading files, 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, and the maximum upload file is 2MB. Here I set it to only upload a maximum of 3 files in a single request, and each file has a maximum of 10MB. Do not set this value too much large, otherwise a timeout will occur.

Note: If you have to upload large files, the configuration of the Web server should also be adjusted accordingly. In addition to the settings in php.ini, adjust the client_max_body_size setting in the Nginx virtual host configuration.

In addition, if uploading extra-large files, I recommend using the special upload component of Webuploader. The front-end slices the large files, and the back-end PHP merges the sliced data to restore the files. For the application of WebUploader, please refer to the article on this site: A powerful file uploading component - WebUploader.

execution time

max_execution_time is used to set the maximum time a single PHP process can run before it is terminated. This setting defaults to 30 seconds, it is recommended to set it to 5 seconds:

 max_execution_time = 5
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 results into a PDF file, this task may take 10 minutes to complete, and we definitely don't want to make the PHP request wait 10 minutes, we should write a separate PHP file, let it in a separate background In-process execution, a web application can spawn a separate background process in just a few milliseconds and return an HTTP response.

In fact, we are running tasks that take a lot of time to complete, generally using background processes. For example, we can use the swoole extension of PHP to generate reports and send emails in batches. Tasks that take a long time.

handle session

The default situation of PHP is to store the information generated by the session on the disk, such as the so-called session information. When creating and reading a session, I/O operations are performed on the disk. Reading and writing to the disk is actually a time-consuming operation. And the session is not convenient for the session mechanism processing of distributed applications. It is recommended to be placed in in-memory services such as Redis and memcached, with fast read and write speed, and can be processed by a distributed session mechanism.

The following example stores information such as session in memcached memory.

 session.save_handler = "memcached"
session.save_path = "服务地址:端口号"

buffer

The network is more efficient if more data is sent in fewer chunks, rather than less data in more chunks, that is, the content is delivered to the visitor in fewer fragments browser, which reduces the total number of HTTP requests.

Therefore, we need to let PHP buffer the output. By default, PHP has enabled the output buffering function. After PHP buffers the output of 4096 bytes, it will send the content to the web server. The recommended configuration is as follows:

 output_buffering = 4096
implicit_flush = false
If you want to modify the size of the output buffer, be sure to use a value that is a multiple of 4 (32-bit systems) or 8 (64-bit systems).

Security Settings

open_basedir : Use the open_basedir option to control that the PHP script can only access the specified directory, which can prevent the PHP script from accessing files that should not be accessed, and limit the harm of phpshell to a certain extent. We can generally set it to only access the website directory:

 open_basedir = /data/www

disable_functions : Generally, we want to prohibit system functions and prohibit any file and directory operations, such as:

 disable_functions = '.....'

expose_php = Off : Setting this to false will not output PHP version information in the header.

display_errors = Off : In the production environment, we should disable the error prompt. If it is a local development environment, it can be set to On.

log_errors = On : It is recommended to record the error information after turning off display_errors, so as to find the reason for the server running.

error_log : Set the directory where PHP error logs are stored.


Mandy
412 声望627 粉丝