Configure an upstream in nginx, and then configure all related server IP. Then the polling scheme is adopted, and in the configuration item of nginx, the proxy pass points to the upstream, so that the load balancing can be realized.
CGI is a general gateway protocol. In order to solve the communication between different language interpreters (such as PHP, Python interpreter) and webserver, a protocol is produced. As long as the protocol is obeyed, the language can communicate with webserver. CGI is a protocol that specifies what data to transfer / in what format to transfer to PHP parser.
It is a protocol that sublimates CGI protocol. Fastcgi is like a long live CGI, which can be executed all the time. As long as it is activated, it will not take time to fork every time (this is the most criticized fork and execute mode of CGI). It also supports distributed computing, that is, fastcgi programs can be executed on hosts other than web servers and accept requests from other web servers..
(PHP fastcgi process manager), php-fpm is a program that implements the fastcgi protocol and is used to manage the processes started by fastcgi, that is, the program that can schedule PHP CGI processes. It also provides the function of process management. Process includes master process and worker process. There is only one master process, which is responsible for listening to the port (9000 by default) and receiving requests from webserver. However, there are generally multiple worker processes (the specific number is configured according to the actual needs). A PHP interpreter is embedded in each process, which is the real execution place of PHP code.
Fastcgi will first fork a master, parse the configuration file, initialize the execution environment, and then fork multiple workers. When the request comes, the master will pass it to a worker, and then immediately accept the next request. In this way, repeated labor is avoided and the efficiency is naturally high. Moreover, when there are not enough workers, the master can start several workers in advance and wait according to the configuration; of course, when there are too many idle workers, some will stop, which improves the performance and saves resources. This is fastcgi’s management of processes. Most fastcgi implementations maintain a process pool. Note: as an HTTP server, spool works in a similar way.
1. User request index.php First, nginx
2. Nginx process steps:
(1) Find route according to configuration
(2) The fast CGI module of nginx (the client of fastcgi) is loaded fastcgi.conf Fastcgi in file_ *The configuration parameter values are also added to the forwarding task
(3) According to nginx.conf File fastcgi_ The pass configuration forwards the request to 127.0.0.1:9000.
3. Php-fpm operation:
(1) The master process of php-fpm listens to port 9000.
(2) call the sub process to process logic after receiving the request. The PHP interpreter explains the PHP syntax and returns it to Nginx.
4. Nginx operation:
Return the response to the user
There are three modules in PHP
a. PHP kernel: used to handle requests, file streams, error handling and other related operations
b. Zend engine: used to convert the source file into machine language, and then run it on the virtual machine
c. Extension layer: a set of functions, class libraries and streams. PHP uses them to perform specific operations
For example, we need mysqli extension to connect to MySQL database; when Zend executive program may need to connect several extensions, Zend engine will give the controller to the extension and return it after processing specific tasks. Finally, Zend engine will return the running results to PHP kernel, which will send the results to SAPI layer, and finally output them to the browser.
The core architecture of PHP is as follows:
a. Zend engine: the whole Zend engine is implemented in pure C, which is the kernel part of PHP. It translates PHP code (morphology, syntax analysis, etc.) into executable opcode processing and implements corresponding processing methods, realizes basic data structure (such as hashtable, OO), memory allocation and management, and provides corresponding API methods for external calls. It is the core of everything, All peripheral functions are implemented around Zend
b. Extensions: around Zend engine, extensions provide various basic services in a component-based way. Our common built-in functions (such as array Series) and standard libraries are implemented through extensions. Users can also implement their own extensions according to their own needs to achieve the purpose of function expansion and performance optimization.
c. SAPI: the full name of SAPI is server application programming interface. That is the application programming interface on the server side. SAPI enables PHP to interact with peripheral data through a series of hook functions. This is a very elegant and successful design of PHP. It successfully decouples PHP itself from the upper application through SAPI. PHP can no longer consider how to be compatible with different applications, but the application itself can realize different processing methods according to its own characteristics
d. Upper application: This is the PHP program we usually write. We can get a variety of application modes through different SAPI methods, such as realizing web application through web server, running in script mode under command line, and so on.
If PHP is a car, then the framework of the car is PHP itself. Zend is the engine of the car. The various components under ext are the wheels of the car. SAPI can be regarded as a road. Cars can run on different types of roads. A PHP program execution is that cars run on the road. Therefore, we need: excellent engine + right wheel + right track.
As mentioned above, SAPI enables external applications to exchange data with PHP through a series of interfaces and implement specific processing methods according to the characteristics of different applications
Apache 2handler: This is based on Apache as web server, using mod_ PHP mode is the most widely used way to deal with runtime.
CGI: This is another direct interaction mode between web server and PHP, that is, the famous fastcgi protocol. In recent years, fastcgi + PHP has been used more and more, and it is also the only way supported by asynchronous web server.
CLI: application mode of command line call
PHP executes this code through the following four steps (specifically, it should be the language engine Zend of PHP)
Scanning (lexing), converting PHP code into tokens
Parsing, which transforms tokens into simple and meaningful expressions
Compilation, which compiles the expression into opocds
Execution, execute opcodes one at a time in order to realize the function of PHP script.
The difference and choice of PHP abstract class and interface class
1. The interface is used through the keyword implements. The use of abstract classes is through the keyword extends. Of course, interfaces can also be inherited through the keyword extensions.
2. Member variables (including class static variables) cannot be declared in an interface, but class constants can be declared. Abstract class can declare various types of member variables to realize data encapsulation. (in addition, all member variables in java interface should be declared as public static final type)
3. Interfaces don’t have constructors. Abstract classes can have constructors.
4. Methods in interfaces are all public by default, while methods in abstract classes can be modified with private, protected and public.
5. A class can implement multiple interfaces at the same time, but a class can only inherit from an abstract class.
If you want to create a model that will be adopted by some closely related objects, you can use abstract classes. If you want to create functions that will be adopted by some unrelated objects, use interfaces.
If you have to inherit behavior from multiple sources, use the interface.
If you know that all classes share a common behavior implementation, use an abstract class and implement the behavior in it.
Abstract classes are not necessarily abstract methods, but can also have specific implementation methods. In this way, the common methods can be promoted to abstract classes, and then the specific methods can be implemented by subclasses themselves (here’s the classic application, template method design pattern). So the abstract class can better realize the reuse of code
Interfaces and abstract classes have different concepts. This can be understood as the interface is the abstraction of action, and the abstract class is the abstraction of root (that is, the abstraction of essence is different from the essence of other classes).
PHP can automatically manage memory and clear objects that are no longer needed.
PHP uses the simple garbage collection mechanism of reference counting. Each object contains a reference counter, and each reference is connected to the object, and the counter is increased by 1. When reference leaves the living space or is set to null, the counter is decremented by 1. When the reference counter of an object is zero, PHP knows that you will no longer need to use the object, freeing up its memory.
This work adoptsCC agreementReprint must indicate the author and the link of this article