这里有新鲜出炉的 PHP 设计模式,程序狗速度看过来!
PHP(外文名: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源脚本语言。语法吸收了 C 语言、Java 和 Perl 的特点,入门门槛较低,易于学习,使用广泛,主要适用于 web 开发领域。PHP 的文件后缀名为 php。
这篇文章主要介绍了 php 的 4 种常用运行方式,CGI、FastCGI、APACHE2HANDLER 和 CLI,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
php 的 4 种常用运行方式:CGI、FastCGI、APACHE2HANDLER、CLI。
1、CGI
CGI 即通用网关接口(common gatewag interface), 它是一段程序,通俗的讲 CGI 就象是一座桥,把网页和 WEB 服务器中的执行程序连接起来,它把 html 接收的指令传递给服务器的执 行程序,再把服务器执行程序的结果返还给 HTML 页。CGI 的跨平台性能极佳,几乎可以在任何操作系统上实现。
CGI 方式在遇到连接请求(用户 请求)先要创建 cgi 的子进程,激活一个 CGI 进程,然后处理请求,处理完后结束这个子进程。这就是 fork-and-execute 模式。所以用 cgi 方式的服务器有多少连接请求就会有多少 cgi 子进程,子进程反复加载是 cgi 性能低下的主要原因。都会当用户请求数量非常多时,会大量挤占系统的资源如内 存,CPU 时间等,造成效能低下。
2、FastCGI
fast-cgi 是 cgi 的升级版本,FastCGI 像是一个常驻 (long-live) 型的 CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去 fork 一 次。PHP 使用 PHP-FPM(FastCGI Process Manager),全称 PHP FastCGI 进程管理器进行管理。
Web Server 启动时载入 FastCGI 进程管理器 (IIS ISAPI 或 Apache Module)。FastCGI 进程管理器自身初始化,启动多个 CGI 解释器进程(可见多个 php-cgi) 并等待来自 Web Server 的连接。
当客户端请求到达 Web Server 时,FastCGI 进程管理器选择并连接到一个 CGI 解释器。Web server 将 CGI 环境变量和标准输入发送到 FastCGI 子进程 php-cgi。
FastCGI 子进程完成处理后将标准输出和错误信息从同一连接返回 Web Server。当 FastCGI 子进程关闭连接时,请求便告处理完成。FastCGI 子进程接着等待并处理来自 FastCGI 进程管理器 (运行在 Web Server 中) 的下一个连接。 在 CGI 模式中,php-cgi 在此便退出了。
在上述情况中,你可以想象 CGI 通常有多慢。每一个 Web 请求 PHP 都必须重新解析 php.ini、重新载入全部扩展并重初始化全部数据结构。使用 FastCGI,所有这些都只在进程启动时发生一次。一个额外的 好处是,持续数据库连接 (Persistent database connection) 可以工作。
3、APACHE2HANDLER
PHP 作为 Apache 模块,Apache 服务器在系统启动后,预先生成多个进程副本驻留在内存中,一旦有请求出 现,就立即使用这些空余的子进程进行处理,这样就不存在生成子进程造成的延迟了。这些服务器副本在处理完一次 HTTP 请求之后并不立即退出,而是停留在计算机中等待下次请求。对于客户浏览器的请求反应更快,性能较高。
4、CLI
cli 是 php 的命令行运行模式,cli 端的运行命令有时会很有用,以下总结几个:
查看 php 版本信息
- eric:~ youngeric$ php -v
- PHP 5.5.38 (cli) (built: Oct 1 2016 23:03:00)
- Copyright (c) 1997-2015 The PHP Group
- Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
查看当前 php 的扩展
- eric:~ youngeric$ php -m
- [PHP Modules]
- bcmath
- bz2
- calendar
- Core
- ctype
- curl
- date
- ......
查看 php.ini 配置信息(相当于使用 phpinfo() 函数)
- eric:~ youngeric$ php -ini
- phpinfo()
- PHP Version => 5.5.38
- System => Darwin eric.local 16.1.0 Darwin Kernel Version 16.1.0: Wed Oct 19 20:31:56 PDT 2016; root:xnu-3789.21.4~4/RELEASE_X86_64 x86_64
- Build Date => Oct 1 2016 23:01:51
- Configure Command => './configure' '--prefix=/usr/local/Cellar/php55/5.5.38_11' '--localstatedir=/usr/local/var' '--sysconfdir=/usr/local/etc/php/5.5' '--with-config-file-path=/usr/local/etc/php/5.5' '--with-config-file-scan-dir=/usr/local/etc/php/5.5/conf.d' '--mandir=/usr/local/Cellar/php55/5.5.38_11/share/man' '--enable-bcmath' '--enable-calendar' '--enable-dba' '--enable-exif' '--enable-ftp' '--enable-gd-native-ttf' '--enable-mbregex' '--enable-mbstring' '--enable-shmop' '--enable-soap' '--enable-sockets' '--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm' '--enable-wddx' '--enable-zip' '--with-freetype-dir=/usr/local/opt/freetype' '--with-gd' '--with-gettext=/usr/local/opt/gettext' '--with-iconv-dir=/usr' '--with-icu-dir=/usr/local/opt/icu4c' '--with-jpeg-dir=/usr/local/opt/jpeg' '--with-kerberos=/usr' '--with-libedit' '--with-mhash' '--with-ndbm=/usr' '--with-png-dir=/usr/local/opt/libpng' '--with-xmlrpc' '--with-zlib=/usr' '--with-readline=/usr/local/opt/readline' '--without-gmp' '--without-snmp' '--with-libxml-dir=/usr/local/opt/libxml2' '--with-pdo-odbc=unixODBC,/usr/local/opt/unixodbc' '--with-unixODBC=/usr/local/opt/unixodbc' '--with-bz2=/usr' '--with-openssl=/usr/local/opt/openssl' '--enable-fpm' '--with-fpm-user=_www' '--with-fpm-group=_www' '--with-curl' '--with-xsl=/usr' '--with-ldap' '--with-ldap-sasl=/usr' '--with-mysql-sock=/tmp/mysql.sock' '--with-mysqli=mysqlnd' '--with-mysql=mysqlnd' '--with-pdo-mysql=mysqlnd' '--disable-opcache' '--enable-pcntl' '--without-pear' '--enable-dtrace' '--disable-phpdbg' '--enable-zend-signals'
- Server API => Command Line Interface
- Virtual Directory Support => disabled
- Configuration File (php.ini) Path => /usr/local/etc/php/5.5
- Loaded Configuration File => /usr/local/etc/php/5.5/php.ini
- Scan this dir for additional .ini files => /usr/local/etc/php/5.5/conf.d
- ......
查看函数信息
- eric:~ youngeric$ php --rf date
- Function [ <internal:date> function date ] {
- - Parameters [2] {
- Parameter #0 [ <required> $format ]
- Parameter #1 [ <optional> $timestamp ]
- }
- }
查看类信息
- eric:~ youngeric$ php --rc pdo
- Class [ <internal:PDO> class PDO ] {
- - Constants [89] {
- Constant [ integer PARAM_BOOL ] { 5 }
- Constant [ integer PARAM_NULL ] { 0 }
- Constant [ integer PARAM_INT ] { 1 }
- Constant [ integer PARAM_STR ] { 2 }
- Constant [ integer PARAM_LOB ] { 3 }
- Constant [ integer PARAM_STMT ] { 4 }
- Constant [ integer PARAM_INPUT_OUTPUT ] { 2147483648 }
- ......
检测 php 代码
- eric:~ youngeric$ php -l jiance.php
- PHP Parse error: syntax error, unexpected end of file, expecting ',' or ';' in jiance.php on line 3
- Errors parsing jiance.php
作为世界上最好的语言,php 甚至还内置了服务器的功能(有没有很震惊的样子)。
- eric:Desktop youngeric$ php -S 127.0.0.1:8080
- PHP 5.5.38 Development Server started at Thu Dec 22 09:44:20 2016
- Listening on http://127.0.0.1:8080
- Document root is /Users/youngeric/Desktop
- Press Ctrl-C to quit.
- [Thu Dec 22 09:44:29 2016] 127.0.0.1:52988 [404]: / - No such file or directory
- [Thu Dec 22 09:44:29 2016] 127.0.0.1:52989 [404]: /favicon.ico - No such file or directory
来源: http://www.phperz.com/article/17/0806/340807.html