PHP入門及服務環境配置(Nginx+PHP)php
PHP維基百科:html
PHP(全稱:PHP:Hypertext Preprocessor,即「PHP:超文本預處理器」)是一種開源的通用計算機腳本語言,尤爲適用於網絡開發並可嵌入HTML中使用。PHP的語法借鑑吸取C語言、Java和Perl等流行計算機語言的特色,易於通常程序員學習。PHP的主要目標是容許網絡開發人員快速編寫動態頁面,但PHP也被用於其餘不少領域。mysql
最新版本:2015年12月3日 7.0.0發佈nginx
PHP是一個應用範圍很廣的語言,特別是在網絡程序開發方面。git
使用PHP不須要任何費用,官方組織PHP Group提供了完整的程序源代碼,容許用戶修改、編譯、擴充來使用。程序員
參考了Perl、C語言,並且能夠集成在HTML之中。github
簡單的Hello World代碼:web
<?php echo 'Hello World!'; ?>
PHP剖析引擎只剖析<?php
到?>
之間的代碼,而不包含在<?php
到?>
之間的內容則會直接提交。sql
在判斷語句中的HTML代碼並不會被直接提交:shell
<?php if (false) { ?> HTML Code <?php } ?>
註釋
C與C++所使用的
/*...*/
//
#
數據類型
變量
PHP框架
Laravel:
Symfony2:
Thinkphp:
庫
源代碼
PHP編譯器
PHP一直被看成解釋器使用。
PHP編譯器則將PHP從解釋器中分離,爲加快運行和改善與以其餘編程語言編寫部分的互通性。
PHP 版本
論壇評論摘要:
結論:使用 >=5.3 ,同時不宜太新。
網站和 web 應用程序在一般狀況下,須要三樣東西:
若是須要本身配置服務器和 PHP,有兩個方法將 PHP 鏈接到服務器上。
下面將採用 FastCGI + Nginx 的方式。
在 Unix 平臺下安裝 PHP 有幾種方法:
FastCGI:
快速通用網關接口(Fast Common Gateway Interface/
FastCGI
)是一種讓交互程序與Web服務器通訊的協議。FastCGI是早期通用網關接口(CGI
)的加強版本。
FastCGI致力於減小網頁服務器與CGI程序之間互動的開銷,從而使服務器能夠同時處理更多的網頁請求。
與爲每一個請求建立一個新的進程不一樣,FastCGI使用持續的進程來處理一連串的請求。這些進程由FastCGI服務器管理,而不是web服務器。 當進來一個請求時,web服務器把環境變量和這個頁面請求經過一個socket好比FastCGI進程與web服務器(都位於本地)或者一個TCP connection(FastCGI進程在遠端的server farm)傳遞給FastCGI進程。
CGI:
通用網關接口(Common Gateway Interface/
CGI
)是一種重要的互聯網技術,可讓一個客戶端,從網頁瀏覽器向執行在網絡服務器上的程序請求數據。CGI描述了客戶端和服務器程序之間傳輸數據的一種標準。
Nginx(發音同engine x)是一個網頁服務器,它能反向代理HTTP, HTTPS, SMTP, POP3, IMAP的協議連接,以及一個負載均衡器和一個HTTP緩存。是一款面向性能設計的HTTP服務器,相較於Apache、lighttpd具備佔有內存少,穩定性高等優點。
發明人是俄國人。此軟件BSD-like協議下發行,能夠在UNIX、GNU/Linux、BSD、Mac OS X、Solaris,以及Microsoft Windows等操做系統中運行。
總體採用模塊化設計是nginx的一個重大特色,甚至http服務器核心功能也是一個模塊。要注意的是:nginx的模塊是靜態的,添加和刪除模塊都要對nginx進行從新編譯,這一點與Apache的動態模塊徹底不一樣。
瞭解Tengine:Tengine - 維基百科,自由的百科全書(一個由淘寶從nginx復刻出來的HTTP服務器)
自PHP-5.3.3起,PHP-FPM
加入到了PHP核心,編譯時加上--enable-fpm
便可提供支持。 PHP-FPM以守護進程在後臺運行,Nginx響應請求後,自行處理靜態請求,PHP請求則通過fastcgi_pass
交由PHP-FPM
處理,處理完畢後返回。 Nginx和PHP-FPM的組合,是一種穩定、高效的PHP運行方式
命令及提示摘要:
sudo apt-get install nginx 下列【新】軟件包將被安裝: nginx nginx-common nginx-full 21:31 root@debian:/home/weaming $ whereis nginx nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx /usr/share/man/man1/nginx.1.gz 21:31 root@debian:/home/weaming $ nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 23:24 root@debian:/ $ nginx -V nginx version: nginx/1.6.2 TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt=-Wl,-z,relro --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/nginx-auth-pam --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/nginx-dav-ext-module --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/nginx-echo --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/nginx-upstream-fair --add-module=/tmp/buildd/nginx-1.6.2/debian/modules/ngx_http_substitutions_filter_module
小結:
nginx nginx-common nginx-full
三個包whereis nginx
查看nginx可執行程序位置/etc/nginx/nginx.conf
。nginx默認配置nginx -t
檢查配置是否有錯誤nginx -V
查看安裝了哪些模塊/etc/nginx/
文件夾下還有(tree /etc/nginx/ -L 2):/etc/nginx/ ├── conf.d ├── fastcgi.conf ├── fastcgi_params ├── koi-utf ├── koi-win ├── mime.types ├── nginx.conf ├── proxy_params ├── scgi_params ├── sites-available │ └── default ├── sites-enabled │ └── default -> /etc/nginx/sites-available/default ├── snippets │ ├── fastcgi-php.conf │ └── snakeoil.conf ├── uwsgi_params └── win-utf 4 directories, 14 files
php自身分爲三個東西,php-cgi
,php-cli
和php-fpm
,cli則是在shell執行php的工具。
經典的功能模塊有php-mysql,php-curl等等。mysql,mysqli,pdo,curl,gd,pear,xmlrpc,imagemagick字樣的都是經典的包,前綴根據環境不一樣,多是php-
,多是php5-
pdo用於取代mysql和mysqli鏈接數據庫,curl用於post,gd用於生成驗證碼,pear和pecl用於安裝擴展,xmlrpc用於通訊,imagemagic用於圖片處理。具體的功能,apt-cache search php
或者yum search php
就能出現一句話的功能簡介。
$ sudo apt-get install php5 php5-fpm php-apc php5-curl php5-cli php-pear php5-gd -y Creating config file /etc/php5/mods-available/pdo.ini with new version php5_invoke: Enable module pdo for cli SAPI php5_invoke: Enable module pdo for fpm SAPI Creating config file /etc/php5/mods-available/opcache.ini with new version php5_invoke: Enable module opcache for cli SAPI php5_invoke: Enable module opcache for fpm SAPI 正在設置 php5-json (1.3.6-1) ... php5_invoke: Enable module json for cli SAPI php5_invoke: Enable module json for fpm SAPI 正在設置 php5-cli (5.6.17+dfsg-0+deb8u1) ... update-alternatives: 使用 /usr/bin/php5 來在自動模式中提供 /usr/bin/php (php) update-alternatives: 使用 /usr/bin/phar5 來在自動模式中提供 /usr/bin/phar (phar) Creating config file /etc/php5/cli/php.ini with new version 正在設置 php-pear (5.6.17+dfsg-0+deb8u1) ... 正在設置 libapparmor1:amd64 (2.9.0-3) ... 正在設置 php5-fpm (5.6.17+dfsg-0+deb8u1) ... Creating config file /etc/php5/fpm/php.ini with new version 正在設置 php5 (5.6.17+dfsg-0+deb8u1) ... 正在設置 php5-apcu (4.0.7-1) ... php5_invoke: Enable module apcu for cli SAPI php5_invoke: Enable module apcu for fpm SAPI 正在設置 php5-curl (5.6.17+dfsg-0+deb8u1) ... Creating config file /etc/php5/mods-available/curl.ini with new version php5_invoke: Enable module curl for cli SAPI php5_invoke: Enable module curl for fpm SAPI 正在設置 php5-gd (5.6.17+dfsg-0+deb8u1) ... Creating config file /etc/php5/mods-available/gd.ini with new version php5_invoke: Enable module gd for cli SAPI php5_invoke: Enable module gd for fpm SAPI 正在設置 php5-readline (5.6.17+dfsg-0+deb8u1) ... Creating config file /etc/php5/mods-available/readline.ini with new version php5_invoke: Enable module readline for cli SAPI php5_invoke: Enable module readline for fpm SAPI 正在設置 php-apc (4.0.7-1) ... 正在處理用於 libc-bin (2.19-18+deb8u1) 的觸發器 ... 正在處理用於 systemd (215-17+deb8u2) 的觸發器 ... 正在處理用於 php5-fpm (5.6.17+dfsg-0+deb8u1) 的觸發器 ...
數據庫
#sudo apt-get php-mysql php5-pgsql php5-sqlite -y
APT 會自動把適當的行添加到不一樣的 php.ini 相關文件中去,例如:
/etc/php5 ├── cli │ ├── conf.d │ └── php.ini ├── fpm │ ├── conf.d │ ├── php-fpm.conf │ ├── php.ini │ └── pool.d └── mods-available ├── apcu.ini ├── curl.ini ├── gd.ini ├── json.ini ├── opcache.ini ├── pdo.ini └── readline.ini
重啓php-fpm:
php5-fpm reload php5-fpm restart
均報錯。像下面這樣作便可:
23:06 root@debian:/etc/php5 $ cat fpm/php-fpm.conf | grep pid pid = /run/php5-fpm.pid 23:07 root@debian:/etc/php5 $ kill -USR2 `cat /run/php5-fpm.pid`
php-cgi是 PHP 的解釋器
,它只是個 CGI 程序,只能解析請求,返回結果,不會進程管理。因此就出現了一些可以調度 php-cgi 進程的程序。
FPM(FastCGI 進程管理器)用於替換 PHP FastCGI 的大部分附加功能,對於高負載網站是很是有用的。
修改了php.ini配置文件後,沒辦法平滑重啓。php-fpm對此的處理機制是新的worker用新的配置,已經存在的worker處理完手上的活就能夠歇着了,經過這種機制來平滑過分。
"PHP解析器(php-cgi)會解析php.ini文件,初始化執行環境",就是這裏了。標準的CGI對每一個請求都會執行這些步驟(不閒累啊!啓動進程很累的說!),因此處理每一個時間的時間會比較長。這明顯不合理嘛!那麼Fastcgi是怎麼作的呢?首先,Fastcgi會先啓一個master,解析配置文件,初始化執行環境,而後再啓動多個worker。當請求過來時,master會傳遞給一個worker,而後當即能夠接受下一個請求。這樣就避免了重複的勞動,效率天然是高。並且當worker不夠用時,master能夠根據配置預先啓動幾個worker等着;固然空閒worker太多時,也會停掉一些,這樣就提升了性能,也節約了資源。這就是fastcgi的對進程的管理。 來源:搞不清FastCgi與PHP-fpm之間是個什麼樣的關係 - SegmentFault
搞笑版:
你(PHP)去和愛斯基摩人(web服務器,如 Apache、Nginx)談生意
你說中文(PHP代碼),他說愛斯基摩語(C代碼),互相聽不懂,怎麼辦?那就都把各自說的話轉換成英語(FastCGI 協議)吧。
怎麼轉換呢?你就要使用一個翻譯機(PHP-FPM)(固然對方也有一個翻譯機,那個是他自帶的)
咱們這個翻譯機是最新型的,老式的那個(PHP-CGI)被淘汰了。不過它(PHP-FPM)只有年輕人(Linux系統)會用,老頭子們(Windows系統)不會擺弄它,只好繼續用老式的那個。