配置 Web 服務器

推薦使用的 Apache 配置
在 Apache 的 httpd.conf 文件或在一個虛擬主機配置文件中使用如下配置。 注意,你應該將 path/to/basic/web 替換爲實際的 basic/web 目錄。

# 設置文檔根目錄爲 "basic/web"
DocumentRoot "path/to/basic/web"

<Directory "path/to/basic/web">
    # 開啓 mod_rewrite 用於美化 URL 功能的支持(譯註:對應 pretty URL 選項)
    RewriteEngine on
    # 如果請求的是真實存在的文件或目錄,直接訪問
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # 如果請求的不是真實文件或目錄,分發請求至 index.php
    RewriteRule . index.php

    # if $showScriptName is false in UrlManager, do not allow accessing URLs with script name
    RewriteRule ^index.php/ - [L,R=404]

    # ...其它設置...
</Directory>

推薦使用的 Nginx 配置
爲了使用 Nginx,你應該已經將 PHP 安裝爲 FPM SAPI 了。 你可以使用如下 Nginx 配置,將 path/to/basic/web 替換爲實際的 basic/web 目錄, mysite.local 替換爲實際的主機名以提供服務。

server {
    charset utf-8;
    client_max_body_size 128M;

    listen 80; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name mysite.test;
    root        /path/to/basic/web;
    index       index.php;

    access_log  /path/to/basic/log/access.log;
    error_log   /path/to/basic/log/error.log;

    location / {
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # uncomment to avoid processing of calls to non-existing static files by Yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

    # deny accessing php files for the /assets directory
    location ~ ^/assets/.*\.php$ {
        deny all;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $uri =404;
    }

    location ~* /\. {
        deny all;
    }
}

使用該配置時,你還應該在 php.ini 文件中設置 cgi.fix_pathinfo=0 , 能避免掉很多不必要的 stat() 系統調用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章