PHP running mode rollup _php tips

Source: Internet
Author: User
Tags apache error log data structures fpm install php http request php file php script php source code

PHP running mode has 4 minutes:

1 CGI Universal Gateway Interface (Common Gateway Interface))
2 fast-cgi resident (long-live) type of CGI
3 CLI command-line run (command line Interface)
4 Web Module mode (module mode for Web server running Apache, etc.)
5) ISAPI (Internet Server application program Interface)

Note: After PHP5.3, PHP no longer has ISAPI mode, after installation there is no php5isapi.dll this file. To use a high version of PHP on IIS6, you must install the fastcgi extension and then make IIS6 support fastcgi.

1. CGI (Common Gateway Interface)

CGI is the Universal Gateway Interface (Common Gateway Interface), it is a program, popular speaking CGI is like a bridge, the Web page and the Web server in the implementation of the program to connect the HTML received instructions to the server to execute the program, Return 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 already a relatively old model, and it's been used very little for years.

Each user request will first create a CGI subprocess, then process the request, end the subprocess after processing, which is the Fork-and-execute mode. When the user requests a very long time, will be a large number of crowding out the system's resources such as memory, CPU times, resulting in low performance. So the CGI server has how many connection requests there will be the number of CGI child processes, the child process repeatedly loading is the main reason for the low performance of CGI.

If you do not want to embed PHP in server-side software (such as Apache) as a modular installation, you can choose to install it in CGI mode. Or use PHP in a different CGI wrapper to create a secure chroot and SETUID environment for your code. So each client requests a PHP file, the Web server calls Php.exe (Win is Php.exe,linux is PHP) to explain the file, and then the interpretation of the results as a Web page back to the client. This installation usually installs the PHP executable file to the Cgi-bin directory of the Web server. CERT proposal CA-96.11 recommend that you do not place any interpreter in the Cgi-bin directory.

The advantage of this approach is that the Web server and specific program processing independent, clear structure, strong controllability, and the disadvantage is that if the high access requirements, the CGI process fork will become a large server burden, want to Just like hundreds of concurrent requests that cause the server to fork out hundreds of processes is clear. That's why CGI has been saddled with the stigma of low performance and high resource consumption.

CGI mode installation:

CGI is already the older model, which is rarely used in years, so we just want to test.

     Install CGI mode needs to be commented out
     LoadModule php5_module modules/libphp5.so this line. If you do not annotate this line go straight to handler mode. Which is the module mode.
     then add action in httpd.conf:
     Action application/x-httpd-php/ cgi-bin/
     If php-cgi is not found in the/cgi-bin/directory. can be the CP one from PHP bin.
     then restart Apache, and then open the test page to discover that the server API becomes: cgi/fastcgi. Description successfully switched to CGI mode.
          Issues:

