How to connect PHP to the Nginx server and parse Nginx logs

Source: Internet
Author: User
This article mainly introduces how to connect PHP to the Nginx server and parse Nginx logs. PHP + Nginx is also a very popular server construction solution. For more information, see

This article mainly introduces how to connect PHP to the Nginx server and parse Nginx logs. PHP + Nginx is also a very popular server construction solution. For more information, see

Php and nginx Integration

PHP-FPM is also a third-party FastCGI Process Manager, It is developed as a patch of PHP, also needs to be compiled together with the PHP source code during installation, that is to say, the PHP-FPM is compiled into the PHP kernel, so it is better in terms of processing performance; it is also much better in terms of processing high concurrency than the spawn-fcgi engine, so, it is recommended that Nginx + PHP/PHP-FPM this combination of PHP parsing.
FastCGI's main advantage is the dynamic language and HTTP Server separated, so Nginx and PHP/PHP-FPM are often deployed on different servers, to share the front-end Nginx Server pressure, enables Nginx to specifically handle static requests and forward dynamic requests, while PHP/PHP-FPM servers specifically parse PHP dynamic requests

# Fastcgi
FastCGI is a scalable and fast interface for communication between HTTP server and dynamic scripting language. Most popular HTTP servers support FastCGI, including Apache, Nginx, and lighttpd. FastCGI is also supported by many script languages, including PHP.
FastCGI is improved from CGI development. The main disadvantage of the traditional CGI interface method is poor performance, because every time the HTTP server encounters a dynamic program, it needs to restart the script parser to execute the parsing, and then the result is returned to the HTTP server. This is almost unavailable when processing highly concurrent access. In addition, the traditional CGI interface method has poor security and is rarely used.
The FastCGI interface adopts the C/S structure, which can separate the HTTP server and the script parsing server, and start one or more script parsing Daemon Processes on the script parsing server. When the HTTP server encounters a dynamic program, it can be directly delivered to the FastCGI process for execution, and then the result is returned to the browser. This method allows the HTTP server to process static requests or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.
Nginx + FastCGI operating principles
Nginx does not support direct calling or parsing of external programs. All external programs (including PHP) must be called through the FastCGI interface. The FastCGI interface is a socket in Linux (this socket can be a file socket or an ip socket ). To call the CGI program, you also need a FastCGI wrapper (wrapper can be understood as the program used to start another program). This wrapper is bound to a fixed socket, such as a port or file socket. When Nginx sends a CGI request to this socket, through the FastCGI interface, wrapper accepts the request and then derives a new thread, this thread calls the interpreter or external program to process the script and read the returned data. Then, wrapper transmits the returned data to Nginx through the FastCGI interface along a fixed socket. Finally, nginx sends the returned data to the client, which is the entire operation process of Nginx + FastCGI.

Php and nginx Integration
Php. ini: Main configuration file of php

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

Copy the php 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

Add executable permissions to the Startup Script

[Root @ server79 fpm] # chmod + x/etc/init. d/php-fpm [root @ server79 ~] # Vim/usr/local/lnmp/php/etc/php. inicgi. fix_pathinfo = 0date. 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 annotation pid = run/php-fpm.pid

Php-fpm.conf file Parameter Parsing
The global configuration file of PHP is php. ini. In the above step, the file has been copied to/usr/local/lnmp/php/etc/php. ini. You can configure php. ini based on different application requirements.
The following describes the configuration files for the PHP-FPM engine.
The default configuration file for the PHP-FPM is/usr/local/lnmp/php/etc/php-fpm.conf, depending on the installation path specified above.
Php-fpm.conf is a plain text file in XML format and its content is easy to understand. Here we will focus on several important configuration labels:
The listen_address label is the IP address and port configured for fastcgi process listening. The default value is 127.0.0.1: 9000.

Listen = 127.0.0.1: 9000

The tag user and group are used to set the user and user group for running the FastCGI process. Note that the user and user group specified here must be consistent with the user and user group specified in the Nginx configuration file.

User = nginxgroup = nginx

The label max_children is used to set the number of FastCGI processes. According to official recommendations, for servers with less than 2 GB of memory, only 64 processes can be enabled, and for servers with more than 4 GB of memory, 200 processes can be enabled.

5

The request_terminate_timeout label is used to set the time for FastCGI to execute the script. The default value is 0 s, that is, it can be infinitely executed and can be modified as needed.

0 s

The label rlimit_files is used to set the PHP-FPM's limit on opening file descriptors. The default value is 1024. The value of this label must be associated with the number of files opened in the Linux kernel. For example, to set this value to 65535, you must execute 'ulimit-HSn 65536 'on the Linux Command Line '.

1024

The max_requests label indicates the maximum number of requests processed by each children. The default value is 500.

Pm. max_requests = 500

The label allowed_clients is used to set the IP addresses allowed to access the FastCGI process parser. If the IP address is not specified here, the PHP resolution request forwarded by Nginx will not be accepted.

127.0.0.1

5. Manage FastCGI Processes
After configuring 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

We recommend that you use the second method to start the FastCGI process.
/Usr/local/php/sbin/php-fpm has other parameters, such as start | stop | quit | restart | reload | logrotate.
The meanings of each startup parameter are as follows:

[Root @ server79 etc] #/etc/init. d/php-fpm start

Configure the main configuration file of nginx and open the interface with php

[Root @ server79 conf] # pwd/usr/local/lnmp/nginx/conf [root @ server79 conf] # vim nginx. confuser nginx; # location ~ \. Php $ {root html; fastcgi_pass 127.0.0.1: 9000; // local port 9000 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 <? Phpphpinfo ()?>

Test: Enter 192.168.0.179/index. php In the browser. The php page appears.


Parsing Nginx logs using PHP
Nginx log format

Access_log log format

Log_format main '$ server_name $ remote_addr $ remote_user [$ time_local] "$ request" ''$ status $ response" $ http_referer "'' "$ http_user_agent" "$ http_x_forwarded_for "';


Log Parameters

Related Article

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.