欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Debian系統(tǒng)下為PHP程序配置Nginx服務(wù)器的基本教程

 更新時(shí)間:2015年12月21日 14:46:10   作者:China_OS  
這篇文章主要介紹了Debian系統(tǒng)下為PHP程序配置Nginx服務(wù)器的基本教程,這里使用到了FastCGI和php-fpm,需要的朋友可以參考下

  Nginx 目前來(lái)說(shuō)已經(jīng)很流行了,所以打算把現(xiàn)有的Apache先替換一部分,在批量部署和管理方面都要考慮到。因?yàn)閚ginx的所有模塊都是靜態(tài)編譯的不像apache那樣可以動(dòng)態(tài)添加模塊,所以在一開(kāi)始就要考慮清楚需求,結(jié)合現(xiàn)在很火的LAMP模式,其中P最常見(jiàn)的是PHP,目前較好的FastCGI的管理器有spaw-fcgi和php-fpm,而php老版本都是需要源碼安裝通過(guò)打補(bǔ)丁的方式把php-fpm打進(jìn)去,spaw-fcgi是lighttpd默認(rèn)的FastCGI管理器,因?yàn)闋砍兜皆创a安裝,當(dāng)前的軟件包版本在大規(guī)模部署起來(lái)不是很方便,但是還是有解決辦法的。

    Nginx安裝方法:

    1 apt網(wǎng)絡(luò)安裝

       修改源列表

vi /etc/apt/sources.list
#加入以下源
deb http://nginx.org/packages/debian/ squeeze nginx
deb-src http://nginx.org/packages/debian/ squeeze nginx

        更新源列表并通過(guò)apt安裝nginx

apt-key add nginx_signing.key
apt-get update
apt-get install nginx

    2 源碼編譯安裝
       先安裝編譯環(huán)境, 由于nginx在以后的使用中會(huì)需要用到perl正則、壓縮算法、SSL等特性,所以我們需要提前安裝相關(guān)庫(kù)文件。

apt-get install build-essential 
apt-get install libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev libssl0.9.8

        下載最新的穩(wěn)定版nginx

wget http://nginx.org/download/nginx-1.2.3.tar.gz
        解壓查看編譯選項(xiàng)

tar zxvf nginx-1.2.3.tar.gz
cd nginx-1.2.3
#--help可以看到可以配置的參數(shù)
./configure --help

       查看編譯可選的配置參數(shù)(以下只是一些常用的配置項(xiàng)):

     
--prefix=PATH                      nginx的默認(rèn)安裝路徑,沒(méi)有指定的話(huà)。默認(rèn)為/usr/local/nginx

--sbin-path=PATH                nginx可執(zhí)行命令文件的路徑,沒(méi)有指定的話(huà),默認(rèn)<prefix>/sbin/nginx

--conf-path=PATH               nginx配置文件路徑,沒(méi)有指定的話(huà),默認(rèn)為<prefix>/conf/nginx.conf

--error-log-path=PATH        在nginx.conf中沒(méi)有使用error_log指定錯(cuò)誤日志路徑時(shí),默認(rèn)<prefix>/logs/error.log

--http-log-path=PATH         定義被訪問(wèn)文件的日志存放路徑,如果在nginx.conf中沒(méi)有使用access_log指定,默認(rèn)為<prefix>/logs/access.log

--pid-path=PATH                    當(dāng)在nginx.conf中沒(méi)有使用pid指定pid文件路徑時(shí),默認(rèn)為<prefix>/logs/nginx.pid

--lock-path=PATH                   鎖文件存放路由,如果在nginx.conf中沒(méi)有指定,則默認(rèn)為<prefix>/logs/nginx.lock

--user=USER                        如果在Nginx.conf中沒(méi)有使用user定義進(jìn)程運(yùn)行的屬主,則默認(rèn)為nobody

--group=GROUP                       如果在Nginx.conf中沒(méi)有使用user定義進(jìn)程運(yùn)行的屬組,則默認(rèn)為nobody

--builddir=DIR                     set build directory

--with-rtsig_module                使用rtsig模式

--with-select_module               使用select模式,如果當(dāng)前平臺(tái)沒(méi)有其他有效模式,則默認(rèn)編譯

--without-select_module            禁止select模式

--with-poll_module                 使用poll模式,如果當(dāng)前平臺(tái)沒(méi)有其他有效模式,則默認(rèn)編譯

--without-poll_module              禁止poll模式

--with-file-aio                    enable file AIO support

--with-ipv6                        開(kāi)啟IPV6支持

--with-debug                       開(kāi)啟debug

