共計 8594 個字符,預計需要花費 22 分鐘才能閱讀完成。
這篇文章主要介紹了 Debian 系統下如何為 PHP 程序配置 Nginx 服務器的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇 Debian 系統下如何為 PHP 程序配置 Nginx 服務器文章都會有所收獲,下面我們一起來看看吧。
nginx 安裝方法:
1 apt 網絡安裝
修改源列表
vi /etc/apt/sources.list
#加入以下源
deb http://nginx.org/packages/debian/ squeeze nginx
deb-src http://nginx.org/packages/debian/ squeeze nginx
更新源列表并通過 apt 安裝 nginx
apt-key add nginx_signing.key
apt-get update
apt-get install nginx
2 源碼編譯安裝
先安裝編譯環境,由于 nginx 在以后的使用中會需要用到 perl 正則、壓縮算法、ssl 等特性,所以我們需要提前安裝相關庫文件。
apt-get install build-essential
apt-get install libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev libssl0.9.8
下載最新的穩定版 nginx
解壓查看編譯選項
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
ps: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 的整個運作過程。
php-fpm
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 環境就搭建完畢了。
nginx 配置文件粗解
nginx 的配置文件結構類似下圖這樣的結構:
配置文件主要參照編譯完成后生成的默認配置文件。
主模塊的配置選項
user 指令設置進程以什么用戶運行,在源碼編譯安裝時指定的 nginx 用戶,如果在編譯時沒有指定,默認是 nobody 賬戶,在配置文件中此行處于注釋狀態, user 指令可以設置兩個參數,第一個指定進程所屬用戶,第二個是可選,指定進程所屬組
user nginx nobody;
設置工作進程數,一個工作進程為一個單線程,在 cpu 密集型環境中,可以設置 worker_processes 數目為 cpu 核數
worker_processes 4;
指定 nginx 錯誤日志文件的位置,如果要禁止錯誤日志使用 error_log /dev/null,error_log 可以存在于不同的字段 main、http、server 等,文件后面可以指定記錄的日志的默認等級。
error_log logs/error.log;
設置 pid 文件路徑,可以使用 kill 命令發送相關信號
pid logs/nginx.pid;
event 模塊配置選項,event 模塊主要控制 nginx 處理連接的方式
events {
# 如果在 configure 時指定的不止一個事件模型,可以通過 use 告訴 nginx 要使用哪一個模型:seletc、poll、kqueue、epoll、rtsig、/dev/poll、eventport 等
use epoll;
#worker_connections 和 worker_processes 可以計算你的理論最大鏈接數, worker_connections*worker_processes
worker_connections 1024;
}
http 模塊里面主要是對 http 服務器相關屬性進行設置
http {
# 可以用 include 指令包含一些其他文件,支持通配符,可以使用絕對路徑,也可以使用相對路徑,相對路徑以 nginx.conf 為根據
include mime.types;
# 設置默認的 mime 類型
default_type application/octet-stream;
# 描述記錄日志的格式,在定義格式時可以使用一些變量,各個變了的含義可以從各個模塊匯總查找
log_format main $remote_addr - $remote_user [$time_local] $request
$status $body_bytes_sent $http_referer
$http_user_agent $http_x_forwarded_for
# 指定訪問日志的路徑和格式,緩沖區大小
access_log logs/access.log main;
#sendfile 拷貝文件在內核態完成,更加高效
sendfile on;
#tcp_nopush on;
# 可以設置兩個值,第一個表示客戶端與服務器長連接的超時時間,超過這個時間,服務器將關閉連接。第二個值指定的應答頭中 keep-alive 中 timeout 的值,讓瀏覽器知道什么時候關閉連接。 keepalive_timeout 65;
# 開啟 gzip 壓縮
gzip on;
# 在三次握手時,發送給客戶端應答后的超時時間,目前還沒進入連接狀態,只完成了兩次握手,如果在規定時間沒收到應答包,nginx 將關閉鏈接
send_timeout 30
server {
xxx
}
}
server 模塊嵌在 http 模塊中,主要用來配置虛擬主機
server {
# 指定 server 字段中可以被訪問到的 ip 地址及端口
listen 80;
# 將 http 請求的主機頭與 server 中的 server_name 參數進行匹配,并找出第一個結果,如果沒有 server_name 參數匹配上,則第一個出現 listen 的 server 將被匹配,多域名用空格分割
server_name www.nginx.com;
# 設個指令是應答頭重的 content-type 字段使用指定的編碼集,off 表示不在應答頭重添加 content-type 信息
charset off;
# 指定 www.nginx.com 域名的訪問日志路徑及格式
access_log logs/host.access.log main;
# 如果在 url 中沒有指定文件,則設置一個默認主頁,可以設置多個文件,空格分開,可以在 http、server、location 中設置
index index.php index.htm;
# 根據 url 的不同需求進行配置,可以使用字符串和正則匹配,最確切的匹配被使用,搜索到第一個后會停止
# ~* 不區分大小寫;~ 區分大小寫;^~ 禁止在字符串匹配后檢查正則;= 在 url 和 location 之間精確匹配,匹配完成后不做額外搜索。 location /i/ {
# 請求到達后的文件根目錄,在請求中 root 會把 location 匹配的值加到 root 指定的值后面,請求 /i/a.php,則會是 /html/i/a.php 響應
root html;
# 在 location 中設置 index
index index.html index.htm;
}
# 為錯誤代碼指定相應的錯誤界面,可以用在 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,真實響應是 /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;
}
# 拒絕訪問.htaccess 文件
location ~ /\.ht {
deny all;
}
}
關于“Debian 系統下如何為 PHP 程序配置 Nginx 服務器”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Debian 系統下如何為 PHP 程序配置 Nginx 服務器”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道。