mod_php、FastCGI、PHP-FPM等PHP运行方式对比
mod_php、FastCGI、PHP-FPM等PHP运行方式对比
这篇文章主要介绍了mod_php、FastCGI、PHP-FPM等PHP运行方式对比,本文讲解了什么是PHP处理器(PHP handlers)、mod_php的优缺点、FastCGI的优缺点、PHP-FPM(FastCGI Process Manager)的优缺点等内容,需要的朋友可以参考下
概述
写这篇文章的是因为今天要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 and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
