mod_php、FastCGI、PHP-FPM等PHP运行方式对比,mod_phpfastcgi
mod_php、FastCGI、PHP-FPM等PHP运行方式对比,mod_phpfastcgi
概述
写这篇文章的是因为今天要Ubuntu下搭建LNMP环境,Nginx使用的是PHP-FPM,所以对Web服务器与PHP解释器的交互方式做了个整理。
众所周知,PHP是跨平台、跨服务器的语言,这也是它如此流行的原因之一。但是,很少有人知道PHP解释器可以以不同的方式运行在Web服务器中。PHP最常用的方式是以模块的方式(mod_php)运行在Apache中,也是Apache运行PHP的默认方式。但是在Nginx中,Nginx又使用的是PHP-FPM。
这篇文章就对这些概念做个介绍,如有不对的地方,请多多批评指教。
什么是PHP处理器(PHP handlers)?
首先需要记住的是,任何一种Web服务器(Apache、Nginx等)都是被设计成向用户发送html、图片等静态资源的,Web服务器自身并不能解释任何动态脚本(PHP、Python等)。PHP处理器就是用来解释Web应用中的PHP代码,并将它解释为HTML或其他静态资源,然后将解析的结果传给Web服务器,最后再由Web服务器发送给用户。大多数的Web服务器都不能解析PHP代码,因此它需要一个能解析PHP代码的程序,这就是PHP处理器。
mod_php
首先,来看一下以Apache模块方式运行PHP。mod_php现在在Linux各版本的软件仓库里都有,因此很容易被安装。
当PHP以模块的方式运行在Apache中时,PHP解释器被“内嵌”在Apache的进程里。Apache不会调用任何外部的PHP进程,因此这种方式使Apache与PHP能更好的通信。但是,当以这种方式运行PHP的时候,哪怕Apache提供的仅仅是静态的资源(如HTML),Apache的每个子进程也都会载入 mod_php,导致了比正常情况下更多的内存开销。
以这种方式运行的另一个缺点是,它仅能与Apache一起配合工作。另外,在小型的VPS和大型的网站中,这种方式也不合适,因为大型网站可能有很多静态资源,而这些静态资源是不需要PHP程序解释的。
优点:
1.易于安装和更新
2.容配置
缺点:
1.仅能与Apache一起工作
2.增加了Apache子进程内存开销
3.当更改php.ini文件后,需要重启Apache
FastCGI
FastCGI是交互程序与Web服务器通用的协议接口,是早期CGI(Common Gateway Interface)的一个变种。相对于CGI来说,FastCGI减少了和Web服务器交互的开销,同时一次可以处理更多的请求。
Apache可以以mod_fcgid的形式使用FastCGI。其他Web服务器,如lighttpd, nginx, Cherokee,甚至微软的IIS也都能使用FastCGI。使用FastCGI,可以同时设置多个版本的PHP,这在某些情况下非常有用。
FastCGI还利用suexec来支持不同的用户用自己的PHP的实例。这个特性对于在共享环境下提高安全性尤其重要。FastCGI在保证性能的同时,也减少了Web服务器的内存开销。
优点:
1.兼容多数Web服务器
2.比mod_php占内存小
3.更多的配置项,包括多版本PHP和suexec
缺点
1.配置复杂
2.不被大家所熟知
PHP-FPM(FastCGI Process Manager)
PHP-FPM是Web服务器使用PHP的一种最新方式,也是PHP FastCGI的另外一种实现。PHP-FPM对于运行在小型VPS和多服务器上的Web应用非常应用。同时,它也可以被兼容FastCGI的任何Web服务器所使用。
PHP-FPM使管理员能够优雅地停止和启动PHP工作进程而不丢失任何查询。这允许我们逐步更新配置和二进制,而不会损失任何查询。它还允许我们在发生任何意外破坏的情况下,紧急重启进程。
优点:
1.兼容多数Web服务器
2.比mod_php占内存小
3.更多的配置项,包括多版本PHP和suexec
缺点
1.配置复杂
2.不被大家所熟知
补充:Apache运行PHP的四种方式
mod_php (DSO,Dynamic Shared Object)
CGI
suPHP
FastCGI
总结
内容相对抽象,理解的不深,记录下留作以后参考~:)

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
