Nginx配置支援php

svoid發表於2015-04-19

Nginx 基本配置與引數說明

#執行使用者
user  nginx;
#啟動程式,通常設定成和cpu的數量相等
worker_processes  4;

#全域性錯誤日誌及PID檔案
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#工作模式及連線數上限
events {
    #epoll是多路複用IO(I/O Multiplexing)中的一種方式,僅用於linux2.6以上核心,可以大大提高nginx的效能
    use   epoll;

    #單個後臺worker process程式的最大併發連結數 
    #併發總數是 worker_processes 和 worker_connections 的乘積
    # 在設定了反向代理的情況下,max_clients = worker_processes * worker_connections / 4
    worker_connections  1024;
}

http {
    include       mime.types;
    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 指令指定 nginx 是否呼叫 sendfile 函式(zero copy 方式)來輸出檔案,
    #對於普通應用,必須設為 on,如果用來進行下載等應用磁碟IO重負載應用,可設定為 off,
    #以平衡磁碟與網路I/O處理速度,降低系統的uptime.
    sendfile        on;
    #tcp_nopush     on;

    #連線超時時間
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #開啟gzip壓縮
    gzip  on;

    #設定虛擬機器
    server {
        listen       80;
        server_name  rac1;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #定義伺服器的預設網站根目錄位置
            root   html;
            #定義首頁索引檔案的名稱
            index  index.html index.htm;
        }

        # 定義錯誤提示頁面
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       # PHP 指令碼請求全部轉發到 FastCGI處理. 使用FastCGI預設配置
       location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }
}

Nginx 配置支援php

Nginx本身不能處理PHP,當接收到請求後,如果是PHP請求,則發給PHP直譯器處理,並把結果返回給客戶端。
Nginx一般是把請求發FastCGI管理程式處理,FastCGI管理程式選擇CGI子程式處理結果並返回被Nginx。
PHP-FPM是一個PHP FastCGI管理器,是隻用於PHP的。PHP-FPM提供了更好的PHP程式管理方式,可以有效控制記憶體和程式、可以平滑過載PHP配置。新版PHP已經整合了PHP-FPM,在./configure的時候帶 -–enable-fpm引數即可開啟PHP-FPM。

1. 安裝php-fpm
shell> cd php-5.5.23
shell> ./configure --prefix=/usr/local/php --enable-fpm --with-mysql --with-apxs2=/usr/local/apache2/bin/apxs
shell> make && make install 

2. 修改php-fpm配置

shell> cd /usr/local/php
shell> cp etc/php-fpm.conf.default etc/php-fpm.conf
shell> vi etc/php-fpm.conf
=================================================================
user = nginx
group = nginx
=================================================================

3. 修改nginx配置檔案以支援php-fpm
shell> vi /usr/local/nginx/nginx.conf
=================================================================
location ~ \.php$ {
    root           /var/web;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
=================================================================

啟動服務並測試

建立測試php檔案
shell> vi /var/web/test.php
=================================================================

 phpinfo();
?>
=================================================================

啟動php-fpm和nginx
shell> /usr/local/php/sbin/php-fpm
shell> /usr/local/nginx/nginx

訪問http://你的伺服器ip/test.php,可以見到php資訊。

參考:
http://www.nginx.cn/76.html
http://www.nginx.cn/231.html
http://www.it165.net/admin/html/201308/1759.html

整理自網路

Svoid
2015-04-18

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29733787/viewspace-1585541/,如需轉載,請註明出處,否則將追究法律責任。

相關文章