以下這些模塊默認(rèn)沒(méi)有開(kāi)啟,可以使用--with命令開(kāi)啟以下模塊

  --with-http_ssl_module             開(kāi)啟ssl模塊

  --with-http_realip_module          可以在后端記錄客戶(hù)端IP

  --with-http_addition_module        enable ngx_http_addition_module

  --with-http_flv_module             開(kāi)啟flv模塊

  --with-http_mp4_module             開(kāi)啟mp4模塊

  --with-http_gzip_static_module     開(kāi)啟gzip模塊

  --with-http_secure_link_module     enable ngx_http_secure_link_module

  --with-http_stub_status_module     開(kāi)啟狀態(tài)查看模塊

 

  以下模塊默認(rèn)是開(kāi)啟的,可以使用--without選項(xiàng)關(guān)閉相關(guān)模塊

  --without-http_charset_module      disable ngx_http_charset_module

  --without-http_gzip_module         禁用gzip壓縮模塊

  --without-http_ssi_module          禁用ssl模塊

  --without-http_userid_module       disable ngx_http_userid_module

  --without-http_access_module       禁用access模塊

  --without-http_auth_basic_module   禁用auth_basic認(rèn)證模塊

  --without-http_autoindex_module    禁用列目錄模塊

  --without-http_geo_module          禁用geo模塊

  --without-http_map_module          禁用map模塊

  --without-http_referer_module      disable ngx_http_referer_module

  --without-http_rewrite_module      禁用重定向模塊

  --without-http_proxy_module        禁用代理模塊

  --without-http_fastcgi_module      禁用fastcgi模塊

  --without-http_uwsgi_module        disable ngx_http_uwsgi_module

  --without-http_scgi_module         disable ngx_http_scgi_module

  --without-http_memcached_module    disable ngx_http_memcached_module

  --without-http_limit_conn_module   disable ngx_http_limit_conn_module

  --without-http_limit_req_module    disable ngx_http_limit_req_module

  --without-http_empty_gif_module    disable ngx_http_empty_gif_module

  --without-http_browser_module      disable ngx_http_browser_module

  --without-http_upstream_ip_hash_module    禁用upstream模塊

 --with-http_perl_module            開(kāi)啟perl模塊
  --with-perl_modules_path=PATH      設(shè)置perl模塊路徑
  --with-perl=PATH                   為perl庫(kù)設(shè)置路徑
  --http-client-body-temp-path=PATH  set path to store http client request body temporary files
  --http-proxy-temp-path=PATH        set path to store http proxy temporary files
  --http-fastcgi-temp-path=PATH      set path to store http fastcgi temporary files
  --http-uwsgi-temp-path=PATH        set path to store http uwsgi temporary files
  --http-scgi-temp-path=PATH         set path to store http scgi temporary files
  --without-http                     禁用http服務(wù)
  --without-http-cache               禁用http cache
  --with-mail                       開(kāi)啟mail服務(wù)
  --with-mail_ssl_module             在mail服務(wù)中開(kāi)啟ssl
  --without-mail_pop3_module         disable ngx_mail_pop3_module
  --without-mail_imap_module         disable ngx_mail_imap_module
  --without-mail_smtp_module         disable ngx_mail_smtp_module
  --with-google_perftools_module     開(kāi)啟google_perftools模塊
  --with-cpp_test_module             開(kāi)啟cpp_test模塊
  --add-module=PATH                  enable an external module
  --without-pcre                     禁止使用perl正則庫(kù)
  --with-pcre                        強(qiáng)制使用perl正則庫(kù)

     編譯安裝

./configure --prefix=/etc/nginx/ --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module 
--with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module 
--with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-file-aio --with-ipv6

    結(jié)果如下圖:

20151221143837331.png (622×381)

  編譯并安裝

