Detailed explanation of php based running mode on various web servers

  • 2020-06-03 06:03:31
  • OfStack

1. php runs mode in apache

In apache, php has three working modes: CGI mode, FastCGI mode and Apache module DLL
The following comparisons are made:
1. Comparison between CGI mode and module mode:
The Difference between php and apache (CGI mode, Apache module DLL)
Installation of these two ways of working:
CGI in Apache 2.0
ScriptAlias /php/ "c:/php/"
AddType application/x-httpd-php .php
# use this line for PHP 4
Action application/x-httpd-php "/php/php.exe"
# use this line for PHP 5
Action application/x-httpd-php "/php/php-cgi.exe"
PHP in Apache 2.0 modular approach
# Use these two lines for PHP 4:
LoadModule php4_module "c:/php/php4apache2.dll"
Don't forget to copy php4ES46en2.dll from sapi directory!
AddType application/x-httpd-php .php
# for PHP 5 use these two lines:
LoadModule php5_module "c:/php/php5apache2.dll"
AddType application/x-httpd-php .php
# Configure the path to php.ini
PHPIniDir "C:/php"
The difference between these two ways of working:
In CGI mode, if the client requests an php file, the Web server calls php.exe to interpret the file, and then returns the interpreted result to the client as a web page.
In modularity (DLL), PHP is up and running with Web server 1.
So in a way, PHP4 installed as an apache module has better security and better execution efficiency and speed than CGI mode.
2. Analysis of FastCGI operation Mode:
The working principle of FastCGI is:
(1) Load the FastCGI process manager on startup [the FastCGI process manager of PHP is ES86en-ES87en (ES88en-ES90en Manager)] (IIS ISAPI or Apache Module);
(2) The FastCGI process manager initializes itself, starts multiple CGI interpreter processes (multiple ES99en-ES100en.exe are visible in the task manager) and waits for a connection from Web Server.
(3) When the client request arrives at Web Server, the FastCGI process manager selects and connects to an CGI interpreter. Web server sends the CGI environment variable and standard input to the FastCGI child process php-ES114en.exe.
(4) FastCGI sub-process will return the standard output and error information from the connection with 1 to Web after completion of processing. When the FastCGI child closes the connection, the request is processed. The FastCGI child then waits and processes the next connection from the FastCGI process manager running in WebServer. In the normal CGI mode, ES125en-ES126en.ES127en exits here.
In this case, you can imagine how slow CGI is usually. Each Web request to PHP must reparse ES132en.ini, reload all dll extensions and reinitialize all data structures. With FastCGI, all of this happens only once at process startup. As an added bonus, the persistent database connection (Persistent database connection) works.

3. Why use FastCGI instead of the multithreaded CGI interpreter?
This may be due to a number of considerations, such as:
(1) You cannot use the multithreaded CGI interpreter on the windows platform in any stable way, whether it is IIS ISAPI or APACHE Module, they always run for a period of time and then crash. Strange? But it does happen!
Of course, there are many times when you can reliably use the multithreaded CGI interpreter. However, you may find that your web pages sometimes make errors that you can't figure out in any way. I'm not sure why, but I think the CGI interpreter for a separate address space is probably a little bit more stable than a Shared address space after all.
(2) Performance! Performance? Is FastCGI faster than the multithreaded CGI interpreter? But sometimes it does, and you can't tell until you test your website 1. The reason, I think it's hard to tell, but some sources say that in the era of Zend WinEnabler, Zend was also recommended to use FastCGI instead of IIS ISAPI or Apache Module, but Zend is no longer making this product.

4. Advantages of FastCGI mode:
Running PHP in FastCGI mode has several major benefits. The first is that PHP fails without crashing Apache, only PHP's own process fails (but FastCGI immediately restarts a new PHP process to replace the failed process). Second, FastCGI mode runs PHP mode with better performance than ISAPI mode (I used ApacheBench to test originally, but forgot to save the results, you can test by yourself if you are interested).
Finally, you can run PHP5 and PHP4 simultaneously. Refer to the following configuration file to set up two virtual hosts, one using PHP5 and the other using PHP4.

