Home >Backend Development >PHP Tutorial >In-depth understanding of PHP running mode_PHP tutorial
There are 4 operating modes for PHP:
1) cgi Common Gateway Interface)
2) fast-cgi resident (long- live) type CGI
3) cli command line operation (Command Line Interface)
4) web module mode (module mode run by web servers such as apache)
1 .CGI (Common Gateway Interface)
CGI is the Common Gateway Interface (Common Gateway Interface). It is a program. In layman's terms, CGI is like a bridge that connects the web page and the execution in the WEB server. The program is connected, it passes the instructions received by the HTML to the server's execution program, and then returns the results of the server's execution program to the HTML page. CGI is extremely cross-platform and can be implemented on almost any operating system. CGI is already an older model and has been rarely used in recent years.
Every time there is a user request, a cgi sub-process will be created first, then the request will be processed, and the sub-process will be terminated after processing. This is the fork-and-execute mode. When the number of user requests is very large, a large amount of system resources such as memory, CPU time, etc. will be occupied, resulting in low performance. Therefore, a server using CGI will have as many CGI sub-processes as there are connection requests. Repeated loading of sub-processes is the main reason for low CGI performance.
If you don’t want to embed PHP into server-side software (such as Apache) and install it as a module, you can choose to install it in CGI mode. Or use PHP with different CGI wrappers to create secure chroot and setuid environments for your code. In this way, each client requests a php file, and the web server calls php.exe (php.exe under win, php under Linux) to interpret the file, and then returns the result of the interpretation to the client in the form of a web page. This installation method usually installs the PHP executable file into the cgi-bin directory of the web server. CERT Recommendation CA-96.11 recommends not placing any interpreters in the cgi-bin directory.
The advantage of this method is that it separates the web server from specific program processing, has a clear structure and strong controllability. At the same time, the disadvantage is that if there is high access demand, the cgi process will fork. It becomes a huge burden on the server. Just imagine that hundreds of concurrent requests cause the server to fork hundreds of processes. This is why cgi has always been notorious for low performance and high resource consumption.
CGI mode installation:
CGI is an older mode and has rarely been used in recent years, so we are just for testing.
To install CGI mode, you need to comment out the line
LoadModule php5_module modules/libphp5.so. If you don't comment this line, it will go all the way to handler mode. That is the module mode.
Then add action in httpd.conf:
Action application/x-httpd-php /cgi-bin/
If you cannot find php-cgi in the /cgi-bin/ directory, you can download it from php yourself There is a cp in the bin.
Then restart apache, then open the test page and find that the Server API changes to: CGI/FastCGI. Description: Successfully switched to cgi mode.
Problems:
1) If the cgi program cannot be executed if it is placed in /usr/local/httpd/cgi-bin/ and encounters a 403 or 500 error
When opening the apache error log, the following prompt appears: Permission denied: exec of
You can check the attributes of the cgi program. According to the definition in the Linux contexts file, /usr/local/httpd/cgi-bin/ must be the httpd_sys_script_exec_t attribute. Check it with ls -Z. If not, change it with the following command: chcon -t httpd_sys_script_exec_t /var/www/cgi-bin/*.cgi If it is cgi in the virtual host, refer to question 2 to make it use normal functions. After that, set the context of the cgi file to
httpd_sys_script_exec_t through chcon. chcon -R -t httpd_sys_script_exec_t cgi-bin/
2) apache error message: .... malformed header from script. Bad header=
According to the prompt, there is a problem with the header. Check the first sentence of the file output. What it is, it should be similar to the following
Content-type: text/plain; charset=iso-8859-1nn
or Content-type: text/htmlnn
Note: After declaring Content-type, two outputs are required. A blank line.
3) Apache error message: Exec format error
Script interpreter setting error. The first line of the script should be in the form of '#!Interpreter Path', fill in the path of the script interpreter. If it is a PERL program, the common path is: #!/usr/bin/perl or #!/usr/local/bin /perl If it is a PHP program, there is no need to fill in the interpreter path, the system will automatically find PHP.
2. Fastcgi mode
fast-cgi is an upgraded version of cgi. FastCGI is like a long-live CGI that 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).
The working principle of FastCGI is:
(1). When the Web Server starts, the FastCGI process manager is loaded [PHP’s FastCGI process manager is PHP-FPM (php-FastCGI Process Manager)] (IIS ISAPI or Apache Module);
(2), FastCGI process manager initializes itself, starts multiple CGI interpreter processes (multiple php-cgi.exe visible in the task manager) and waits for the connection from the Web Server.
(3). When the client request reaches the Web Server, the FastCGI process manager selects and connects to a CGI interpreter. The Web server sends CGI environment variables and standard input to the FastCGI subprocess php-cgi.
(4). After the FastCGI sub-process completes processing, it returns standard output and error information to the Web Server from the same connection. When the FastCGI child process closes the connection, the request is processed. The FastCGI child process then waits for and handles the next connection from the FastCGI process manager (running in WebServer). In normal CGI mode, php-cgi.exe exits here.
In CGI mode, you can imagine how slow CGI usually is. Every web request to PHP must re-parse php.ini, reload all dll extensions and re-initialize all data structures. With FastCGI, all of this happens only once, when the process starts. An added bonus is that persistent database connections work.
Advantages of Fastcgi:
1) From a stability point of view, fastcgi runs cgi in an independent process pool. If a single process dies, the system will be very unstable. Easily discard it and then reassign a new process to run the logic.
2) From a security point of view, Fastcgi supports distributed computing. Fastcgi is completely independent from the host server. No matter how fastcgi is down, it will not bring down the server.
3) From a performance point of view, fastcgi separates the processing of dynamic logic from the server. The heavy-load IO processing is still left to the host server, so that the host server can focus on IO. For an ordinary dynamic web page, There may only be a small part of the logical processing, and a large number of static pictures etc.
FastCGI Disadvantages: After talking about the advantages, let’s talk about the disadvantages. From my actual use, FastCGI mode is more suitable for servers in production environments. But it is not suitable for development machines. Because when using Zend Studio to debug the program, FastCGI will think that the PHP process has timed out and return a 500 error on the page. This was so annoying that I switched back to ISAPI mode on my development machine.
Install fastcgi mode:
The path to install apache is /usr/local/httpd/
The path to install php is /usr/local/php/
1) Install mod_fastcgi
wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz
tar zxvf mod_fastcgi-2.4.6.tar.gz
cd mod_fastcgi-2.4.6
cp Makefile.AP2 Makefile
vi Makefile, edit top_dir = /usr/local/httpd
make
make install
After installation,
There is one more file in /usr/local/httpd/modules/: mod_fcgid.so
2) Recompile php
./configure --prefix=/usr/local/php --enable -fastcgi --enable-force-cgi-redirect --disable-cli
make
make install
After compiling in this way, php-cgi in the PHP bin directory will be the fastcgi mode php interpreter
After successful installation, execute
php -v to output
PHP 5.3.2 (cgi-fcgi).
The output here includes cgi-fcgi
Note:
1. The compilation parameters cannot be added –with-apxs=/usr/local/httpd/bin/apxs, otherwise the installed php execution file is in cli mode
2 If you do not add - -disable-cli outputs PHP 5.3.2 (cli)
3) Configure apache
You need to configure apache to run the php program in fastcgi mode
vi httpd.conf
We use a virtual machine Implemented by: