PHP: the opening, the bottom of the world's best language is how to run

PHP is ridicule in the industry, is the world's best language, PHP is indeed in Web development tool, specifically, is a C language software framework includes a number of components of the module. It is a powerful UI framework.

PHP dynamic language execution process: After get a piece of code, through the lexical parse, syntax analysis phases, a source program is translated into instructions (with opcodes), then the virtual machine ZEND sequentially executing the instructions to complete the operation. PHP itself is implemented in C, so the final call to the C function is, in fact, we can be seen as a PHP C software development.

Layered Architecture

Complex layered structure needs, TCP / IP protocol because stratification has been very good application, unfamiliar students can go to my computer network column of the article to see, PHP is divided into four layers, slicing improve processing efficiency and functions.

PHP is a four-layer system from the bottom down:

1, Zend Engine: Zend overall pure C implementation, is part of the core PHP, he will translate PHP code (morphology, syntax analysis and a series of compilation) to perform opcode process and to achieve the appropriate approach to achieve the basic data structures (such as: hash_table, OO), memory allocation and management, to provide the corresponding api method for external calls, the core of everything, all the peripheral functions are around the Zend implementation.

2, Extensions: around the Zend engine, extensions provide a variety of basic services through modular way, our common variety of built-in functions (array series), the standard library are all that is to implement CGI Common Gateway Interface, users can also by extension typical applications may need to implement your own extension based).

Bloggers in your sleep: When impression on the win development, there ext folder, which is loaded with a variety of libraries, that is, Extensions layer.

3, Sapi: Sapi full name Server Application Programming Interface, which is server-side application programming interface, Sapi by a series of hook function, so that PHP can interact with data and peripherals, PHP which is very elegant and successful design, by the success of the PHP sapi decoupled itself and the upper application isolation, PHP can no longer consider how compatible for different applications, and the application itself can also achieve a different approach for its own characteristics.

4, the upper application: This is what we usually write PHP applications, obtained by a variety of different ways spai application mode, how to implement a web application through webserver, at the command line script has been run, and so on.

Cow

Sapi through a series of interfaces, so that external applications can exchange data and PHP and can achieve a particular processing method according to the characteristics of different applications, we often have some of sapi:

var_dump(php_sapi_name()); # 输出当前php运行模式

1、CGI/FastCGI

He explained in detail below.

2、APACHE2HANDLER

PHP as Apache module, Apache server after the system starts, a plurality of processes generated in advance in the memory-resident copies, request occurs once, immediately use the spare subprocesses are processed, so there is a delay caused by generating sub-process a. The server copy does not exit immediately after handling an HTTP request, but stay in the machine for the next request. For the requesting client browser more responsive, higher performance.

3, apache DLL module

This mode of operation is that we previously used in the windows environment apache servers are often used in a modular (DLL) in, PHP is up and running with the Web server.

Bloggers in your sleep: When impression on the win development, there ext folder, which is loaded with a variety of libraries, that is the end of the .dll file.

4、Cli

Is at the start with the design of the language in order to execute PHP scripts, so the win in both the Linux and Cli mode, Cli mode will be forced to use some configuration parameters, forced off the data buffer, the implementation is very efficient, commonly used parameters:

-a 交互式运行PHPCLI程序
-c 告诉PHP,从那个路径寻找php.ini
-l 检查php文件的语法,不执行
-m 查看编译的模块

CGI/FastCGI

Before Web services on the Internet are all html, static distribution of resources, with the development of the Internet, and later appeared as the asp, jsp, php this dynamic language features of dynamic languages ​​is the need to resolve Cgi Web server is a Web server and dynamic gateway verbal communication.

CGI即通用网关接口(Common Gateway Interface),它是动态语言的产物,是负责Web服务与PHP通信的接口,CGI方式在遇到连接请求(用户 请求)先要创建cgi的子进程,激活一个CGI进程,然后处理请求,处理完后结束这个子进程。这就是fork-and-execute模式。所以用cgi 方式的服务器有多少连接请求就会有多少cgi子进程,子进程反复加载是cgi性能低下的主要原因。

在这个基础上产生了FastCGI,FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一 次。PHP使用PHP-FPM(FastCGI Process Manager),全称PHP FastCGI进程管理器进行管理。

NGINX与php-fpm

我理解的php-fpm是PHP对FastCGi封装的模块,是PHP对CGI网关通信的优化和扩展,Web服务器都在php-fpm实现。配置文件里的listen。listen负责监听ip和端口,查看php-fpm的配置文件,查看监听端口,默认是9000,然后配置你的Nginx。

ps aux | grep php
listen = 127.0.0.1:9000

Nginx Server里配置:

server {

    listen       80;
	...

    location ~ \.php/ {
        include        fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param  PATH_INFO $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_pass   127.0.0.1:9000;
    }
}
发布了98 篇原创文章 · 获赞 185 · 访问量 9万+

Guess you like

Origin blog.csdn.net/xuezhiwu001/article/details/99288265