LoadModule fastcgi_module modules/mod_fastcgi-2.4.2-AP13.dll
ScriptAlias /fcgi-php5/ "d:/usr/local/php-5.0.4/"
FastCgiServer "d:/usr/local/php-5.0.4/php-cgi.exe" -processes 3
ScriptAlias /fcgi-php4/ "d:/usr/local/php-4.3.11/"
FastCgiServer "d:/usr/local/php-4.3.11/php.exe"
Listen 80
NameVirtualHost *:80
DocumentRoot d:/www
Options Indexes FollowSymlinks MultiViews
ServerName php5.localhost
AddType application/x-httpd-fastphp5 .php
Action application/x-httpd-fastphp5 "/fcgi-php5/php-cgi.exe"

IndexOptions FancyIndexing FoldersFirst
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all

Listen 8080
NameVirtualHost *:8080

DocumentRoot d:/www
Options Indexes FollowSymlinks MultiViews
ServerName php4.localhost
AddType application/x-httpd-fastphp4 .php
Action application/x-httpd-fastphp4 "/fcgi-php4/php.exe"

Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all

Using the above configuration, access http://localhost/ will use PHP5 and access http://localhost:8080/ will use PHP4. So as long as the proper configuration, you can let different virtual hosts use different versions of PHP.
1 Disadvantages of the FastCGI model:
Having said the advantages, also said the disadvantages. In my actual use, FastCGI mode is more suitable for production servers. But for the development of the machine is not suitable. When debugging with Zend Studio, 500 errors are returned on the page because FastCGI thinks the PHP process has timed out. This point was very annoying, so I switched back to ISAPI on the development machine.
Finally, there is potential security in Windows as FastCGI

php running mode in nginx (nginx+ ES216en-ES217en) is currently the ideal choice

There are two common ways to use FastCGI: ligthttpd+ ES223en-ES224en; The other is nginx+ ES226en-ES227en (or ES228en-ES229en).
(1) As mentioned above, both structures adopt FastCGI to support PHP, so HTTPServer is completely freed up for better response and concurrent processing. Thus lighttpd and nginx are known as small, but powerful and efficient.

spawn-fcgi is part 1 of lighttpd, so when lighttpd1 is installed, spawn-ES247en will support php. However, some users say that spwan-ES251en of ligttpd will leak the memory mentioned above or even automatically restart fastcgi when high concurrent access occurs. The PHP script processor is down, at which point a white page may appear if the user accesses it (that is, PHP cannot be parsed or fails).

Another one: first nginx unlike lighttpd itself contains brought fastcgi (spawn - fcgi), so it is lightweight, must with the help of a third party FastCGI processor to PHP parsing, so actually so nginx is very flexible, it can provide resolution and any third party processor implementation connected to achieve on the interpretation of PHP (in nginx. conf is very easy to set up).

nginx can use spwan-fcgi(you need 1 to install lighttpd, but you need to avoid ports for nginx, 1 some earlier blog have tutorials for this installation), but spawn-ES277en has gradually reduced the use of nginx+ ES279en-ES280en combination as described above.

c. Due to the defects of spawn-ES285en, the FastCGI processor of PHP, called PHP-ES291en, has now appeared in a new third party (currently, I heard that it is working on adding PHP core in the near future). Compared with spawn-ES294en, it has the following advantages:

As it is as PHP patch patch to develop, installation and php source code 1 from the compilation, that is, compiled to php core, so in terms of performance is better 1;
It also handles high concurrency better than es303EN-ES304en, at least without automatically restarting the fastcgi processor. The specific algorithm and design can be understood by google.

As a result, more and more people are using this combination: nginx+PHP/ ES312en-ES313en due to its lightweight and flexible performance
IIS+ ISAPI Mode This mode is suitable for the development environment and is rarely used in the production environment.

4. To summarize
At present, there are basically three kinds of stack popular in HTTPServer:
(1) Apache + mod_php5
(2) lighttp + spawn - fcgi
(3) nginx + PHP - FPM
The latter two may be slightly better, but Apache is still the number one for now due to its rich modules and features. It has been tested that nginx+ ES337en-ES338en may be 5~10 times higher than Apache+mod_php5 at high concurrency, and now more and more people are using nginx+ ES343en-ES344en.

Related articles: