Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the operation modes of php in various web servers?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces php in a variety of web server operation mode which has a certain reference value, interested friends can refer to, I hope you read this article after a lot of gains, the following let Xiaobian with you to understand.

PHP runs in Apache.

php works in Apache in three ways: CGI mode, FastCGI mode, Apache module DLL

Compare them separately as follows:

1. CGI mode versus module mode:

php in apache the difference between the two ways of working (CGI mode, Apache module DLL)

Installation of these two modes of operation:

PHP 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 module approach in Apache 2.0

#Use these two lines for PHP 4:

LoadModule php4_module "c:/php/php4apache2.dll"

#Don't forget to copy php4apache2.dll from the sapi directory!

AddType application/x-httpd-php .php

#Use these two lines for PHP 5:

LoadModule php5_module "c:/php/php5apache2.dll"

AddType application/x-httpd-php .php

#Configure php.ini path

PHPIniDir "C:/php"

The difference between these two ways of working:

In CGI mode, if the client requests a php file, the Web server calls php.exe to interpret the file and returns the interpreted result to the client as a Web page.

In modularity (DLL), PHP is started and runs with the Web server.

So in some ways PHP4 installed as an apache module has better security and better execution efficiency and speed than CGI mode.

2. FastCGI operating mode analysis:

FastCGI works by:

(1)FastCGI Process Manager loaded when Web Server starts [PHP 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 Task Manager) and waits for connections from Web Server.

(3)When a client request arrives at 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.exe.

(4)The FastCGI subprocess completes processing and returns standard output and error messages from the same connection to the Web Server. When the FastCGI subprocess closes the connection, the request is completed. The FastCGI child process then waits for and processes the next connection from the FastCGI process manager (running in Web Server). In normal CGI mode, php-cgi.exe exits here.

In the above scenario, you can imagine how slow CGI is usually. PHP must parse php.ini, reload all dll extensions, and reinitialize all data structures for each Web request. With FastCGI, all of this happens only once at process startup. An added benefit is that persistent database connections work.

3. Why use FastCGI instead of the multithreaded CGI interpreter?

This may be due to various considerations, such as:

(1)You can't use multithreaded CGI interpreters stably on Windows anyway, either IIS ISAPI mode or APACHE Module mode, they always crash after a while. Is that weird? However, such a situation did exist!

Of course, there are many times when you can use multithreaded CGI interpreters stably, but you may find that web pages sometimes have errors, and you can't find the reason anyway, and the probability of such errors will be greatly reduced when you switch to FastCGI mode. I don't know why, but I think the CGI interpreter for independent address space is probably a little more stable than the shared-address-space version after all.

(2)Performance! Performance? Could FastCGI be faster than the multithreaded CGI interpreter? But sometimes it does, and only by testing your site can you make the final decision. The reason, I think it's hard to say, but there is information that in the era of Zend WinEnabler, Zend originally recommended using FastCGI instead of IIS ISAPI or Apache Module on Windows platform, but now Zend no longer makes this product.

4. Advantages of FastCGI mode running PHP:

Running PHP in FastCGI mode has several major benefits. The first is that PHP errors don't crash Apache, just PHP's own processes crash (but FastCGI immediately restarts a new PHP process to replace the crashed process). Second, FastCGI mode runs PHP better than ISAPI mode (I tested it with Apache Bench, but forgot to save the results, you can test it yourself if you are interested).

Finally, it is possible to run PHP 5 and PHP4 simultaneously. Referring to the configuration file below, two virtual hosts were set up, one using PHP 5 and the other using PHP4.

The copy code is as follows:

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, visit http://localhost/to use PHP 5, and visit http://localhost:8080/to use PHP4. So as long as the configuration is reasonable, you can let different virtual hosts use different versions of PHP.

Some disadvantages of FastCGI mode:

After talking about the advantages, let's talk about the disadvantages. From my actual usage, FastCGI mode is more suitable for production servers. But it is not suitable for development machines. Because when using the Zend Studio debugger, FastCGI will think PHP process timed out and return a 500 error on the page. This was annoying, so I switched back to ISAPI mode on the development machine anyway.

Finally, there is potential security in FastCGI mode in Windows.

Second, php in nginx running mode (nginx+PHP-FPM ) is currently the ideal choice

There are two common stacks that use FastCGI: ligthttpd+spawn-fcgi; the other is nginx+PHP-FPM(also spawn-fcgi).

(1)As mentioned above, both architectures use FastCGI support for PHP, so HTTP Server is completely liberated and can be better responsive and concurrent. Therefore, lighttpd and nginx have the reputation of small, but powerful and efficient.

(2)The two can also be divided into a good and bad, spawn-fcgi because it is part of lighttpd, so the installation of lighttpd will generally use spawn-fcgi for php support, but there are currently users who say that ligttpd spwan-fcgi in high concurrent access, there will be the memory leak mentioned above and even automatically restart fastcgi. That is: PHP script processor down, this time if the user visits, it may appear white pages (that is, PHP can not be parsed or errors).

Another: First of all, nginx is not like lighttpd itself with fastcgi(spawn-fcgi), so it is completely lightweight, you must use a third-party FastCGI processor to parse PHP, so in fact it seems that nginx is very flexible, it can be connected to any third-party parsing processor to achieve parsing of PHP (in nginx.conf is easy to set).

Nginx can use spwan-fcgi(lighttpd needs to be installed along with it, but you need to avoid ports for nginx, some older blogs have tutorials for installing this), but since spawn-fcgi has the flaws mentioned above, users are gradually finding that it is now using nginx+spawn-fcgi less often.

c. Due to the flaw in spawn-fcgi, there is now a new third-party (currently, and reportedly working on adding it to PHP core in the near future) FastCGI processor for PHP called PHP-FPM(google). It has the following advantages over spawn-fcgi:

Since it is developed as a PHP patch, it needs to be compiled with php source code when installed, that is, compiled into php core, so it should be excellent in performance;

It is also better than spawn-fcgi at handling high concurrency, at least without automatically restarting fastcgi processors. The specific algorithms and designs used can be found on Google.

Therefore, as mentioned above, due to the lightweight and flexibility of nginx, the current performance is superior, and more and more people are gradually using this combination: nginx+PHP/PHP-FPM

IIS+ ISAPI mode This mode is suitable for development environment, less used in production environment.

Thank you for reading this article carefully. I hope that Xiaobian will share the article "What are the operating modes of php in various web servers?" This article is helpful to everyone. At the same time, I hope that everyone will support you a lot. Pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report