search
HomeBackend DevelopmentPHP ProblemThere are several startup modes in php
There are several startup modes in phpSep 16, 2019 am 11:58 AM
phpstartup mode

There are several startup modes in php

Several common startup modes of PHP:

1. CGI mode

CGI is the universal gateway interface (Common Gateway Interface), it is a program. In layman's terms, CGI is like a bridge that connects the web page and the execution program in the Web server. It passes the instructions received by HTML to the execution of the server. program, and then returns the result of the server's execution of the program to the HTML page. CGI is extremely cross-platform and can be implemented on almost any operating system. CGI is already an older model and has rarely been used in recent years.

Every time there is a user request, a CGI sub-process will be created first, then the request will be processed, and the sub-process will be terminated after processing. This is the Fork-And-Execute mode. When the number of user requests is very large, a large amount of system resources such as memory, CPU time, etc. will be occupied, resulting in low performance. Therefore, a server using CGI will have as many CGI sub-processes as there are connection requests. Repeated loading of sub-processes is the main reason for low CGI performance.

If you do not want to embed PHP into server-side software (such as Apache) and install it as a module, you can choose to install it in CGI mode. Or use PHP with different CGI wrappers to create secure chroot and setuid environments for your code. In this way, each client requests a PHP file, and the Web server calls php.exe (php.exe under win, php under Linux) to interpret the file, and then returns the result of the interpretation to the client in the form of a web page. This installation method usually installs the PHP executable file into the cgi-bin directory of the web server. CERT Recommendation CA-96.11 recommends not placing any interpreters in the cgi-bin directory.

The advantage of this method is that it separates the Web Server from specific program processing, has a clear structure and strong controllability. At the same time, the disadvantage is that if there is high access demand, the CGI process Fork will It becomes a huge burden on the server. Just imagine that hundreds of concurrent requests cause the server to fork hundreds of processes. This is why CGI has always been notorious for low performance and high resource consumption.

2. FastCGI mode

FastCGI is an upgraded version of CGI. FastCGI is like a long-live CGI, which can be executed all the time. As long as it is activated, it will not take time to Fork every time (this is the most criticized fork-and-execute mode of CGI).

FastCGI is a scalable, high-speed interface for communication between HTTP servers and dynamic scripting languages. Most popular HTTP servers support FastCGI, including Apache, Nginx and lighttpd. At the same time, FastCGI is also supported by many scripting languages, including PHP.

FastCGI interface mode adopts C/S structure, which can separate the HTTP server and the script parsing server, and start one or more script parsing daemons on the script parsing server. Every time the HTTP server encounters a dynamic program, it can be delivered directly to the FastCGI process for execution, and then the result is returned to the browser. This method allows the HTTP server to exclusively 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.

3. CLI mode

PHP-CLI is the abbreviation of PHP Command Line Interface, as its name means, it is PHP The interface running on the command line is different from the PHP environment (PHP-CGI, ISAPI, etc.) running on the web server. In other words, PHP can not only write front-end web pages, it can also be used to write back-end programs. PHP CLI Shell Scripting applies to all PHP advantages, enabling the creation of either scripts or server-side systems or even with GUI applications. PHP-CLI mode is supported under both Windows and Linux.

[Advantages]

1) Using multiple processes, after the child process ends, the kernel will be responsible for recycling resources;

2) Using multiple processes, The abnormal exit of the child process will not cause the entire process Thread to exit, and the parent process will still have the opportunity to rebuild the process;

3) A resident main process is only responsible for task distribution, and the logic is clearer.

We often use "php -m" under Linux to find out which extensions PHP has installed, which is the PHP command line running mode; interested students can enter "php -h" to study the running mode in depth.

4. Module mode

Module mode is integrated in the form of mod_php5 module. At this time, the function of mod_php5 module is to receive PHP file requests passed by Apache. , and process these requests, and then return the processed results to Apache. If we configure the PHP module (mod_php5) in its configuration file before Apache starts, the PHP module registers the ap_hook_post_config hook of apache2 and starts this module when Apache starts to accept requests for PHP files.

In addition to this loading method at startup, Apache modules can be dynamically loaded at runtime, which means that the server can be expanded without the need to recompile the source code, or even without stopping the server at all. All we need to do is to send the signal HUP or AP_SIG_GRACEFUL to the server to notify the server to reload the module. But before dynamic loading, we need to compile the module into a dynamic link library. Dynamic loading at this time is to load the dynamic link library. The processing of dynamic link libraries in Apache is completed through the module mod_so, so the mod_so module cannot be dynamically loaded, it can only be statically compiled into the core of Apache. This means it is started along with Apache.

How does Apache load modules? Let’s take the mod_php5 module mentioned earlier as an example. First we need to add a line to Apache's configuration file httpd.conf:

LoadModule php5_module modules/mod_php5.so

Here we use the LoadModule command. The first parameter of the command is the name of the module. The name can be found in the source code of the module implementation. . The second option is the path where the module is located. If you need to load a module while the server is running, you can send the signal HUP or AP_SIG_GRACEFUL to the server. Once the signal is received, Apache will reload the module without restarting the server.

This operating mode is what we often used when using the apache server in the windows environment. In modularization (DLL), PHP is started and run together with the web server. (It is an extension of apache based on CGI to speed up the operating efficiency of PHP).

5. ISAPI mode

ISAPI (Internet Server Application Program Interface) is a A set of API interfaces for Internet services. An ISAPI DLL can reside in memory after being activated by a user request and wait for another user request. Multiple user request processing functions can also be set in one DLL. In addition, an ISAPI DLL The application and the WWW server are in the same process, and the efficiency is significantly higher than CGI. (Due to Microsoft's exclusivity, it can only run in the windows environment)

As an Apache module, the Apache server pre-generates multiple process copies and resides in the memory after the system starts. Once a request occurs, it will be immediately Use these spare sub-processes for processing, so there is no delay caused by spawning sub-processes. These server copies do not exit immediately after processing an HTTP request, but stay in the computer waiting for the next request. The response to client browser requests is faster and the performance is higher.

Recommended tutorial: PHP video tutorial

The above is the detailed content of There are several startup modes in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software