ubuntu10.04配置 nginx+php-fpm模式的詳解_php執行個體

來源:互聯網
上載者:User

ppa安裝php-fpm
安裝工具包

複製代碼 代碼如下:

$ sudo apt-get install python-software-properties  

添加ppa源
複製代碼 代碼如下:

$ sudo add-apt-repository ppa:yola/php5

安裝php5-fpm
複製代碼 代碼如下:

sudo  apt-get  update
sudo  apt-get install  php5-fpm

其它必要的軟體安裝接
複製代碼 代碼如下:

sudo   apt-get   install   nginx

配置php-fpm
php-fpm的解析器是C/S結構,它的設定檔位於:
(1)/etc/php5/fpm/php-fpm.conf
(2)/etc/php5/fpm/pool.d/
一般沒什麼嚴格的配置的要求,或者說這塊我還沒有具體的研究每個配置參數的意義
我採用了tcp模式與fastcgi進程進行串連,因此我修改了tcp監聽的地址和連接埠,修改了一下監視目錄的名稱,這裡不做具體詳細解釋了,大家可以參考官方文檔根據自己的需求進行配置
重啟php5-fpm



配置nginx
前言
nginx本身並不會對php語言進行解析,這個區別於apache(apache有在帶的mod_php模組進行php解析).nginx是通過fastcgi將用戶端的php請求交給背景php5-fpm進程管理器,php5-fpm具有解析php的功能
nginx的主設定檔
檔案位置:/etc/nginx/nginx.conf,我的配置參數如下:
複製代碼 代碼如下:

user  www-data;
#主動開啟cpu多核功能
worker_processes  2;
worker_cpu_affinity 01 10;
#指定nginx進程可以開啟的最大檔案描述符數量
worker_rlimit_nofile 65535;
pid /var/run/nginx.pid;
events {
  #使用epoll的I/O模型
 use epoll;
 #工作單進程的並發串連數,總體並發串連數 = worker_connections * worker_processes
 worker_connections 2048;
 #multi_accept在Nginx接到一個新串連通知後調用accept()來接受盡量多的串連
 multi_accept on;
}
http {
 include       /etc/nginx/mime.types;
 default_type  application/octet-stream;
 charset utf-8;

 server_names_hash_bucket_size 128;
 client_header_buffer_size 2k;
 large_client_header_buffers 4 4k;
 #通過nginx上傳檔案的大小
 client_max_body_size 8m;

#$remote_addr:記錄ip地址;$remote_user:記錄遠程用戶端使用者名稱稱;$request:請求的url和http協議;$status:用於記錄請求狀態;$body_bytes_sent:用於記錄發送給用戶端檔案主體內容的大小;$http_referer:跳轉連結;$http_x_forwarded_for:客戶的真實ip地址
 log_format  main  '$server_name$remote_addr$remote_user[$time_local]"$request"'
                      '$status$body_bytes_sent"$http_referer"'
                      '"$http_user_agent""$http_x_forwarded_for"';
 access_log /var/log/nginx/access.log  main;
 error_log /var/log/nginx/error.log;

 sendfile        on;
 tcp_nopush     on;
 #keepalive的逾時時間
 keepalive_timeout  60;
 open_file_cache max=204800 inactive=20s;
 open_file_cache_min_uses 1;
 open_file_cache_valid 30s;
     tcp_nodelay on;
     gzip  on;
     include /etc/nginx/conf.d/*.conf;
}

日誌格式之間是用不可列印符號進行分隔的,ctrl+v && ctrl+a
nginx虛擬機器主機設定檔
複製代碼 代碼如下:

upstream haolianxi_php {
 server 127.0.0.1:9444;
}
server {
 listen 192.168.1.137:7777;

 access_log /var/log/nginx/haolianxi/haolianxi.access.log main;
 error_log /var/log/nginx/haolianxi/haolianxi.error.log;
 #通用匹配 
 location / {
  root /srv/www/php/;
  autoindex on;
  autoindex_exact_size off;
  autoindex_localtime on;
  access_log /var/log/nginx/haolianxi/location.default.access.log main; 
  error_log /var/log/nginx/haolianxi/location.default.error.log;
  allow 192.168.1.0/24;
  deny all;
 }
 #Regex匹配 
 #proxy the php scripts to php-fpm
 location ~ \.php$ {
  root /srv/www/php/;
  include /etc/nginx/fastcgi_params;
  fastcgi_pass haolianxi_php; # The upstream determined above
  fastcgi_index index.php;
 }
 #php-fpm status monitor
 location = /phpfpm_status {
  fastcgi_pass 127.0.0.1:9444;
  fastcgi_index index.php;
  include /etc/nginx/fastcgi_params;
  allow 192.168.1.127;
  allow 127.0.0.1;
  deny all;
 }
 ## Compression
 # src: http://www.ruby-forum.com/topic/141251
 # src: http://wiki.brightbox.co.uk/docs:nginx
     gzip on;
     gzip_http_version 1.0;
     gzip_comp_level 2;
     gzip_proxied any;
     gzip_min_length  1100;
     gzip_buffers 16 8k;
     gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

     # Some version of IE 6 don't handle compression well on some mime-types, so just disable for them
     gzip_disable "MSIE [1-6].(?!.*SV1)";

     # Set a vary header so downstream proxies don't send cached gzipped content to IE6
     gzip_vary on;
     ## /Compression
}

注意:
include /etc/nginx/fastcgi_params中一個參數設定需要修改,修改如下:
複製代碼 代碼如下:

fastcgi_param   SCRIPT_NAME             $document_root$fastcgi_script_name;

因為指令碼的名稱不加上$document_root,php5-fpm是無法找到需要執行的php指令碼的絕對路徑的
重啟nginx
複製代碼 代碼如下:

sudo  /etc/init.d/nginx  restart

測試fastcgi_finish_request()函數
複製代碼 代碼如下:

<?php
echo "OK";
fastcgi_finish_request(); /* 響應完成, 關閉串連 */
sleep(5);
file_put_contents("/tmp/fastcgi.log", "hello",FILE_APPEND);
sleep(5);
file_put_contents("/tmp/fastcgi.log", "world",FILE_APPEND);
?>

說明:
用最大的白話說,fastcgi_finish_request()可以提前關閉和用戶端的串連,把需要返回的資料返回給用戶端,但是函數之後的分支商務邏輯還是繼續在後台運行!
php5-fpm日誌按天分割指令碼
複製代碼 代碼如下:

#!/bin/bash -
#1.php5-fpm日誌存放路徑
php5_fpm_logs_path="/var/log/php5-fpm/"
category_array=("access" "error")
#2.php5-fpm日誌名尾碼
postfix=`date -d '-1 days' +%Y%m%d`".log"
#3.php5-fpm日誌切割
for category in ${category_array[*]}
do
 if [ -e $php5_fpm_logs_path/php5-fpm.$category.log ]
 then
  mv $php5_fpm_logs_path/php5-fpm.$category.log \
   $php5_fpm_logs_path/php5-fpm.$category.$postfix
 fi
done
#4.尋找php5-fpm進程號,讓其產生新的記錄檔
php5fpm_pid=`ps -aux |grep -E 'php-fpm: master process'|grep -v 'grep'|awk '{print $2}'`
#USR1:Reopen log files,重新整理nginx記錄檔
kill -USR1 $php5fpm_pid

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.