PHP to connect nginx server and parse Nginx log method _php Tips

Source: Internet
Author: User
Tags fpm php source code wrapper nginx server

PHP and Nginx Integration

PHP-FPM is also a third-party fastcgi process manager, which is developed as a patch in PHP, also need to compile with the PHP source code when installing, that is to say, PHP-FPM is compiled into the PHP kernel, so it is better to handle the performance At the same time it is much better at handling high concurrency than the spawn-fcgi engine, so it is recommended that nginx+php/php-fpm this combination to parse PHP.
The main advantage of FastCGI is the separation of the dynamic language and HTTP server, so nginx and PHP/PHP-FPM are often deployed on different servers to share the pressure of the front-end Nginx server, so that the nginx single-minded processing of static requests and forwarding dynamic requests, while PHP /PHP-FPM Server dedicated parse PHP dynamic request

The

#fastcgi
fastcgi is a scalable, high-speed interface for communicating between HTTP server and dynamic scripting languages. Most popular HTTP servers support FASTCGI, including Apache, Nginx, and lighttpd, and fastcgi are supported by a number of scripting languages, including PHP. The
FastCGI is a development improvement from CGI. The main disadvantage of the traditional CGI interface approach is poor performance, because every time the HTTP server encounters a dynamic program, it needs to restart the script parser to perform the parsing, and the result is returned to the HTTP server. This is almost not available when handling high concurrent access. In addition, the traditional CGI interface is also very poor security, it is now rarely used. The
FastCGI interface uses the C/s structure, which separates the HTTP server from the script resolution server and initiates one or more script resolution Daemons on the script resolution server. Each time the HTTP server encounters a dynamic program, it can be delivered directly to the fastcgi process to execute, and the resulting results are returned to the browser. This approach allows HTTP servers to handle static requests in a single-minded manner or to return the results of a dynamic script server to the client, which greatly improves the performance of the entire application system.
nginx+fastcgi Operation
Nginx does not support direct calls or parsing of external programs, and all external programs (including PHP) must be invoked through the FastCGI interface. FastCGI interface in Linux is the socket, (this socket can be a file socket, can also be an IP socket). To invoke a CGI program, you also need a fastcgi wrapper (wrapper can be understood as a program to start another program), which is bound to a fixed socket, such as a port or a file socket. When Nginx sends a CGI request to this socket, the FastCGI interface wrapper the request and derives a new thread that invokes the interpreter or external program to process the script and read the return data; Wrapper then passes the returned data through the FastCGI interface to the nginx along the fixed socket, and Nginx sends the returned data to the client, which is the whole operation of the nginx+fastcgi.

PHP and Nginx Integration
php.ini:php Main configuration file

[Root@server79 php-5.4.12]# CP Php.ini-production/usr/local/lnmp/php/etc/php.ini

Copy PHP's startup script

[root@server79 fpm]# pwd
/root/php-5.4.12/sapi/fpm
[root@server79 fpm]# CP init.d.php-fpm/etc/init.d/ php-fpm

To add executable permissions to the startup script

[root@server79 fpm]# chmod +x/etc/init.d/php-fpm
[root@server79
~]# cgi.fix_pathinfo=0
date.timezone =/asia/shanghai
[root@server79 ~]# cp/usr/local/lnmp/php/etc/ php-fpm.conf.default/usr/local/lnmp/php/etc/php-fpm.conf
[root@server79 etc]# vim php-fpm.conf

Open comment pid = run/php-fpm.pid

php-fpm.conf File Parameter parsing
the global configuration file for PHP is php.ini, and in the previous steps, the file has been copied to/usr/local/lnmp/php/etc/php.ini. PHP.ini can be configured according to the different requirements of each application.
The following highlights the configuration files for the PHP-FPM engine.
Depending on the installation path specified above, the default profile for PHP-FPM is/usr/local/lnmp/php/etc/php-fpm.conf.
Php-fpm.conf is a plain text file in XML format, and its contents are easily understood. Here are a few key configuration tags:
The label listen_address is the IP address and port on which the FASTCGI process is configured to monitor, and the default is 127.0.0.1:9000.

Listen = 127.0.0.1:9000

Label user and group are used to set up users and groups of users who are running the fastcgi process. Note that the user and user groups specified here are consistent with the user and user groups specified in the Nginx configuration file.

user = Nginx
group = Nginx

Label Max_children the number of processes that are used to set fastcgi. According to the official recommendation, servers less than 2GB of memory can open only 64 processes, and servers with 4GB or more memory can open 200 processes.

