PHP concept

Posted Jun 15, 20202 min read

PHP Life Cycle

1. Request/response cycle
    1). Presentation layer analysis
        The program starts -> the server processes the request -> response, returns to the client -> the program ends
    2). Low-level analysis
        MINT --> RINT--> PHP execution --> RSHUTDOWN --> MSHUTDOWN
        a. Module initialization phase
            PHP loads the code of each extension and calls its module initialization, to apply for some module required variables, memory allocation, etc.
        b. Request initialization phase(process --> read, after parsing request data)
            PHP initializes the environment variables needed to execute the script for this request.
        c. PHP script execution stage
            The process of PHP code analysis and execution(Zend engine takes control, compiles PHP script code into opcodes and executes them in sequence)
        d. Request end phase
            After the request is processed, it enters the end phase, and PHP will start the cleaning process(clear the symbol table generated when the program runs, and each variable calls the unset function)
        e. Module shutdown phase
            Cleanup of resources, shutdown of PHP modules

PHP garbage collection
1.zval structure storage(type | value | refcount | is_ref)

1) Type type value value refcount reference times is_ref reference status

2.gc_enable |gc_disable
3.gc_collect_cycles
PHP-FPM tuning
1.etc/php-fpm.d/*.conf

pm.max_children = 100 //Maximum number of child processes [pm = static]
pm.start_servers = 20 //Number of child processes created at startup
pm.max_requests = 10240 //When the number of requests processed by a php-cgi process reaches this configuration, it will automatically restart the process
request_terminate_timeout = 400 //Timeout termination time for a single request, the process will be terminated after timeout, nginx finds that the signal is broken, it will return a 502 error to the client
pm.min_spare_servers = 10 //Ensure that the minimum number of idle processes is less than this value, then create a new child process
pm.max_spare_servers = 30 //Guarantee the maximum number of idle processes, greater than this value, this will be cleaned up

Reference PHP underlying operating mechanism and principle