PHP连接Nginx服务器并解析Nginx日志的方法
这篇文章主要介绍了PHP连接Nginx服务器并解析Nginx日志的方法,PHP+Nginx也是目前一种相当流行的服务器搭建方案,需要的朋友可以参考下
php与nginx整合
PHP-FPM也是一个第三方的FastCGI进程管理器,它是作为PHP的一个补丁来开发的,在安装的时候也需要和PHP源码一起编译,也就是说PHP-FPM被编译到PHP内核中,因此在处理性能方面更加优秀;同时它在处理高并发方面也比spawn-fcgi引擎好很多,因此,推荐Nginx+PHP/PHP-FPM这个组合对PHP进行解析。
FastCGI 的主要优点是把动态语言和HTTP Server分离开来,所以Nginx与PHP/PHP-FPM经常被部署在不同的服务器上,以分担前端Nginx服务器的压力,使Nginx专一处理静态请求和转发动态请求,而PHP/PHP-FPM服务器专一解析PHP动态请求
#fastcgi
FastCGI是一个可伸缩地、高速地在HTTP server和动态脚本语言间通信的接口。多数流行的HTTP server都支持FastCGI,包括Apache、Nginx和lighttpd等,同时,FastCGI也被许多脚本语言所支持,其中就有PHP。
FastCGI是从CGI发展改进而来的。传统CGI接口方式的主要缺点是性能很差,因为每次HTTP服务器遇到动态程序时都需要重新启动脚本解析器来执行解析,然后结果被返回给HTTP服务器。这在处理高并发访问时,几乎是不可用的。另外传统的CGI接口方式安全性也很差,现在已经很少被使用了。
FastCGI接口方式采用C/S结构,可以将HTTP服务器和脚本解析服务器分开,同时在脚本解析服务器上启动一个或者多个脚本解析守护进程。当HTTP服务器每次遇到动态程序时,可以将其直接交付给FastCGI进程来执行,然后将得到的结果返回给浏览器。这种方式可以让HTTP服务器专一地处理静态请求或者将动态脚本服务器的结果返回给客户端,这在很大程度上提高了整个应用系统的性能。
Nginx+FastCGI运行原理
Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket,(这个socket可以是文件socket,也可以是ip socket)。为了调用CGI程序,,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接纳到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端,这就是Nginx+FastCGI的整个运作过程。
php与nginx整合
php.ini:php的主配置文件
[root@server79 php-5.4.12]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini
拷贝php的启动脚本
[root@server79 fpm]# pwd /root/php-5.4.12/sapi/fpm [root@server79 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
给启动脚本加可执行权限
[root@server79 fpm]# chmod +x /etc/init.d/php-fpm [root@server79 ~]# vim /usr/local/lnmp/php/etc/php.ini 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
打开注释pid = run/php-fpm.pid
php-fpm.conf文件参数解析
PHP的全局配置文件是php.ini,在上面的步骤中,已经将此文件复制到了/usr/local/lnmp/php/etc/php.ini下。可以根据每个应用需求的不同,对php.ini进行相应的配置。
下面重点介绍PHP-FPM引擎的配置文件。
根据上面指定的安装路径,PHP-FPM的默认配置文件为/usr/local/lnmp/php/etc/php-fpm.conf。
php-fpm.conf是一个XML格式的纯文本文件,其内容很容易看明白。这里重点介绍几个重要的配置标签:
标签listen_address是配置fastcgi进程监听的IP地址以及端口,默认是127.0.0.1:9000。
listen = 127.0.0.1:9000
标签user和group用于设置运行FastCGI进程的用户和用户组。需要注意的是,这里指定的用户和用户组要和Nginx配置文件中指定的用户和用户组一致。
user = nginx group = nginx
标签max_children用于设置FastCGI的进程数。根据官方建议,小于2GB内存的服务器,可以只开启64个进程,4GB以上内存的服务器可以开启200个进程。
标签request_terminate_timeout用于设置FastCGI执行脚本的时间。默认是0s,也就是无限执行下去,可以根据情况对其进行修改。
标签rlimit_files用于设置PHP-FPM对打开文件描述符的限制,默认值为1024。这个标签的值必须和Linux内核打开文件数关联起来,例如要将此值设置为65535,就必须在Linux命令行执行'ulimit -HSn 65536'。
标签max_requests指明了每个children最多处理多少个请求后便会被关闭,默认的设置是500。
pm.max_requests = 500
标签allowed_clients用于设置允许访问FastCGI进程解析器的IP地址。如果不在这里指定IP地址,Nginx转发过来的PHP解析请求将无法被接受。
5.管理FastCGI进程
在配置完php-fpm后,就可以启动FastCGI进程了。启动fastcgi进程有两种方式:
/usr/local/php/bin/php-cgi --fpm
或者
/usr/local/php/sbin/php-fpm start
建议采用第二种方式启动FastCGI进程。
/usr/local/php/sbin/php-fpm还有其他参数,具体为start|stop|quit|restart|reload|logrotate。
每个启动参数的含义如下:
[root@server79 etc]# /etc/init.d/php-fpm start
配置nginx的主配置文件,打开与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; //本地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
测试:浏览器中输入192.168.0.179/index.php,出现php页面
PHP解析Nginx日志
nginx日志格式
access_log日志格式
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"';
日志参数

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.