make && make install

     PS:FastCGI回顧
     FastCGI是一個(gè)可伸縮地、高速地在HTTP server和動(dòng)態(tài)腳本語(yǔ)言間通信的接口。多數(shù)流行的HTTP server都支持FastCGI,包括Apache、Nginx和lighttpd等,同時(shí),F(xiàn)astCGI也被許多腳本語(yǔ)言所支持,其中就有PHP。FastCGI是從CGI發(fā)展改進(jìn)而來(lái)的。傳統(tǒng)CGI接口方式的主要缺點(diǎn)是性能很差,因?yàn)槊看蜨TTP服務(wù)器遇到動(dòng)態(tài)程序時(shí)都需要重新啟動(dòng)腳本解析器來(lái)執(zhí)行解析,然后結(jié)果被返回給HTTP服務(wù)器。這在處理高并發(fā)訪問(wèn)時(shí),幾乎是不可用的。另外傳統(tǒng)的CGI接口方式安全性也很差,現(xiàn)在已經(jīng)很少被使用了。FastCGI接口方式采用C/S結(jié)構(gòu),可以將HTTP服務(wù)器和腳本解析服務(wù)器分開(kāi),同時(shí)在腳本解析服務(wù)器上啟動(dòng)一個(gè)或者多個(gè)腳本解析守護(hù)進(jìn)程。當(dāng)HTTP服務(wù)器每次遇到動(dòng)態(tài)程序時(shí),可以將其直接交付給FastCGI進(jìn)程來(lái)執(zhí)行,然后將得到的結(jié)果返回給瀏覽器。這種方式可以讓HTTP服務(wù)器專(zhuān)一地處理靜態(tài)請(qǐng)求或者將動(dòng)態(tài)腳本服務(wù)器的結(jié)果返回給客戶(hù)端,這在很大程度上提高了整個(gè)應(yīng)用系統(tǒng)的性能。
    Nginx不支持對(duì)外部程序的直接解析,所有的外部程序(包括PHP)必須通過(guò)FastCGI接口來(lái)調(diào)用。FastCGI接口在Linux下是socket,(這個(gè)socket可以是文件socket,也可以是ip socket)。為了調(diào)用CGI程序,還需要一個(gè)FastCGI的wrapper(wrapper可以理解為用于啟動(dòng)另一個(gè)程序的程序),這個(gè)wrapper綁定在某個(gè)固定socket上,如端口或者文件socket。當(dāng)Nginx將CGI請(qǐng)求發(fā)送給這個(gè)socket的時(shí)候,通過(guò)FastCGI接口,wrapper接納到請(qǐng)求,然后派生出一個(gè)新的線程,這個(gè)線程調(diào)用解釋器或者外部程序處理腳本并讀取返回?cái)?shù)據(jù);接著,wrapper再將返回的數(shù)據(jù)通過(guò)FastCGI接口,沿著固定的socket傳遞給Nginx;最后,Nginx將返回的數(shù)據(jù)發(fā)送給客戶(hù)端,這就是Nginx+FastCGI的整個(gè)運(yùn)作過(guò)程。

20151221144039021.png (569×242)

php-fpm

PHP5.3版本源碼已經(jīng)默認(rèn)支持php-fpm了,但是debian6認(rèn)為它還沒(méi)經(jīng)過(guò)廣泛的測(cè)試,所以在debian6的軟件倉(cāng)庫(kù)中,雖然php版本為5.3.3,但是卻沒(méi)包含php-fpm,如果不想手工編譯安裝php的話(huà)可以換一個(gè)源。
    
     修改源列表

vi /etc/apt/sources.list
deb http://packages.dotdeb.org stable all
deb-src http://packages.dotdeb.org stable all

    更新源列表,安裝php5-fpm

apt-get update wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | apt-key add -
apt-get install php5-fpm

    安裝其他常用php5組件

apt-get install php5 php5-cgi php5-cli php5-mysql php5-memcache

    啟動(dòng)php-fpm

/etc/init.d/php5-fpm start

    這樣的話(huà)最基本的nginx+php環(huán)境就搭建完畢了。   

Nginx配置文件粗解
nginx的配置文件結(jié)構(gòu)類(lèi)似下圖這樣的結(jié)構(gòu):

20151221144142626.png (655×323)

