Php-CGI Configuration

Source: Internet
Author: User
Tags server memory

I recently investigated the PHP-CGI configuration and found that the online articles are really just for example. I don't know how to make the web server run according to their configuration method.

The Research on PHP-CGI mainly aims to understand the working process of CGI. Of course, the basic concept is still the same. Let's search for the article and read it.

Paste it for your convenience.

What is CGI?

CGI is a common gateway interface. It is a tool for the HTTP server to "talk" with programs on your or other machines, the program must run on the network server.

CGI can be written in any language as long as it has standard input, output, and environment variables. Such as PHP, Perl, and TCL.

 

  What is FastCGI?

FastCGI is like a long-live CGI. It can be executed all the time, it will not take time to fork every time (this is the most criticized fork-and-execute mode for CGI ). It also supports distributed operations, that is, FastCGI programs can run on hosts other than website servers and receive requests from other website servers.

FastCGI is a CGI open extension in a language-independent and scalable architecture. Its main behavior is to maintain the CGI interpreter process in the memory and thus achieve high performance. As we all know, repeated loading of the CGI interpreter is the main cause of low CGI performance. If the CGI interpreter is kept in the memory and is scheduled by the FastCGI Process Manager, it can provide good performance, scalability, fail-over features, and so on.

  FastCGI and CGI features

1. For example, CGI and FastCGI have language independence.

2. applications such as CGI and FastCGI in processes run independently of core web servers and provide a safer environment than APIs. (Apis links the application code with the core Web server, which means that the application in a wrong API may damage other applications or core servers; malicious API application code can even steal the keys of another application or core server .)

3. Currently, FastCGI technology supports the following languages: C/C ++, Java, Perl, TCL, Python, smalltalk, and Ruby. Related modules are also available on popular servers such as Apache, ISS, and Lighttpd.

4. For example, CGI and FastCGI do not depend on the internal architecture of any web server. Therefore, even if the server technology changes, FastCGI remains stable.

  FastCGI Working Principle
  
1. Load FastCGI Process Manager (iis isapi or Apache module) when the web server starts)
  
2. FastCGI Process Manager initializes itself, starts Multiple CGI interpreter processes (multiple PHP-cgi are visible), and waits for a connection from the 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 sub-process PHP-CGI.
  
4. After the FastCGI sub-process completes processing, the standard output and error messages are returned from the same connection to the web server. When the FastCGI sub-process closes the connection, the request processing is complete. The FastCGI sub-process then waits for and processes the next connection from the FastCGI Process Manager (running on the Web server. In CGI Mode, PHP-CGI exits here.
  
In the above cases, you can imagine how slow CGI is. For each Web request, PHP must re-Parse PHP. ini, re-load all extensions, and initialize all data structures. With FastCGI, all of these occur only once when the process starts. An additional benefit is that persistent database connection can work.

  FastCGI Deficiency

Because it is a multi-process, it consumes more server memory than CGI multithreading, The PHP-CGI interpreter consumes 7 to 25 MB memory per process, multiply this number by 50 or 100, which is a large amount of memory.
  
The nginx 0.8.46 + PhP 5.2.14 (FastCGI) server consumes 30 thousand MB of memory (15 MB * 10 = 150 MB) for the 10 nginx processes enabled with concurrent connections ), the 64 opened PHP-CGI processes consume 1280 MB of memory (20 mb * 64 = Mb). In addition, the system consumes less than 2 GB of memory. If the server memory is small, only 25 PHP-CGI processes can be started, so that the total memory consumed by PHP-CGI is 500 mb.

  What is PHP-CGI

PHP-CGI is the FastCGI manager that comes with PHP.

Start the PHP-CGI with the following command:

1
php-cgi -b 127.0.0.1:9000

  PHP-CGI Deficiency

1. After PHP-CGI changes the php. ini configuration, restart PHP-CGI to make the new PHP-ini take effect.

