In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you what the operation mode of PHP, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
The PHP operation mode has 4 minutes:
1) cgi Universal Gateway Interface (Common Gateway Interface))
2) fast-cgi resident (long-live) CGI
3) cli Command Line run (Command Line Interface)
4) web module mode (module mode running by web servers such as apache)
1.CGI (Common Gateway Interface)
CGI is the Universal Gateway Interface (Common Gateway Interface), which is a program. Generally speaking, CGI is like a bridge that connects the web page with the execution program in the WEB server. It transmits the instructions received by HTML to the server execution program, and then returns the results of the server execution program to the HTML page. CGI has excellent cross-platform performance and can be implemented on almost any operating system. CGI is an older model that has been rarely used in recent years.
Every time there is a user request, a child process of cgi is created first, then the request is processed, and then the child process is finished. This is the fork-and-execute pattern. When the number of user requests is very large, it will occupy a large number of system resources such as memory, CPU time and so on, resulting in low performance. Therefore, the server in cgi mode will have as many CGI child processes as there are connection requests, and repeated loading of child processes is the main reason for the poor performance of cgi.
If you do not want to embed PHP into server-side software (such as Apache) as a module installation, you can choose to install it in CGI mode. Or use PHP in different CGI wrappers to create secure chroot and setuid environments for your code. So each client requests a php file, and the Web server calls php.exe (php.exe,linux is php in win) to interpret the file, and then returns the interpreted result to the client as a web page. This installation usually installs the executable file of PHP to the cgi-bin directory of the web server. The CERT recommendation CA-96.11 recommends that you do not put any interpreters in the cgi-bin directory.
The advantage of this approach is to separate the web server from the specific procedures, with a clear structure and strong controllability, while the disadvantage is that if the process fork of cgi becomes a great burden on the server in the case of high access requirements, imagine hundreds of concurrent requests leading to hundreds of processes in the server fork. This is why cgi has always had a reputation for low performance and high resource consumption.
CGI mode installation:
CGI is an older model that has been rarely used in recent years, so we just want to test it.
Installation of CGI mode needs to be commented out
The LoadModule php5_module modules/libphp5.so business. If you leave this line uncommented, you will go all the way to handler mode. That is, module mode.
Then add action to httpd.conf:
Action application/x-httpd-php / cgi-bin/
If you can't find php-cgi in the / cgi-bin/ directory. You can cp one from the bin of php.
Then restart apache, and then open the test page to find that Server API becomes: CGI/FastCGI. Indicates that you successfully switched to cgi mode.
Question:
1) if the cgi program cannot be executed in / usr/local/httpd/cgi-bin/ and encounters a 403or 500error
Open the apache error log with the following prompt: Permission denied: exec of
You can check the properties of the cgi program, as defined in the Linux contexts file, the httpd_sys_script_exec_t attribute must be in / usr/local/httpd/cgi-bin/. Check through ls-Z, if not, change it by the following command: chcon-t httpd_sys_script_exec_t / var/www/cgi-bin/*.cgi if it is the cgi in the virtual host, refer to question 2 to enable it to use normal functions, and then set the context of the cgi file to
Httpd_sys_script_exec_t is fine. Chcon-R-t httpd_sys_script_exec_t cgi-bin/
2) apache error message:. Malformed header from script. Bad header=
According to the prompt that there is a problem with header, what is the first sentence of the file output? it should be similar to the following
Content-type: text/plain; charset=iso-8859-1\ n\ n
Or Content-type:text/html\ n\ n
Note: after declaring Content-type, you need to output two blank lines.
3) apache error message: Exec format error
The script interpreter is set incorrectly. The first line of the script should be'#! In the form of 'interpreter path', enter 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, you do not need to enter the interpreter path, and the system will automatically find PHP.
2. Fastcgi mode
Fast-cgi is an upgraded version of cgi, and FastCGI is like a resident (long-live) CGI that can be executed all the time, as long as it is activated and does not take the time to fork every time (this is CGI's most criticized fork-and-execute mode).
How FastCGI works is:
(1) when Web Server starts, load the FastCGI process manager [PHP's FastCGI process manager is PHP-FPM (php-FastCGI Process Manager)] (IIS ISAPI or Apache Module)
(2) the FastCGI process manager initializes itself, starts multiple CGI interpreter processes (multiple php-cgi.exe are visible in the task manager) and waits for a connection from Web Server.
When the client request arrives at the Web Server, the FastCGI process manager selects and connects to a CGI interpreter. Web server sends CGI environment variables and standard input to the FastCGI child process php-cgi.
(4) the FastCGI child process returns the standard output and error information from the same connection to Web Server after processing. When the FastCGI child process closes the connection, the request is processed. The FastCGI child process then waits and processes 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. Each Web request PHP must reparse the php.ini, reload all dll extensions, and reinitialize all data structures. With FastCGI, all of this happens only once when the process starts. An added benefit is that persistent database connections (Persistent database connection) work.
Advantages of Fastcgi:
1) in terms of stability, fastcgi runs as an independent process pool to cgi. If a single process dies, the system can easily discard it, and then reassign a new process to run the logic.
2) from the security point of view, Fastcgi supports distributed computing. Fastcgi and the host server are completely independent, fastcgi down will not bring down server.
3) in terms of performance, fastcgi separates the dynamic logic processing from the server, and the heavy load of IO processing is left to the host server, so that the host server can be IO wholeheartedly. For an ordinary dynamic web page, the logic processing may be only a small part, a large number of pictures and other static.
Disadvantages of FastCGI: after talking about the advantages, let's also talk about the disadvantages. From my actual use, FastCGI mode is more suitable for servers in production environment. But it is not suitable for developing machines. Because when using Zend Studio to debug a program, because FastCGI thinks the PHP process timed out, it returns a 500error on the page. This is very annoying, so I switched back to ISAPI mode on the 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
/ usr/local/httpd/modules/ has one more file: mod_fcgid.so
2) recompile php
. / configure-prefix=/usr/local/php-enable-fastcgi-enable-force-cgi-redirect-disable-cli
Make
Make install
After compilation, php-cgi in the bin directory of PHP is the php interpreter of fastcgi mode.
After the installation is successful, execute
Php-v output
PHP 5.3.2 (cgi-fcgi).
Here is the output with cgi-fcgi
Note:
1. The compilation parameter cannot be added with-with-apxs=/usr/local/httpd/bin/apxs, otherwise the installed php execution file is in cli mode.
2 output PHP 5.3.2 (cli) if-disable-cli is not added at compile time
3) configure apache
You need to configure apache to run the php program in fastcgi mode
Vi httpd.conf
We use a virtual machine to implement:
The copy code is as follows:
# load fastcgi module
LoadModule fastcgi_module modules/mod_fastcgi.so
# / / execute fastcgi statically and start 10 processes
FastCgiServer / usr/local/php/bin/php-cgi-processes 10-idle-timeout 150-pass-header HTTP_AUTHORIZATION
#
DocumentRoot / usr/local/httpd/fcgi-bin
ServerName www.fastcgitest.com
ScriptAlias / fcgi-bin/ / usr/local/php/bin/ # defines directory mapping / fcgi-bin/ instead of / usr/local/php/bin/
Options + ExecCGI
Requests at the end of AddHandler fastcgi-script .php .fcgi # .php are processed with php-fastcgi
AddType application/x-httpd-php .php # adds MIME type
Action application/x-httpd-php / fcgi-bin/php-cgi # set the processor of php-fastcgi: / usr/local/php/bin/php-cgi
Options Indexes ExecCGI
Order allow,deny
Allow from all
Or
The copy code is as follows:
ScriptAlias / fcgi-bin/ "/ usr/local/php/bin" # define directory mapping FastCgiServer / usr/local/php/bin/php-cgi-processes 10 # configure fastcgi server,SetHandler fastcgi-scriptOptions FollowSymLinksOrder allow,denyAllow from allAddType application/x-httpd-php .php # add MIME type AddHandler php-fastcgi .php # .php end requests use php-fastcgi to process Action php-fastcgi / fcgi-bin/php-cgi # set php-fastcgi 's processor
4). Apache under restart, and check phpinfo. If the server information is:
Things like Apache/2.2.11 (Unix) mod_fastcgi/2.4.6 indicate that the installation is successful.
If there is a 403 error, check to see if / usr/local/httpd/fcgi-bin/ has sufficient permissions.
Or
The copy code is as follows:
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Change to:
The copy code is as follows:
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Just do it.
Ps-ef | grep php-cgi can see 10 fastcgi processes running.
3. CLI mode
Cli is the command line operation mode of php, and you often use it, but you may not notice it (for example, we often use "php-m" to find PHP under linux and those extensions installed are PHP command line operation mode; interested students can type php-h to study this operation mode in depth)
1. Let PHP run the specified file.
Php script.php
Php-f script.php
Both of the above methods (with or without the-f parameter) can run the script's script.php. You can choose any file to run, and the PHP scripts you specify don't have to have a .php extension; they can have any file name and extension.
two。 Run the PHP code directly on the command line.
Php-r "print_r (get_defined_constants ());"
When using this method, please pay attention to the substitution of shell variables and the use of quotation marks.
Note: please read the above example carefully, there are no start and end markers when running the code! With the-r parameter added, these markers are not needed, and they can cause syntax errors.
3. Provide the PHP code you need to run through standard input (stdin).
The above usage provides us with very powerful features that allow us to dynamically generate PHP code and run it from the command line, as shown in the following example:
$some_application | some_filter | php | sort-u > final_output.txt
4. Module mode
Module mode is integrated in the form of mod_php5 module, and the function of mod_php5 module is to receive PHP file requests from Apache, process these requests, and then return the processed results to Apache. If we configure the PHP module (mod_php5) in its configuration file before Apache starts, the PHP module starts this module when Apache starts to accept requests for PHP files by registering apache2's ap_hook_post_config hook.
In addition to this startup loading method, Apache's modules can be loaded dynamically at run time, which means that the server can be extended without recompiling the source code or even stopping the server at all. All we need to do is send a signal to the server HUP or AP_SIG_GRACEFUL to inform the server to reload the module. But before loading dynamically, we need to compile the module into a dynamic link library. The dynamic loading at this time is to load the dynamic link library. The processing of dynamic link libraries in Apache is done through the module mod_so, so the mod_so module can not be dynamically loaded, it can only be statically compiled into the core of Apache. This means that it was started with Apache.
How does Apache load the module? Let's take the mod_php5 module mentioned earlier as an example. First, we need to add a line to the configuration file httpd.conf of Apache:
This mode of operation is often used by us to use apache server in windows environment, while in DLL, PHP is started and run together with Web server. (it is an extension of apache based on CGI to speed up the running efficiency of PHP.)
The copy code is as follows:
LoadModule php5_module modules/mod_php5.so
Here we use the LoadModule command, whose first argument is the name of the module, which can be found in the source code of the module implementation. The second option is the path where the module is located. If you need to load the module while the server is running, you can send a signal HUP or AP_SIG_GRACEFUL to the server, and once the signal is received, Apache will reload the module without restarting the server.
5.php operating mode in Nginx (Nginx+ PHP-FPM)
There are two kinds of stack:ligthttpd+spawn-fcgi; that are now common to use FastCGI. The other is nginx+PHP-FPM (you can also use spawn-fcgi).
A, as mentioned above, both structures use FastCGI to support PHP, so HTTPServer is completely liberated for better response and concurrent processing. So both lighttpd and nginx have the reputation of small, but powerful and efficient.
B, the two can also tell the good from the bad. Since spawn-fcgi is a part of lighttpd, if you install lighttpd, you will generally use spawn-fcgi to support php. But at present, some users say that when ligttpd's spwan-fcgi is accessed with high concurrency, there will be a memory leak or even automatically restart fastcgi. That is, when the PHP script processor crashes, a white page may appear if the user accesses it (that is, the PHP cannot be parsed or an error occurs).
Another: first of all, nginx does not contain fastcgi (spawn-fcgi) like lighttpd itself, so it is completely lightweight and must rely on a third-party FastCGI processor to parse PHP, so it seems that nginx is very flexible. It can be connected to any third-party processor that provides parsing to achieve parsing of PHP (it is easy to set up in nginx.conf). Nginx can use spwan-fcgi (you need to install lighttpd together, but you need to avoid ports for nginx, and some earlier blog has tutorials on this installation), but because of the defects that users have gradually discovered in spawn-fcgi described above, you are gradually reducing the use of nginx+spawn-fcgi combinations.
C. Due to the defects of spawn-fcgi, there is a new third-party PHP FastCGI processor called PHP-FPM (specifically google), which is still being added to PHP core in the near future. Compared with spawn-fcgi, it has the following advantages:
Because it is developed as a patch patch for PHP, it needs to be compiled with the php source code when installed, that is to say, it is compiled into php core, so it is better in terms of performance.
At the same time, it is also better than spawn-fcgi in handling high concurrency, at least not automatically restarting the fastcgi processor. The specific algorithm and design can be understood by 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.
The above is all the contents of the article "what is the running mode of PHP"? thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.