配置文件主要參照編譯完成后生成的默認(rèn)配置文件。

   主模塊的配置選項(xiàng)

    user指令設(shè)置進(jìn)程以什么用戶(hù)運(yùn)行,在源碼編譯安裝時(shí)指定的nginx用戶(hù),如果在編譯時(shí)沒(méi)有指定,默認(rèn)是nobody賬戶(hù),在配置文件中此行處于注釋狀態(tài),  user 指令可以設(shè)置兩個(gè)參數(shù),第一個(gè)指定進(jìn)程所屬用戶(hù),第二個(gè)是可選,指定進(jìn)程所屬組

  user nginx nobody;

    設(shè)置工作進(jìn)程數(shù),一個(gè)工作進(jìn)程為一個(gè)單線程,在CPU密集型環(huán)境中,可以設(shè)置worker_processes數(shù)目為CPU核數(shù)

  worker_processes 4;

    指定nginx錯(cuò)誤日志文件的位置,如果要禁止錯(cuò)誤日志使用error_log /dev/null,error_log可以存在于不同的字段main、http、server等,文件后面可以指定記錄的日志的默認(rèn)等級(jí)。

  error_log logs/error.log;

    設(shè)置pid文件路徑,可以使用kill命令發(fā)送相關(guān)信號(hào)

  pid    logs/nginx.pid;

    event模塊配置選項(xiàng),event模塊主要控制nginx處理連接的方式

  events {

  #如果在configure時(shí)指定的不止一個(gè)事件模型,可以通過(guò)use告訴nginx要使用哪一個(gè)模型:seletc、poll、kqueue、epoll、rtsig、/dev/poll、eventport等

  use epoll;

  #worker_connections和worker_processes可以計(jì)算你的理論最大鏈接數(shù), worker_connections*worker_processes

  worker_connections 1024;

}

    http模塊里面主要是對(duì)http服務(wù)器相關(guān)屬性進(jìn)行設(shè)置

  

 http {

  #可以用include指令包含一些其他文件,支持通配符,可以使用絕對(duì)路徑,也可以使用相對(duì)路徑,相對(duì)路徑以nginx.conf為根據(jù)

  include    mime.types;

  #設(shè)置默認(rèn)的MIME類(lèi)型

  default_type application/octet-stream;

  #描述記錄日志的格式,在定義格式時(shí)可以使用一些變量,各個(gè)變了的含義可以從各個(gè)模塊匯總查找

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '

           '$status $body_bytes_sent "$http_referer" '

           '"$http_user_agent" "$http_x_forwarded_for"';

  #指定訪問(wèn)日志的路徑和格式,緩沖區(qū)大小

  access_log logs/access.log main;

  #sendfile拷貝文件在內(nèi)核態(tài)完成,更加高效

  sendfile    on;

  #tcp_nopush   on;

  #可以設(shè)置兩個(gè)值,第一個(gè)表示客戶(hù)端與服務(wù)器長(zhǎng)連接的超時(shí)時(shí)間,超過(guò)這個(gè)時(shí)間,服務(wù)器將關(guān)閉連接。第二個(gè)值指定的應(yīng)答頭中keep-alive中timeout的值,讓瀏覽器知道什么時(shí)候關(guān)閉連接。

  keepalive_timeout 65;

  #開(kāi)啟gzip壓縮

  gzip on;

  #在三次握手時(shí),發(fā)送給客戶(hù)端應(yīng)答后的超時(shí)時(shí)間,目前還沒(méi)進(jìn)入連接狀態(tài),只完成了兩次握手,如果在規(guī)定時(shí)間沒(méi)收到應(yīng)答包,nginx將關(guān)閉鏈接

  send_timeout 30

  server {

     xxx

  }

}

    server模塊嵌在http模塊中,主要用來(lái)配置虛擬主機(jī)

  server {

    #指定server字段中可以被訪問(wèn)到的ip地址及端口

    listen    80;

    #將http請(qǐng)求的主機(jī)頭與server中的server_name參數(shù)進(jìn)行匹配,并找出第一個(gè)結(jié)果,如果沒(méi)有server_name參數(shù)匹配上,則第一個(gè)出現(xiàn)listen的server將被匹配,多域名用空格分割

    server_name www.nginx.com;

    #設(shè)個(gè)指令是應(yīng)答頭重的content-type字段使用指定的編碼集,off表示不在應(yīng)答頭重添加content-type信息

    charset off;

    #指定www.nginx.com域名的訪問(wèn)日志路徑及格式

    access_log logs/host.access.log main;

    #如果在url中沒(méi)有指定文件,則設(shè)置一個(gè)默認(rèn)主頁(yè),可以設(shè)置多個(gè)文件,空格分開(kāi),可以在http、server、location中設(shè)置

    index index.php index.htm;

    #根據(jù)URL的不同需求進(jìn)行配置,可以使用字符串和正則匹配,最確切的匹配被使用,搜索到第一個(gè)后會(huì)停止

    # ~* 不區(qū)分大小寫(xiě);~ 區(qū)分大小寫(xiě);^~ 禁止在字符串匹配后檢查正則;= 在URL和location之間精確匹配,匹配完成后不做額外搜索。

    location /i/ {

      #請(qǐng)求到達(dá)后的文件根目錄,在請(qǐng)求中root會(huì)把location匹配的值加到root指定的值后面,請(qǐng)求/i/a.php,則會(huì)是/html/i/a.php響應(yīng)

      root  html;

      #在location中設(shè)置index

      index index.html index.htm;

    }

    #為錯(cuò)誤代碼指定相應(yīng)的錯(cuò)誤界面,可以用在http、server、location字段中。

    error_page 404       /404.html;

    # redirect server error pages to the static page /50x.html

    error_page  500 502 503 504 /50x.html;

    #精確匹配50x.html,真實(shí)響應(yīng)是/html/50x.html

    location = /50x.html {

      root  html;

    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80

    location ~ \.php$ {

      proxy_pass  http://127.0.0.1;

    }

    #配置php腳本傳至fastcgi

    location ~ \.php$ {

     root      html;

     fastcgi_pass  127.0.0.1:9000;

     fastcgi_index index.php;

     #/scripts是php腳本所在的目錄

      fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

      include    fastcgi_params;

    }

    #拒絕訪問(wèn).htaccess文件

    location ~ /\.ht {

      deny all;

    }

  }

相關(guān)文章

最新評(píng)論