2. directly kill the PHP-CGI process, and PHP cannot run. (PHP-FPM and spawn-fcgi don't have this problem, and the daemon will smoothly generate new sub-processes .)

  What is PHP-FPM

PHP-FPM is a PHP FastCGI manager that is only used for PHP and can be downloaded at http://php-fpm.org/download.

PHP-FPM is actually a patch of PHP source code, designed to integrate FastCGI process management into the PHP package. You must patch it to your PHP source code before using it after compiling and installing PHP.

Now we can download the source code tree in the latest PHP 5.3.2 to directly integrate the branch of the PHP-FPM, it is said that the next version will be integrated into the main branch of PHP. Compared with spawn-fcgi, PHP-FPM has better control over CPU and memory, and the former is easy to crash and must be monitored with crontab, while PHP-FPM has no such troubles.

Php5.3.3 has already integrated PHP-FPM and is no longer a third-party package. PHP-FPM provides a better way of PHP process management, can effectively control the memory and process, can smoothly load PHP configuration, than spawn-fcgi has more, so PHP officially included. You can enable PHP-FPM with the-enable-FPM parameter in./configure.

Use PHP-FPM to control FastCGI processes of PHP-CGI

12345678
/Usr/local/PHP/sbin/PHP-FPM {START | stop | quit | restart | reload | logrotate} -- start FastCGI process of PHP -- stop force terminate FastCGI process of PHP -- quit smoothly terminates PHP FastCGI process -- restart restarts PHP FastCGI process -- reload re-smoothly loads PHP. INI -- logrotate re-enable log files

  What is spawn-fcgi?

Spawn-fcgi is a common FastCGI management server. It is part of Lighttpd. Many people use spawn-fcgi of Lighttpd to manage FastCGI mode, but it has many disadvantages. While the emergence of PHP-FPM has mitigated some problems, but the PHP-FPM has a disadvantage is to re-compile, which for some already running environment may have a small risk (refer ), you can use the PHP-FPM directly in PHP 5.3.3.

Spawn-fcgi has become a single project, which is more stable and convenient for the configuration of many web sites. Many sites have already used it with nginx to solve dynamic web pages.

The latest Lighttpd does not contain this one either (http://www.lighttpd.net/search? Q = spawn-fcgi), but it can be found in previous versions. Included in the lighttpd-1.4.15 version (http://www.lighttpd.net/download/lighttpd-1.4.15.tar.gz)

Currently, spawn-fcgi is composed

Note: The latest spawn-fcgi can be searched for "spawn-fcgi" on the lighttpd.net website to find its latest release address.

Next we can use spawn-fcgi to control the FastCGI process of PHP-CGI.

 
1
    /usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-CGI

The parameter description is as follows:

-F specifies the location of the execution program of the process that calls FastCGI, Which is set according to the PHP installed on the system
-A is bound to the ADDR address.
-P: bind to Port
-S: The Path to bind to the UNIX socket
-C indicates the number of FastCGI processes generated. The default value is 5 (for PHP only)
-P specifies the PID file path of the Generated Process
-U and-G FastCGI use the identity (-u user-G user group) to run. www-data can be used in Ubuntu, and other configuration based on the situation, such as nobody and Apache.

  Comparison between PHP-FPM and spawn-CGI

PHP-FPM is very convenient to use, configuration is in the PHP-FPM.ini file, and start, restart can be done from PHP/sbin/PHP-FPM. More convenient is to modify PHP. ini can be directly loaded using the PHP-FPM reload, without killing the process can complete the php. ini modification Load
The results show that using PHP-FPM can greatly improve PHP performance. PHP-FPM Control Process CPU recovery speed is slow, memory allocation is very even.

The CPU usage of the process controlled by spawn-fcgi decreases rapidly, while the memory allocation is uneven. Many processes do not seem to be assigned, but others occupy a high volume. This may be due to uneven process task allocation, which also leads to a decrease in the overall response speed. The rational allocation of PHP-FPM leads to the mention of the overall response and the average task.

  Comparison of PHP-FPM and spawn-fcgi Functions

Http://php-fpm.org/about/

Php-FPM and spawn-fcgi are all process managers that guard PHP-CGI.

In fact, PHP-CGI itself is the fast-CGI method, but the use of it is the following three methods, the advantages and disadvantages of the above post is very clear

The configuration process is as follows:

This article is good but uses Lighttpd for configuration. Let's take a look at the working process. The above three methods are used for Lighttpd configuration.

FastCGI. Server = (
". Php" =>
(
(
"Host" => "192.168.5.146 ",
"Port" = & gt; 9000,
"Check-local" => "Disable ",
"Fix-root-scriptname" => "enable ",
"Docroot" => "/home/web_root/www.funshion.com. 1.0.0 ",
),

),
)

First, start/usr/local/PHP-FPM/bin/PHP-CGI-B 192.168.5.146: 9000 &

If you use

/Usr/local/lighttpd1.4.22/bin/spawn-fcgi-A 192.168.5.146-P 9000-u Lighttpd-C 1-F/usr/local/PHP-FPM/bin/PHP-CGI

No problem.

Third

Php-FPM After configuring the php-fpm.confi File

Start with/etc/init. d/PHP-FPM start

The above three methods use netstat-anpt | grep 9000 to view the listening port

You can test the advantages and disadvantages of the three methods.

Some articles say that/usr/local/PHP-FPM/bin/PHP is used to start PHP-CGI, but some reports an error.

It can be determined by/usr/local/PHP-FPM/PHP-v.

If the actual PHP 5.2.11 (CGI-fcgi) (built: Jun 30 2011 20:38:37) is CGI-fcgi, there is no problem. Of course, if it is compiled, there will be a PHP-CGI executable file, direct to it

If you do not need to compile PHP to start CGI, you can search

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.