Table of Contents
回复内容:
Home Backend Development PHP Tutorial 既然apache也是调php解析器php-cgi.exe去执行php的,为啥一定要安装apache呢?

既然apache也是调php解析器php-cgi.exe去执行php的,为啥一定要安装apache呢?

Jun 06, 2016 pm 08:34 PM
cgi php

既然apache也是调php解析器php-cgi.exe去执行php的,为啥一定要安装apache呢?
为啥不是直接把请求给php-cgi.exe, 还要经过apache绕一下?

回复内容:

既然apache也是调php解析器php-cgi.exe去执行php的,为啥一定要安装apache呢?
为啥不是直接把请求给php-cgi.exe, 还要经过apache绕一下?

既然apache也是调php解析器php-cgi.exe去执行php的,为啥一定要安装apache呢?

一个php的地址请求的时序图是这样的,php-cgi是和apache之间进行fastcgi通信的监守程序,是不能被去掉的。
当然apache还有一种不通过fastcgi进行调用php的方式,即apache模块方式:

<code>LoadModule php5_module /usr/lib/apache/libphp5.so
</code>
Copy after login

这个时候时序图上是没有php-cgi这个实体的,apache直接调用php.

apache实现了http协议及相关内容,而php只是可以作为它的一个扩展而已
如果你用php实现http协议相关内容,也可以无需apache的。

当然你也可以这样:

<code>php -S 0.0.0.0:8080 -t /www
</code>
Copy after login

直接用php内置的web server,无需安装apache。

PHP作为Apache的一个module(libphp5.so)集成到Apache进程(httpd)运行,这种mod_php的运行模式是最常用的,这时PHP的运行跟php-cgi没有任何关系.mod_php的配置是这样:

<code>LoadModule php5_module modules/libphp5.so
AddHandler application/x-httpd-php .php
</code>
Copy after login

可以配置extra/httpd-mpm.conf来控制Apache的进程数量.

如果你在Apache里用mod_fcgid方式来运行PHP,则需要使用php-cgi,这时Apache作为有一个FastCGI进程管理器来管理php-cgi,在httpd.conf配置:

<code>#载入mod_fcgid模块(不能和php5_module同时使用)
LoadModule fcgid_module modules/mod_fcgid.so
AddHandler fcgid-script .php
#php-cgi的进程数量
FcgidMaxProcesses 5
#指定php-cgi来处理php请求
FcgidWrapper /png/php/5.4.39NTS/bin/php-cgi .php
#在需要使用php-cgi来执行PHP的Directory加上Options +ExecCGI,比如:
<directory>
    Options +ExecCGI
</directory>
</code>
Copy after login

另外在Linux上还有一种非常常用的组合,就是Nginx搭配php-fpm使用,其中php-fpm跟php-cgi没有任何关系,php-fpm自身就集成了PHP解释器,php-fpm有自己的进程管理器,只处理Nginx转发过来的PHP请求并返回,跟Nginx是松耦合的.

php-cgi.exe不是http服务器,它接受的协议和apache完全不一样。如果需要去掉apache就必须用php实现http协议,会导致很多无用代码还可能有bug。

一个网站的构成,除了php生成的动态内容之外,还有其它的部分,比如静态内容,如果由php去下发这些静态内容的话,效率自然不如专门为这种工作优化过的web server来得高效。

除了静态内容之外,还有记录访问日志、url rewrite、http协议相关的很多东西,假设世界上没有web server这种东西,那我们无论使用什么语言来开发网站,都必须把这些东西写一遍。把这些东西独立出来,实现成为web server,就节约了大量的重复性劳动,而且无论后端使用任何语言,都可以直接通过部署到web server享受到这些便利。这一点其实跟我们在设计系统的时候把某些逻辑专门抽象封装出来是一个道理。

别的不说,后端直接暴露在公网你还真敢干啊。。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles