


[Transfer] Figure out the relationship between FastCgi and PHP-fpm, fastcgiphp-fpm_PHP tutorial
[Transfer] Find out the relationship between FastCgi and PHP-fpm, fastcgiphp-fpm
Question: Some people on the Internet say that fastcgi is a protocol , php-fpm implements this protocol; Some say that php-fpm is the manager of the fastcgi process, used to manage the fastcgi process; Some say that php-fpm is a patch of the PHP kernel; Some say that it has been modified After configuring the php.ini file, there is no way to restart smoothly, so php-fpm was born; others say that PHP-CGI is PHP’s own FastCGI manager
First of all, what is CGI? CGI is to ensure that the data passed by the web server is in a standard format, which is convenient for writers of CGI programs.
The web server (such as nginx) is just a distributor of content. For example, if /index.html
is requested, the web server will find the file in the file system and send it to the browser. What is distributed here is static data. Okay, if the request now is /index.php
, according to the configuration file, nginx knows that this is not a static file and needs to be processed by the PHP parser, then it will simply process the request and hand it over to the PHP parser. What data will Nginx pass to the PHP parser? The URL must be present, the query string must be present, the POST data must be present, and the HTTP header must be present. Well, CGI is the protocol that stipulates what data is to be transmitted and in what format to be passed to the backend for processing the request. Think carefully about where the users you use in your PHP code come from.
When the web server receives the /index.php
request, it will start the corresponding CGI program, which is the PHP parser. Next, the PHP parser will parse the php.ini file, initialize the execution environment, process the request, return the processed result in the format specified by CGI, and exit the process. The web server then returns the results to the browser.
Well, CGI is a protocol and has nothing to do with processes or anything like that. So what is fastcgi? Fastcgi is used to improve the performance of CGI programs.
Improve performance, so what are the performance problems of CGI programs? "The PHP parser will parse the php.ini file and initialize the execution environment", that's it. Standard CGI will perform these steps for each request (don't be tired! Starting the process is very tiring!), so the time to process each time will be relatively long. This is obviously unreasonable! So how does Fastcgi do it? First, Fastcgi will start a master, parse the configuration file, initialize the execution environment, and then start multiple workers. When a request comes in, the master will pass it to a worker, and then the next request can be accepted immediately. This avoids duplication of work and is naturally highly efficient. And when there are not enough workers, the master can pre-start several workers according to the configuration and wait; of course, when there are too many idle workers, some will be stopped, which improves performance and saves resources. This is fastcgi's process management.
So what is PHP-FPM? It is a program that implements Fastcgi and was officially accepted by PHP.
As we all know, the interpreter of PHP is php-cgi. php-cgi is just a CGI program. It can only parse requests and return results, but does not know how to manage processes (Your Majesty, I really can’t do that!) So there are some programs that can schedule php-cgi processes. For example, spawn-fcgi is separated from lighthttpd. Well, PHP-FPM is also like this. After a long period of development, it has gradually been recognized by everyone (you know, in the past few years, everyone complained about the poor stability of PHP-FPM), and it has become more and more popular.
Okay, let’s finally come back to your question.
1. Some people on the Internet say that fastcgi is a protocol, and php-fpm implements this protocol
Yes.
2. Some say that php-fpm is the manager of fastcgi process, which is used to manage fastcgi process
Yes. The management object of php-fpm is php-cgi. But it cannot be said that php-fpm is the manager of the fastcgi process, because as mentioned earlier, fastcgi is a protocol, and it seems that no such process exists. Even if php-fpm exists, it cannot manage it (at least for now). Some people say that php-fpm is a patch of the PHP kernel. This was true before. Because php-fpm was not included in the PHP kernel at the beginning, to use this function, you need to find php-fpm that is the same as the source code version, patch the kernel, and then compile it. Later, it became much more convenient after the PHP kernel integrated PHP-FPM. Just use the --enalbe-fpm
compilation parameter.
3. Some people say that after modifying the php.ini configuration file, there is no way to restart smoothly, so php-fpm was born
Yes, after modifying php.ini, the php-cgi process cannot be restarted smoothly. The processing mechanism of php-fpm is that new workers use new configurations, and existing workers can rest after finishing the work at hand. This mechanism is used to smooth the transition.
4. Others say that PHP-CGI is the FastCGI manager that comes with PHP. If so, why create php-fpm?
No. php-cgi is just a program that interprets PHP scripts.
Original text excerpted from: https://segmentfault.com/q/1010000000256516

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