1 if the CGI program is not executed in the/usr/local/httpd/cgi-bin/, 403 or 500 errors are encountered
The Apache error log opens with the following tips: Permission denied:exec of
You can check the properties of the CGI program, as defined in the Linux contexts file,/usr/local/httpd/cgi-bin/must be the httpd_sys_script_exec_t attribute. Through Ls-z view, if not, change the following command: Chcon-t httpd_sys_script_exec_t/var/www/cgi-bin/*.cgi If it is a CGI in a virtual host, refer to question 2 to enable normal functionality to be used And then set the CGI file context by Chcon
Httpd_sys_script_exec_t can be. Chcon-r-T httpd_sys_script_exec_t cgi-bin/

2 Apache Error Tip: ... malformed header from script. Bad header=
Depending on the hint that there is a header problem, look at the file output the first sentence is what, should be similar to the following
Content-type:text/plain; charset=iso-8859-1\n\n
or content-type:text/html\n\n.
Note: Two blank lines are exported after declaring a good content-type.

3 Apache Error TIP: Exec format error
The script interpreter set an error. The first line of the script should fill in the path of the script interpreter in the form of ' #! interpreter ', and 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 fill in the interpreter Path, the system will automatically find PHP.

2. FastCGI mode

FAST-CGI is an upgraded version of CGI, FastCGI like a resident (long-live) CGI, which can be executed all the time, as long as it is activated, it does not have to be spent every single day to fork (this is CGI's most criticized Fork-and-execute model )。

The working principle of fastcgi is:

(1), when the Web server starts loading FastCGI process Manager "PHP FastCGI process Manager is PHP-FPM (php-fastcgi process Manager)" (IIS ISAPI or Apache Module);

(2), fastcgi the process manager itself, initiates multiple CGI interpreter processes (visible in Task Manager Php-cgi.exe) and waits for a connection from the Web server.

(3) When the client requests to reach 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) The FASTCGI process completes processing and returns standard output and error information from the same connection to the Web Server. When the fastcgi child process closes the connection, the request is processed. The fastcgi process then waits and processes the next connection from the FASTCGI process Manager (running in webserver). In the normal CGI mode, Php-cgi.exe quits here.

In CGI mode, you can imagine how slow CGI is usually. Each Web request PHP must reparse php.ini, reload all DLL extensions, and reinitialize all data structures. With fastcgi, all of these occur only once when the process is started. An additional benefit is that persistent database connections (persistent DB connection) can work.

Advantages of fastcgi:

1 from the perspective of stability, fastcgi is run with a separate process pool to CGI, a single process dies, the system can easily discard, and then redistribute new processes to run the logic.
2 from the security perspective, FASTCGI supports distributed computing. FastCGI and the host server are completely independent, and fastcgi how down does not bring the server down.
3 from the performance point of view, fastcgi the processing of dynamic logic from the server, the large load of IO processing or left to host server, so that the host server can be dedicated IO, for a normal Dynamic Web page, logic processing may be only a small part of A large number of pictures and other static

FastCGI Disadvantages:

Said the good, also said the shortcoming. From my actual use, the fastcgi mode is more suitable for the server in the production environment. But for the development of the machine is not very appropriate. Because when the Zend Studio debugger is used, FASTCGI returns a 500 error on the page because it thinks the PHP process timed out. This is very annoying, so I switched back to ISAPI mode on the development machine.

Install fastcgi mode:
Install Apache path is/usr/local/httpd/
Install PHP path is/usr/local/php/

1) Installation 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
Insta

When the installation is complete,

/usr/local/httpd/modules/One more file:
mod_fcgid.so

2) Recompile PHP

./configure–prefix=/usr/local/php–enable-fastcgi–enable-force-cgi-redirect–disable-cli
Make 
Install

After compiling this, php-cgi in the PHP bin directory is the PHP interpreter for fastcgi mode.
After the installation is successful, execute

Php-v output
PHP 5.3.2 (cgi-fcgi).

Here's the output with cgi-fcgi.

Attention:

1. The compiler parameter cannot add –with-apxs=/usr/local/httpd/bin/apxs otherwise installs the PHP execution file is the CLI mode
2 if compile without –disable-cli, output PHP 5.3.2 (CLI)
3) Configure Apache

Need to configure Apache to run PHP programs in fastcgi mode
vi httpd.conf

We use virtual machines to achieve:

 

4). Restart Apache, view phpinfoIf the server information is:
apache/2.2.11 (Unix) mod_fastcgi/2.4.6 or something like that means the installation was successful.
If a 403 error occurs, check to see if the/usr/local/httpd/fcgi-bin/has sufficient permissions.
Or

<directory/>
  Options followsymlinks
  allowoverride None order
  deny,allow
  deny
from all </Directory>

It's OK.
Ps-ef|grep php-cgi can see 10 fastcgi processes running.

3. CLI mode

The CLI is PHP's command-line run mode, which is often used, but may not be noticed (for example: we often use "php-m" in Linux to find PHP installed those extensions is the PHP command line operation mode; interested students can input php-h to delve into the operating mode)

1. Let PHP run the specified file.

PHP script.php
php-f script.php


Both of these methods (using or not using the-f parameter) can run the script's script.php. You can select any file to run, and the PHP script you specify does not have to be an extension of. php, and they can have arbitrary file names and extensions.

2. Run the PHP code directly at 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 quotes.
Note: Please read these examples carefully and there are no start and end tags when you run the code! When the-r parameter is added, these markers are unwanted, plus they can cause syntax errors.

3. Provide PHP code that needs to be run through standard input (stdin).

The above usage provides us with a very powerful feature that allows us to dynamically generate PHP code and run the code 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, at this time the function of the MOD_PHP5 module is to receive the PHP file request from Apache, and handle these requests, then return the processed result to Apache. If we configure the PHP module (MOD_PHP5) in the configuration file before the Apache starts, the PHP module will be hooked up to the ap_hook_post_config of the registered Apache2, starting this module to accept the request of PHP file when the Apache starts.

In addition to this startup loading mode, Apache modules can be dynamically loaded at runtime, which means that the server can extend functionality without having to recompile the source code, or even stop the server at all. All we need to do is send a signal to the server Hup or ap_sig_graceful notify the server to reload the module. But before dynamic loading, we need to compile the module into a dynamic link library. The dynamic load at this point is loading the dynamic link library. The processing of the dynamic link library in Apache is done through the module Mod_so, so the Mod_so module cannot be loaded dynamically, it can only be statically compiled into the core of Apache. This means that it's started with Apache.

How does Apache load a module? Let's take the MOD_PHP5 module mentioned above as an example. First we need to add a line to the Apache configuration file httpd.conf:

This is the mode that we used to use the Apache server in a Windows environment, and in a modular (DLL), PHP is started and run with the Web server. (an extension of Apache based on CGI to speed up PHP's efficiency)

LoadModule php5_module modules/mod_php5.so 

Here we use the LoadModule command, the first parameter of the command is the name of the module, the name can be found in the module implementation of the source code. The second option is the path where the module is. If you need to load a module while the server is running, you can send a signal hup or ap_sig_graceful to the server, and once that signal is received, Apache reloads the module without restarting the server.

5 ISAPI Mode

The ISAPI (Internet Server application program Interface) is a set of Internet service-oriented API interfaces provided by Microsoft, an ISAPI DLL that can be in memory long after being activated by a user request, Waiting for another request from the user, you can also set up multiple user request processing functions in one DLL, in addition, the ISAPI DLL application and the WWW server are in the same process and are significantly more efficient than CGI. (due to Microsoft's exclusivity, can only run in the Windows environment)

PHP as the Apache module, the Apache server after the system is started, the advance generation of multiple copies of the process reside in memory, once the request appears, immediately use these free subprocess processing, so there is no delay in the generation of child processes. These server replicas do not exit immediately after processing an HTTP request, but stay on the computer for the next request. Faster response to client browser requests and higher performance.

6. PHP operating mode in Nginx (nginx+ php-fpm)

There are two kinds of stack:ligthttpd+spawn-fcgi commonly used in the fastcgi way, and the other is NGINX+PHP-FPM (also can use spawn-fcgi).

A, as mentioned above, both structures are supported by fastcgi for PHP, so the httpserver is completely liberated to better respond and concurrent processing. So lighttpd and Nginx all have small, but powerful and efficient reputation.

B, the two can also be divided into a good or bad, spawn-fcgi because it is part of the LIGHTTPD, so installed lighttpd will generally use spawn-fcgi to support PHP, But at present, some users say that ligttpd spwan-fcgi in high concurrent access, will appear above the memory leak or even automatically restart fastcgi. That is, the PHP script processor is a machine, this time if the user access, may appear white page (that is, PHP can not be resolved or error).
Another: First nginx is not like the lighttpd itself contains fastcgi (spawn-fcgi), so it is completely lightweight, must rely on Third-party fastcgi processor to be able to parse the PHP, so in fact, it seems that nginx is very flexible, It can be connected to any Third-party provider with a resolver to enable parsing of PHP (easily set in nginx.conf). Nginx can use spwan-fcgi (you need to install lighttpd together, but you need to avoid ports for Nginx, some older blogs have tutorials on this), but because spawn-fcgi has the bugs that the user described above is gradually discovered, Now slowly reduce the use of nginx+spawn-fcgi combination.

C, due to spawn-fcgi defects, there is now a new third party (currently, heard that is trying to join the PHP core in the near future) of the PHP fastcgi processor, called PHP-FPM (specific Google). Compared with spawn-fcgi, it has the following advantages:
Because it is used as a PHP patch patch to develop, the installation of the need to compile with the PHP source code, that is, compiled into the core of PHP, so in the performance of some good;
It also outperforms spawn-fcgi in handling high concurrency, at least not automatically restarting FASTCGI processors. The specific algorithm and design can be understood by Google.
Therefore, as mentioned above because of the lightweight and flexibility of nginx, so the current performance is superior, more and more people gradually use this combination: NGINX+PHP/PHP-FPM

7. Summary

Currently in

Httpserver this piece basically can see three kinds of stack more popular:

(1) APACHE+MOD_PHP5
(2) lighttp+spawn-fcgi
(3) NGINX+PHP-FPM

After the three, the performance may be slightly superior, but Apache because of the rich modules and functions, is still the eldest. Some people test nginx+php-fpm in high concurrency conditions may reach apache+mod_php5 5~10 times, now nginx+php-fpm use more and more.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.