Nginx+PHP安裝(Debian6)

 

     Nginx 目前來講已經很流行了,因此打算把現有的Apache先替換一部分,在批量部署和管理方面都要考慮到。由於nginx的全部模塊都是靜態編譯的不像apache那樣能夠動態添加模塊,因此在一開始就要考慮清楚需求,結合如今很火的LAMP模式,其中P最多見的是PHP,目前較好的FastCGI的管理器有spaw-fcgi和php-fpm,而php老版本都是須要源碼安裝經過打補丁的方式把php-fpm打進去,spaw-fcgi是lighttpd默認的FastCGI管理器,由於牽扯到源碼安裝,當前的軟件包版本在大規模部署起來不是很方便,可是仍是有解決辦法的。 php

    Nginx安裝方法: mysql

    1 apt網絡安裝 nginx

       修改源列表 算法

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

        更新源列表並經過apt安裝nginx sql

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

    2 源碼編譯安裝

       先安裝編譯環境, 因爲nginx在之後的使用中會須要用到perl正則、壓縮算法、SSL等特性,因此咱們須要提早安裝相關庫文件。 shell

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

        下載最新的穩定版nginx apache

wget http://nginx.org/download/nginx-1.2.3.tar.gz

        解壓查看編譯選項 後端

tar zxvf nginx-1.2.3.tar.gz
cd nginx-1.2.3
#--help能夠看到能夠配置的參數
./configure --help

       查看編譯可選的配置參數(如下只是一些經常使用的配置項): 瀏覽器

     

--prefix=PATH                      nginx的默認安裝路徑,沒有指定的話。默認爲/usr/local/nginx 安全

--sbin-path=PATH                nginx可執行命令文件的路徑,沒有指定的話,默認<prefix>/sbin/nginx

--conf-path=PATH               nginx配置文件路徑,沒有指定的話,默認爲<prefix>/conf/nginx.conf

--error-log-path=PATH        在nginx.conf中沒有使用error_log指定錯誤日誌路徑時,默認<prefix>/logs/error.log

--http-log-path=PATH         定義被訪問文件的日誌存放路徑,若是在nginx.conf中沒有使用access_log指定,默認爲<prefix>/logs/access.log 

--pid-path=PATH                    當在nginx.conf中沒有使用pid指定pid文件路徑時,默認爲<prefix>/logs/nginx.pid

--lock-path=PATH                   鎖文件存放路由,若是在nginx.conf中沒有指定,則默認爲<prefix>/logs/nginx.lock

--user=USER                        若是在Nginx.conf中沒有使用user定義進程運行的屬主,則默認爲nobody

--group=GROUP                       若是在Nginx.conf中沒有使用user定義進程運行的屬組,則默認爲nobody 

--builddir=DIR                     set build directory

--with-rtsig_module                使用rtsig模式

--with-select_module               使用select模式,若是當前平臺沒有其餘有效模式,則默認編譯

--without-select_module            禁止select模式

--with-poll_module                 使用poll模式,若是當前平臺沒有其餘有效模式,則默認編譯

--without-poll_module              禁止poll模式

--with-file-aio                    enable file AIO support

--with-ipv6                        開啓IPV6支持

--with-debug                       開啓debug

如下這些模塊默認沒有開啓,可使用--with命令開啓如下模塊

  --with-http_ssl_module             開啓ssl模塊

  --with-http_realip_module          能夠在後端記錄客戶端IP

  --with-http_addition_module        enable ngx_http_addition_module

  --with-http_flv_module             開啓flv模塊

  --with-http_mp4_module             開啓mp4模塊

  --with-http_gzip_static_module     開啓gzip模塊

  --with-http_secure_link_module     enable ngx_http_secure_link_module

  --with-http_stub_status_module     開啓狀態查看模塊

 

  如下模塊默認是開啓的,可使用--without選項關閉相關模塊

  --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認證模塊

  --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            開啓perl模塊
  --with-perl_modules_path=PATH      設置perl模塊路徑
  --with-perl=PATH                   爲perl庫設置路徑
  --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服務
  --without-http-cache               禁用http cache
  --with-mail                       開啓mail服務
  --with-mail_ssl_module             在mail服務中開啓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     開啓google_perftools模塊
  --with-cpp_test_module             開啓cpp_test模塊
  --add-module=PATH                  enable an external module
  --without-pcre                     禁止使用perl正則庫
  --with-pcre                        強制使用perl正則庫

     編譯安裝
./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
    結果以下圖:

     編譯並安裝
make && make install
     FastCGI回顧
     FastCGI是一個可伸縮地、高速地在HTTP server和動態腳本語言間通訊的接口。多數流行的HTTP server都支持FastCGI,包括Apache、Nginx和lighttpd等,同時,FastCGI也被許多腳本語言所支持,其中就有PHP。FastCGI是從CGI發展改進而來的。傳統CGI接口方式的主要缺點是性能不好,由於每次HTTP服務器遇到動態程序時都須要從新啓動腳本解析器來執行解析,而後結果被返回給HTTP服務器。這在處理高併發訪問時,幾乎是不可用的。另外傳統的CGI接口方式安全性也不好,如今已經不多被使用了。FastCGI接口方式採用C/S結構,能夠將HTTP服務器和腳本解析服務器分開,同時在腳本解析服務器上啓動一個或者多個腳本解析守護進程。當HTTP服務器每次遇到動態程序時,能夠將其直接交付給FastCGI進程來執行,而後將獲得的結果返回給瀏覽器。這種方式可讓HTTP服務器專注地處理靜態請求或者將動態腳本服務器的結果返回給客戶端,這在很大程度上提升了整個應用系統的性能。
    Nginx不支持對外部程序的直接解析,全部的外部程序(包括PHP)必須經過FastCGI接口來調用。FastCGI接口在Linux下是socket,(這個socket能夠是文件socket,也能夠是ip socket)。爲了調用CGI程序,還須要一個FastCGI的wrapper(wrapper能夠理解爲用於啓動另外一個程序的程序),這個wrapper綁定在某個固定socket上,如端口或者文件socket。當Nginx將CGI請求發送給這個socket的時候,經過FastCGI接口,wrapper接納到請求,而後派生出一個新的線程,這個線程調用解釋器或者外部程序處理腳本並讀取返回數據;接着,wrapper再將返回的數據經過FastCGI接口,沿着固定的socket傳遞給Nginx;最後,Nginx將返回的數據發送給客戶端,這就是Nginx+FastCGI的整個運做過程。
      

    PHP5.3版本源碼已經默認支持php-fpm了,可是debian6認爲它還沒通過普遍的測試,因此在debian6的軟件倉庫中,雖然php版本爲5.3.3,可是卻沒包含php-fpm,若是不想手工編譯安裝php的話能夠換一個源。
     
     修改源列表
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
    啓動php-fpm
/etc/init.d/php5-fpm start

    這樣的話最基本的nginx+php環境就搭建完畢了。    
相關文章
相關標籤/搜索