<value name= "Max_children" >5</value>

The label request_terminate_timeout is used to set the time when the fastcgi executes the script. The default is 0s, which is unlimited execution, can be modified according to the situation.

<value name= "Request_terminate_timeout" >0s</value>

The label rlimit_files is used to set the PHP-FPM limit on open file descriptors, which defaults to 1024. The value of this tag must be associated with the number of open files for the Linux kernel, for example, to set this value to 65535, you must perform ' ULIMIT-HSN 65536 ' on the Linux command line.

<value name= "Rlimit_files" >1024</value>

The label max_requests indicates that each children will be closed after the maximum number of requests processed, and the default setting is 500.

Pm.max_requests = 500

The label allowed_clients is used to set the IP address that allows access to the FASTCGI process parser. If you do not specify an IP address here, the PHP resolution request forwarded by NGINX will not be accepted.

<value name= "Allowed_clients" >127.0.0.1</value>

5. Managing the FASTCGI process
After you configure PHP-FPM, you can start the fastcgi process. There are two ways to start the fastcgi process:

/usr/local/php/bin/php-cgi--FPM 

Or
/USR/LOCAL/PHP/SBIN/PHP-FPM start 

It is recommended that the FASTCGI process be started in the second way.
/USR/LOCAL/PHP/SBIN/PHP-FPM also has other parameters, specifically start|stop|quit|restart|reload|logrotate.
The meaning of each startup parameter is as follows:

    • Q Start, starting the PHP fastcgi process.
    • Q Stop, forcing the end of PHP's fastcgi process.
    • Q quit, smooth termination of PHP's fastcgi process.
    • Q Restart, restart the PHP fastcgi process.
    • Q Reload, reload PHP's php.ini.
    • Q Logrotate, re-enable log files.
    • Reload is an important parameter that can reload the changed php.ini without interrupting the fastcgi process in PHP, so you can smooth change the PHP settings in fastcgi mode by PHP-FPM.
[Root@server79 etc]#/etc/init.d/php-fpm start

Configure the Nginx master configuration file to open the interface with PHP

[root@server79 conf]# pwd
/usr/local/lnmp/nginx/conf
[root@server79 conf]# vim nginx.conf
user nginx;
#
Location ~ \.php$ {
root html; 
Fastcgi_pass 127.0.0.1:9000; Local 9000 port
fastcgi_index index.php;
# Fastcgi_param Script_filename/scripts$fastcgi_script_name;
Include fastcgi.conf;
}
[root@server79 conf]# nginx-t^c
[root@server79 conf]# nginx-s reload^c
[root@server79 html]# pwd]/usr/
local/lnmp/nginx/html
[root@server79 html]# cat index.php
<?php phpinfo
()?>

Test: Enter 192.168.0.179/index.php in the browser, the PHP page appears


PHP parsing nginx Log
Nginx log Format

Access_log log Format

 Log_format Main ' $server _name$remote_addr$remote_user[$time _local] "$request" ' 
      $status $body_bytes_sent "$ "Http_referer" " 
      $http _user_agent" "$http _x_forwarded_for"; 


Log Parameters

    server_name         : Host name  
for virtual hosts     remote_addr         : IP address  
  for remote clients    remote_user         : Remote client user name  
     time_local          : Time to visit and timezone  
     status          : Logging request returned HTTP status Code  
     body_bytes_sent     : Size of the file body content sent to the client  
    Http_referer             : from which page link to visit   
     http_user_agent     : Client browser information  
    http_x_forwarded _for    : The true ip  of the client;


Log Split character
use special nonprinting characters ^a (ctrl+v,ctrl+a) as a log separator


Filter file Contents according to keywords

Demand
Extract the contents of the file based on the "Weibo" keyword in the HTTP request

PHP code

 /** 
  * Description: Read the contents of the file by row filter Match 
  * 
  @return 
  array 
 /function readfilecontent ($filename) 
 { 
  $weibo _content = Array (); 
  $fh = @fopen ($filename, ' R '); 
   
  if ($fh) { 
   while (! feof ($fh)) { 
    $row = fgets ($fh, 4096); 
    $row _arr = Explode ("", $row); 
    if (Isset ($row _arr[3]) && preg_match ('/weibo/', $row _arr[3])) { 
     $weibo _content[] = $row _arr; 
    } 
   } 
  } 
  Fclose ($FH); 
   
  return $weibo _content; 
 } 

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.