nginx本身不能處理PHP,它只是個web服務器,當接收到請求后,如果是php請求,則發給php解釋器處理,并把結果返回給客戶端。
nginx一般是把請求發fastcgi管理進程處理,fascgi管理進程選擇cgi子進程處理結果并返回被nginx
本文以php-fpm為例介紹如何使nginx支持PHP
一、編譯安裝php-fpm
什么是PHP-FPM
PHP-FPM是一個PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下載得到.
PHP-FPM其實是PHP源代碼的一個補丁,旨在將FastCGI進程管理整合進PHP包中。必須將它patch到你的PHP源代碼中,在編譯安裝PHP后才可以使用。
新版PHP已經集成php-fpm了,不再是第三方的包了,推薦使用。PHP-FPM提供了更好的PHP進程管理方式,可以有效控制內存和進程、可以平滑重載PHP配置,比spawn-fcgi具有更多優點,所以被PHP官方收錄了。在./configure的時候帶 –enable-fpm參數即可開啟PHP-FPM,其它參數都是配置php的,具體選項含義可以查看這里。
安裝前準備
centos下執行
yum -y install gcc automake autoconf libtool makeyum -y install gcc gcc-c++ glibcyum -y install libmcrypt-devel mhash-devel libxslt-devel
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel
新版php-fpm安裝(推薦安裝方式)
wget http://cn2.php.net/distributions/php-5.4.7.tar.gztar zvxf php-5.4.7.tar.gzcd php-5.4.7./configure --prefix=/usr/local/php --enable-fpm --with-mcrypt
--enable-mbstring --disable-pdo --with-curl --disable-debug --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli --with-gd --with-jpeg-dir
make all install
以上兩種方式都可以安裝php-fpm,安裝后內容放在/usr/local/php目錄下
以上就完成了php-fpm的安裝。
下面是對php-fpm運行用戶進行設置
cd /usr/local/phpcp etc/php-fpm.conf.default etc/php-fpm.confvi etc/php-fpm.conf
修改
user = www-datagroup = www-data
如果www-data用戶不存在,那么先添加www-data用戶
groupadd www-datauseradd -g www-data www-data
二、編譯安裝nginx
然后按照http://www.nginx.cn/install 安裝nginx
三、修改nginx配置文件以支持php-fpm
nginx安裝完成后,修改nginx配置文件為,nginx.conf
其中server段增加如下配置,注意標紅內容配置,否則會出現No input file specified.錯誤
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000#location ~ .php$ {root html;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}
四、創建測試php文件
創建php文件
在/usr/local/nginx/html下創建index.php文件,輸入如下內容
<?php echo phpinfo();?>
五、啟動服務
啟動php-fpm和nginx
/usr/local/php/sbin/php-fpm #手動打補丁的啟動方式/usr/local/php/sbin/php-fpm startsudo /usr/local/nginx/nginx
php-fpm關閉重啟見文章結尾
六、瀏覽器訪問
訪問http://你的服務器ip/index.php,皆可以見到php信息了。
七、錯誤解決
在使用Nginx時,經常會碰到502 Bad Gateway和504 Gateway Time-out錯誤,下面以Nginx+PHP-FPM來分析下這兩種常見錯誤的原因和解決方案。
1.502 Bad Gateway錯誤
在php.ini和php-fpm.conf中分別有這樣兩個配置項:max_execution_time和request_terminate_timeout。
這兩項都是用來配置一個PHP腳本的最大執行時間的。當超過這個時間時,PHP-FPM不只會終止腳本的執行,
還會終止執行腳本的Worker進程。所以Nginx會發現與自己通信的連接斷掉了,就會返回給客戶端502錯誤。
以PHP-FPM的request_terminate_timeout=30秒時為例,報502 Bad Gateway錯誤的具體信息如下:
1)Nginx錯誤訪問日志:
2013/09/19 01:09:00 [error] 27600#0: *78887 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.1.101, server: test.com, request: "POST /index.php HTTP/1.1", upstream: "fastcgi://unix:/dev/shm/php-fcgi.sock:", host: "test.com", referrer: "http://test.com/index.php"
2)PHP-FPM報錯日志:
WARNING: child 25708 exited on signal 15 (SIGTERM) after 21008.883410 seconds from start
所以只需將這兩項的值調大一些就可以讓PHP腳本不會因為執行時間長而被終止了。request_terminate_timeout可以覆蓋max_execution_time,
所以如果不想改全局的php.ini,那只改PHP-FPM的配置就可以了。
此外要注意的是Nginx的upstream模塊中的max_fail和fail_timeout兩項。有時Nginx與上游服務器(如Tomcat、FastCGI)的通信只是偶然斷掉了,
但max_fail如果設置的比較小的話,那么在接下來的fail_timeout時間內,Nginx都會認為上游服務器掛掉了,都會返回502錯誤。
所以可以將max_fail調大一些,將fail_timeout調小一些。
2.504 Gateway Time-out錯誤
PHP-FPM設置的腳本最大執行時間已經夠長了,但執行耗時PHP腳本時,發現Nginx報錯從502變為504了。這是為什么呢?
因為我們修改的只是PHP的配置,Nginx中也有關于與上游服務器通信超時時間的配置factcgi_connect/read/send_timeout。
以Nginx超時時間為90秒,PHP-FPM超時時間為300秒為例,報504 Gateway Timeout錯誤時的Nginx錯誤訪問日志如下:
2013/09/19 00:55:51 [error] 27600#0: *78877 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.1.101, server: test.com, request: "POST /index.php HTTP/1.1", upstream: "fastcgi://unix:/dev/shm/php-fcgi.sock:", host: "test.com", referrer: "http://test.com/index.php"
調高這三項的值(主要是read和send兩項,默認不配置的話Nginx會將超時時間設為60秒)之后,504錯誤也解決了。
而且這三項配置可以配置在http、server級別,也可以配置在location級別。擔心影響其他應用的話,就配置在自己應用的location中吧。
要注意的是factcgi_connect/read/send_timeout是對FastCGI生效的,而proxy_connect/read/send_timeout是對proxy_pass生效的。
配置舉例:
location ~ /.php$ { root /home/cdai/test.com; include fastcgi_params; fastcgi_connect_timeout 180; fastcgi_read_timeout 600; fastcgi_send_timeout 600; fastcgi_pass unix:/dev/shm/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/cdai/test.com$fastcgi_script_name; }