1. 程式人生 > >centos系統LNMP環境配置(五)配置nginx支援php

centos系統LNMP環境配置(五)配置nginx支援php

配置nginx
1、修改nginx.conf
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf$(date "+%F")
vim /usr/local/nginx/conf/nginx.conf

配置檔案內容如下:

# 首行user去掉註釋,修改Nginx執行組為www www;
# 必須與/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否則php執行出錯
user  www www;
worker_processes  1;

# 開啟nginx錯誤日誌
error_log  logs/error
.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { 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 on; #tcp_nopush on; #keepalive_timeout 0; client_max_body_size 2m; #gzip on; # 包含域名配置檔案( 支援萬用字元)
include vhost/*.conf; }

2、配置 fastcgi.conf檔案

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;    # 指令碼檔案請求的路徑  
fastcgi_param  QUERY_STRING       $query_string;            # 請求的引數;如?app=123  
fastcgi_param  REQUEST_METHOD     $request_method;            # 請求的動作(GET,POST)  
fastcgi_param  CONTENT_TYPE       $content_type;             # 請求頭中的Content-Type欄位  
fastcgi_param  CONTENT_LENGTH     $content_length;             # 請求頭中的Content-length欄位。    
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;         # 指令碼名稱   
fastcgi_param  REQUEST_URI        $request_uri;              # 請求的地址不帶引數  
fastcgi_param  DOCUMENT_URI       $document_uri;             # 與$uri相同。   
fastcgi_param  DOCUMENT_ROOT      $document_root;             # 網站的根目錄。在server配置中root指令中指定的值   
fastcgi_param  SERVER_PROTOCOL    $server_protocol;             # 請求使用的協議,通常是HTTP/1.0或HTTP/1.1。    
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;                # cgi 版本  
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;            # nginx 版本號,可修改、隱藏  
fastcgi_param  REMOTE_ADDR        $remote_addr;             # 客戶端IP  
fastcgi_param  REMOTE_PORT        $remote_port;             # 客戶端埠  
fastcgi_param  SERVER_ADDR        $server_addr;             # 伺服器IP地址  
fastcgi_param  SERVER_PORT        $server_port;             # 伺服器埠  
fastcgi_param  SERVER_NAME        $server_name;             # 伺服器名,域名在server配置中指定的server_name
#fastcgi_param  PATH_INFO         $path_info;                # 可自定義變數  
# PHP only, required if PHP was built with --enable-force-cgi-redirect  
fastcgi_param  REDIRECT_STATUS    200;

3、配置虛擬主機公用配置檔案server.conf

# php檔案訪問配置
location ~ .*\.(php|php5)?$
{
    #fastcgi_pass unix:/tmp/php-cgi.sock;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
}

# 靜態檔案快取30天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
    expires 30d;
    # access_log off;
}

# js,css檔案快取15個小時
location ~ .*\.(js|css)?$
{
    expires 15d;
    # access_log off;
}

4、建立虛擬主機檔案 vhost/iong.cn.conf

server {
    listen 80;

    # 配置域名
    server_name  www.iong.cn iong.cn;

    # 配置網站目錄
    root   /usr/local/nginx/html/iong.cn;

    # 配置域名重定向
    if ($host != 'www.iong.cn' ) {
        rewrite ^/(.*)$ http://www.yphp.cn/$1 permanent;
    }

    location / {

        # 配置rewrite
        if (!-e $request_filename) {
            rewrite  ^(.*)$  /index.php?s=$1  last;
            break;
        }

        # include  /usr/local/nginx/html/yphp/.htaccess;
        # rewrite ^/(.+)/(.+)[/]?$ /index.php?m=$1&a=$2 last;

        # 配置預設訪問檔案
        index  index.php index.html index.htm;
    }

    # 包含虛擬主機公用配置檔案
    include server.conf;
}

5、重啟服務

/etc/init.d/nginx stop # 停止nginx 服務 或 service nginx start
/etc/init.d/nginx start # 啟動nginx 服務

6、繫結host檔案,然後在瀏覽器中開啟iong.cn 即可

Composer安裝
1、在 Linux和 Mac OS X 中可以執行如下命令:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer