### 動態網站
[toc]
等一下 - 在所有以前的例子中,我們從來沒有在硬盤中創建任何目錄來存儲這些路由。簡短的答案:我們不必。所有F3路線都是虛擬的。它們不反映我們的硬盤文件夾結構。它們不反映我們的硬盤文件夾結構。如果您有不使用框架的程序或靜態文件(圖像,CSS等)只要這些文件的路徑與應用程序中定義的任何路由不沖突 - 如果服務器配置正確,則Web服務器軟件將將它們傳遞到用戶的瀏覽器。
##### PHP 5.4的內置WEB服務器
* * * * *
> PHP最新的穩定版本有自己的內置Web服務器。使用以下配置啟動它:
```php
php -S localhost:80 -t /var/www/
```
上述命令將開始將所有請求路由到Web根`/var/www`。如果接收到對文件或文件夾的傳入HTTP請求,PHP將在Web根目錄中查找它,并將其發送到瀏覽器(如果找到)。否則,PHP將加載默認的`index.php`(包含啟用F3的代碼)。
##### Apache配置實例
* * * * *
> 如果您使用Apache,請確保激活apache.conf(或httpd.conf)文件中的URL重寫模塊(mod_rewrite)。您還應該創建一個包含以下內容的.htaccess文件:
```
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L,QSA]
```
腳本告訴Apache,只要HTTP請求到達,并且如果沒有找到物理文件(!-f)或路徑(!-d)或符號鏈接(!-l),它應該將控制權轉移到`index.php`,
它包含我們的主/前端控制器,而這又調用框架。
包含上述Apache指令的.htaccess文件應始終位于與index.php相同的文件夾中。 您還需要設置Apache,以便知道您的硬盤中index.php的物理位置。一個典型的配置是:
```
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
Options -Indexes +FollowSymLinks +Includes
AllowOverride All
Order allow,deny
Allow from All
</Directory>
```
如果您將Fat-Free項目放入現有文檔根目錄的子文件夾中,某些Apache配置可能需要在`.htaccess`文件中定義一個RewriteBase。如果應用程序不工作,或默認路由`/`工作,但是`/test`可能會失敗,請嘗試添加基本路徑:
```
RewriteEngine On
RewriteBase /fatfree-project/
```
如果您同時開發多個應用程序,則可以輕松管理虛擬主機配置:
```
NameVirtualHost *
<VirtualHost *>
ServerName site1.com
DocumentRoot "/var/www/site1"
<Directory "/var/www/site1">
Options -Indexes +FollowSymLinks +Includes
AllowOverride All
Order allow,deny
Allow from All
</Directory>
</VirtualHost>
<VirtualHost *>
ServerName site2.com
DocumentRoot "/var/www/site2"
<Directory "/var/www/site2">
Options -Indexes +FollowSymLinks +Includes
AllowOverride All
Order allow,deny
Allow from All
</Directory>
</VirtualHost>
```
必須在`/etc/hosts`文件中列出每個`ServerName`(我們示例中的site1.com和site2.com)。在Windows上,您應該編輯`C:/WINDOWS/system32/drivers/etc/hosts`。可能需要重新啟動才能實現更改。然后,您可以將Web瀏覽器指向地址`http://site1.com`或`http://site2.com`。虛擬主機使您的應用程序更容易部署。
##### Nginx配置實例
* * * * *
> 對于Nginx服務器,以下是推薦的配置(將ip_address:port替換為您的環境的FastCGI PHP設置):
```
server {
root /var/www/html;
location / {
index index.php index.html index.htm;
try_files $uri /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass ip_address:port;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
```
##### Lighttpd配置實例
* * * * *
> Lighttpd服務器的配置方式類似:
```
$HTTP["host"] =~ "www\.example\.com$" {
url.rewrite-once = ( "^/(.*?)(\?.+)?$"=>"/index.php/$1?$2" )
server.error-handler-404 = "/index.php"
}
```
##### IIS 配置實例
* * * * *
> 安裝URL重寫模塊和對應于您的Windows版本的相應的.NET框架。然后在您的應用程序根目錄中創建一個名為web.config的文件,具有以下內容:
```
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Application" stopProcessing="true">
<match url=".*" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
```