The realization of lamp base lamp (PHP-FPM)

Source: Internet
Author: User

Outline:

1. CGI and fastcgi comparative analysis

2. /span> with three hosts to fast-cgi way Span style= "font-size:20px;font-family: ' The song body '; background: #FFFF00;" > implement lamp and install wordpress

3. Detailed explanation of the output results

I. Comparative analysis of CGI and fastcgi

Lamp installation http and the PHP The combination can be divided into three kinds:

1.php as a http the module

2. combine in CGI mode (rarely used)

3. combine with fastcgi mode


About CGI

The CGI full name is the Universal Gateway Interface (Common Gateway Interface), which allows a client to request data from a Web browser to a program that executes on a Web server. CGI describes a standard for transferring data between the client and the program. One purpose of CGI is to be independent of any language, so CGI can be written in any language, as long as the language has standard input, output, and environment variables. such as PHP,PERL,TCL and so on.

How the CGI works
    1. After a client accesses a URL address, it submits the data through get/post/put, etc., and makes a request to the WEB server through the HTTP protocol.

    2. The server-side HTTP Daemon (daemon) initiates a child process. The information described in the HTTP request is then passed through the standard input stdin and environment variables to the CGI program specified by the URL in the subprocess, and the application is started for processing, and the results are returned to the HTTP Daemon child process via the standard output stdout.

    3. The HTTP Daemon child process is then returned to the client via the HTTP protocol.

This paragraph of the above understanding may be more abstract, the following we have a GET request as an example for detailed explanation.




650) this.width=650; "src=" Http://www.php-internals.com/images/book/chapt02/02-02-03-cgi.png "alt=" diagram 2.7 CGI operation schematic example "/>


, the process for this request is as follows:

    1. Client Access Http://127.0.0.1:9003/cgi-bin/user?id=1

    2. The daemon on the 127.0.0.1 that listens on port 9003 accepts the request

    3. By parsing the HTTP header information, it is known that it is a GET request and that the /cgi-bin/ file under the directory is requested user .

    4. The pass in the URI is stored in the id=1 QUERY_STRING environment variable.

    5. The Web Daemon Fork a child process and then executes the user program in the child process, obtained through the environment variable id .

    6. After execution, the result is returned to the child process through standard output.

    7. The child process returns the result to the client.



FastCGI Introduction

FastCGI is a protocol for communication between Web servers and handlers, an improvement to CGI, fastcgi like a resident (long-lived) CGI, which can be executed all the time and will not take a moment to fork a process to handle when the request arrives ( This is the most criticized Fork-and-execute model of CGI). Because he is just a communication protocol, it also supports distributed operations, so the FastCGI program can execute on hosts other than the Web server and can accept requests from other Web servers.

FastCGI is a language-independent, scalable, CGI open-architecture extension that keeps the CGI interpreter process in memory for high performance. CGI program loading is the main reason for the poor CGI performance, if the CGI program is kept in memory and accepted FastCGI process manager scheduling, it can provide good performance, scalability, Fail-over features and so on.

650) this.width=650; "src=" Http://www.php-internals.com/images/book/chapt02/02-02-03-fastcgi-demo.png "alt=" Figure 2.8 FastCGI Operating Principle Example "style=" Font-size:16px;font-family:sans-serif; "/>


Summarize:

    • Mode_php is a module of Apache that embeds the PHP interpreter in the Apache process.

    • CGI and fastcgi are respectively a protocol. Web Server implements the appropriate application for the CGI or FASTCGI Protocol ( hereinafter referred to as CGI or fastcgi), enabling the PHP interpreter to process PHP requests. They all exist in the form of independent processes.

    • Mode_php and FASTCGI can handle multiple requests in a single process, and CGI can handle only one request in a single process.



PHP-CGI is the implementation of a CGI protocol .

    • php-cgi is actually a PHP parser .

    • When in CGI mode, when Web Server receives a xx/index.php request, php-cgi,php-cgi will parse the php.ini file, initialize the environment, process it according to the request parameters, and return the processed results. (All in the CGI protocol specification)

    • PHP-CGI starts a process at each request, and then reads the php.ini to parse it, which can be imagined to be relatively inefficient.

    • PHP-CGI cannot achieve a smooth restart. After you modify the php.ini configuration, the PHP-CGI program that is started later is still not aware.



PHP-FPM , FastCGI Process Management, is an implementation of FastCGI protocol .

    • When the request arrives, PHP-FPM starts and reads the php.ini file to complete the initialization environment, then starts a master, and then starts multiple worker. When the request comes in, master passes to a worker and waits for the next request. PHP-FPM dynamically configures the number of worker.

    • A PHP-FPM process can process multiple requests and start multiple php-cgi programs. when worker Not enough, Master The configuration can be pre-started with several worker Yes, of course. worker too much, will also stop some, this improves the performance, also saves the resources

    • The PHP-FPM can achieve a balanced restart. After you modify php.ini, the new configuration is used when the new worker is enabled.


in the actual production because of the pressure, so the amp is generally separate server, or even a server group, and using a high-availability cluster to share the pressure of a single server, while avoiding single point of failure, so this time will use three host to simulate lamp work scenario.



1. Install and start the service:

Yum install-y php-fpm httpd php-mysql mariadb-server

Systemctl start httpd mariadb

Mysql_secure_installation

2. Configure the HTTPD virtual host and support FPM

<virtualhost *:80>

ServerName www.magedu.com

DocumentRoot "/vhosts/www"

<directory "/vhosts/www" >

Options None

AllowOverride None

Require all granted

</Directory>

Proxyrequests OFF

Proxypassmatch ^/(. *\.php) $ fcgi://127.0.0.1:9000/vhosts/www/$1

Proxypassmatch ^/(ping|status)/?$ fcgi://127.0.0.1:9000/$1

DirectoryIndex index.php

</VirtualHost>

[Email protected] ~]# MKDIR/VHOSTS/WWW-PV

[[email protected] www]# ls

index.php

[email protected] www]# cat index.php

<?php

Phpinfo ();

?>

3. Edit the FPM configuration file:

[Email protected] pma]# vim/etc/php-fpm.d/www.conf

[POOL_ID]

Listen = 127.0.0.1:9000

Listen.backlog =-1

Listen.allowed_clients = 127.0.0.1

user = Apache

Group = Apache

PM = dynamic

Define processor management mechanism: static, dynamic

Pm.max_children: The maximum number of child processes, the maximum capacity of the connection pool;

Pm.start_servers: The number of child processes started at service startup;

Pm.min_spare_servers

Pm.max_spare_servers

Rlimit_files = 1024

Rlimit_core = 0

Pm.status_path =/status

Ping.path =/ping

Ping.response = Pong

Php_value[session.save_path] =/var/lib/php/session

Where the session is stored

4. Restart the httpd and PHP-FPM services

5. Testing

A enter http://10.1.252.228/status?full and http://10.1.252.228/ping in the browser to see if it responds pong

Pool:www

Process Manager:dynamic

Start time:11/oct/2016:21:15:46 +0800

Start since:577

Accepted Conn:16

Listen queue:0

Max Listen queue:1

Listen Queue len:128

Idle Processes:4

Active Processes:3

Total Processes:7

Max Active Processes:3

Max Children reached:0

Slow requests:0

b Unzip Phpmysqladmin to/vhost/www to see if you can enter

#yum install-y php-mbstring, Php-mcrypt, Php-xcache

Note: Be sure to remember to create the/var/lib/php/session directory and give Apache user permissions


The realization of lamp base lamp (PHP-FPM)

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.