# Web 服務器
典型地使用前端控制器將 web 服務器接收到的 HTTP 請求匯集到一個單獨的 PHP 文件。下面的文檔說明了如何通知你的 web 服務器將接收到的 HTTP 請求發送到 PHP 前端控制器文件。
## PHP built-in server
Run the following command in terminal to start localhost web server, assuming `./public/` is public-accessible directory with `index.php` file:
```
php -S localhost:8080 -t ./public/
```
## Apache 配置
確保 `.htaccess` 和 `index.php` 這兩個文件位于同一個可公開訪問的目錄中。這個 `.htaccess` 文件應當包含以下代碼:
```
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
```
為了保證 `.htaccess` 的重寫(rewrite)規則能正常生效,務必確保你的 Apache 虛擬主機開啟了 `AllowOverride` 選項。
```
AllowOverride All
```
## Nginx 配置
這是一個例子,在 Nginx 虛擬主機上針對域名 `example.com` 的配置。它監聽80端口上的入境(inbound)HTTP 連接。它假定一個PHP-FPM服務器在端口9000上運行。你需要將 `server_name`, `error_log`, `access_log`, 和 `root` 這些指令修改成你自己的值。其中 `root` 指令是你的應用程序公共文件根目錄的路徑;你的 Slim 應用的 `index.php` 前端控制器文件應該放在這個目錄中。
```
server {
listen 80;
server_name example.com;
index index.php;
error_log /path/to/example.error.log;
access_log /path/to/example.access.log;
root /path/to/public;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
}
}
```
## HipHop Virtual Machine
Your HipHop Virtual Machine configuration file should contain this code (along with other settings you may need). Be sure you change the `SourceRoot` setting to point to your Slim app’s document root directory.
```
Server {
SourceRoot = /path/to/public/directory
}
ServerVariables {
SCRIPT_NAME = /index.php
}
VirtualHost {
* {
Pattern = .*
RewriteRules {
* {
pattern = ^(.*)$
to = index.php/$1
qsa = true
}
}
}
}
```
## IIS
務必確保 `Web.config` 和 `index.php` 這兩個文件位于同一個可公開訪問的目錄中。 這個 `Web.config` 文件必須包含以下代碼:
```
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="slim" patternSyntax="Wildcard">
<match url="*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
```
## lighttpd
Your lighttpd configuration file should contain this code (along with other settings you may need). This code requires lighttpd >= 1.4.24.
```
url.rewrite-if-not-file = ("(.*)" => "/index.php/$0")
```
This assumes that Slim’s `index.php` is in the root folder of your project (www root).