[TOC]
# Web服務器設置
為了使Phalcon應用程序的路由能夠正常工作,您需要設置Web服務器以正確處理重定向。流行Web服務器的安裝說明如下:
## PHP-FPM
[PHP-FPM](http://php.net/manual/en/install.fpm.php)(FastCGI Process Manager)通常用于處理PHP文件。如今,PHP-FPM與所有基于Linux的PHP發行版捆綁在一起。
在 **Windows** 上,PHP-FPM通過文件 `php-cgi.exe` 位于PHP分發存檔中,您可以使用此腳本啟動它以幫助設置選項。Windows不支持unix套接字,因此該腳本將在端口`9000`上以TCP模式啟動fast-cgi。
使用以下內容創建文件 `php-fcgi.bat` :
```bat
@ECHO OFF
ECHO Starting PHP FastCGI...
set PATH=C:\PHP;%PATH%
c:\bin\RunHiddenConsole.exe C:\PHP\php-cgi.exe -b 127.0.0.1:9000
```
## PHP內置Web服務器(面向開發人員)
為了加快Phalcon應用程序在開發中的運行速度,最簡單的方法是使用這個內置的PHP服務器。請勿在生產環境中使用此服務器。您需要以下Nginx和Apache的配置。
### Phalcon配置
To enable dynamic URI rewrites, without Apache or Nginx, that Phalcon needs, you can use the following router file:
要在沒有Apache或Nginx的情況下啟用動態URI重寫,Phalcon需要,您可以使用以下路由器文件:[.htrouter.php](https://github.com/phalcon/phalcon-devtools/blob/master/templates/.htrouter.php)
如果您使用`Phalcon-Devtools`創建了應用程序,則該文件應該已存在于項目的根目錄中,您可以使用以下命令啟動服務器:
```bash
$(which php) -S localhost:8000 -t public .htrouter.php
```
上面命令的解剖:
- `$(which php)` - 將插入PHP二進制文件的絕對路徑
- `-S localhost:8000` - 使用提供的方式調用服務器模式 `host:port`
- `-t public` - 定義服務器根目錄,這是php將請求路由到公共目錄中的JS,CSS和圖像等靜態資源所必需的
- `.htrouter.php` - 將為每個請求設置的入口點
然后將瀏覽器指向`http://localhost:8000/`以檢查一切是否正常。
## Nginx
[Nginx](http://wiki.nginx.org/Main) 是一個免費的,開源的,高性能的HTTP服務器和反向代理,以及IMAP/POP3代理服務器。與傳統服務器不同,Nginx不依賴線程來處理請求。相反,它使用更加可擴展的事件驅動(異步)架構。
此體架構在負載下使用較小但更重要的可預測內存量。
Phalcon與Nginx和PHP-FPM提供了一套功能強大的工具,可為PHP應用程序提供最高性能。
### 安裝Nginx
[Nginx官網](https://www.nginx.com/resources/wiki/start/topics/tutorials/install/)
### Phalcon配置
您可以使用以下潛在配置來使用Phalcon設置Nginx:
```nginx
server {
# Port 80 will require Nginx to be started with root permissions
# Depending on how you install Nginx to use port 80 you will need
# to start the server with `sudo` ports about 1000 do not require
# root privileges
# listen 80;
listen 8000;
server_name default;
##########################
# In production require SSL
# listen 443 ssl default_server;
# ssl on;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
# These locations depend on where you store your certs
# ssl_certificate /var/nginx/certs/default.cert;
# ssl_certificate_key /var/nginx/certs/default.key;
##########################
# This is the folder that index.php is in
root /var/www/default/public;
index index.php index.html index.htm;
charset utf-8;
client_max_body_size 100M;
fastcgi_read_timeout 1800;
# Represents the root of the domain
# http://localhost:8000/[index.php]
location / {
# Matches URLS `$_GET['_url']`
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
# When the HTTP request does not match the above
# and the file ends in .php
location ~ [^/]\.php(/|$) {
# try_files $uri =404;
# Ubuntu and PHP7.0-fpm in socket mode
# This path is dependent on the version of PHP install
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# Alternatively you use PHP-FPM in TCP mode (Required on Windows)
# You will need to configure FPM to listen on a standard port
# https://www.nginx.com/resources/wiki/start/topics/examples/phpfastcgionwindows/
# fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
include fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_param PATH_INFO $fastcgi_path_info;
# fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
# and set php.ini cgi.fix_pathinfo=0
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
access_log off;
}
}
```
### 啟動Nginx
通常從命令行`啟動nginx`,但這取決于您的安裝方法。
## Apache
[Apache](http://httpd.apache.org/) 是許多平臺上流行且眾所周知的Web服務器。
### Phalcon配置
以下是可用于使用Phalcon設置Apache的潛在配置。這些注釋主要關注`mod_rewrite`模塊的配置,允許使用友好URL和路由器組件。通常,應用程序具有以下結構:
```bash
test/
app/
controllers/
models/
views/
public/
css/
img/
js/
index.php
```
#### 根目錄
這是最常見的情況,應用程序安裝在文檔根目錄下的任何目錄中。在這種情況下,我們使用兩個`.htaccess`文件,第一個隱藏應用程序代碼,將所有請求轉發到應用程序的文檔根目錄(`public/`)。
>[danger] 請注意,使用`.htaccess`文件需要安裝apache才能設置`AllowOverride All`選項。
```apacheconfig
# test/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ((?s).*) public/$1 [L]
</IfModule>
```
第二個`.htaccess` 文件位于`public/`目錄中,這會將所有URI重寫為`public/index.php`文件:
```apacheconfig
# test/public/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
```
對于在uri參數中使用波斯字母'?'(meem)的用戶,`mod_rewrite`存在問題。要使匹配與英文字符一樣工作,您需要更改`.htaccess`文件:
```apacheconfig
# test/public/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9A-Za-z\x7f-\xff]*)$ index.php?params=$1 [L]
</IfModule>
```
如果您的uri包含英語以外的字符,您可能需要采用上述更改以允許`mod_rewrite`準確匹配您的路由。
#### Apache配置
如果您不想使用`.htaccess`文件,可以將這些配置移動到apache的主配置文件中:
```apacheconfig
<IfModule mod_rewrite.c>
<Directory "/var/www/test">
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ((?s).*) public/$1 [L]
</Directory>
<Directory "/var/www/test/public">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
</Directory>
</IfModule>
```
#### 虛擬服務器
第二種配置允許您在虛擬主機中安裝Phalcon應用程序:
```apacheconfig
<VirtualHost *:80>
ServerAdmin admin@example.host
DocumentRoot "/var/vhosts/test/public"
DirectoryIndex index.php
ServerName example.host
ServerAlias www.example.host
<Directory "/var/vhosts/test/public">
Options All
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
```
## Cherokee
[Cherokee](http://www.cherokee-project.com/) 是一款高性能的網絡服務器。它非常快速,靈活且易于配置。
### Phalcon配置
Cherokee提供友好的圖形界面,幾乎可以配置Web服務器中的所有可用設置。
切換root管理員權限, 執行`/path-to-cherokee/sbin/cherokee-admin`以啟動Cherokee

單擊`vServers`創建新的虛擬主機,然后添加新的虛擬服務器:

最近添加的虛擬服務器必定出現在屏幕的左側欄中。在`Behaviors`選項卡中,您將看到此虛擬服務器的一組默認行為。單擊`Rule Management` 按鈕。刪除標記為`Directory /cherokee\_themes`和`Directory /cherokee\_themes`的那些:

使用向導添加`PHP Language`行為。此行為允許您運行PHP應用程序:

通常,此行為不需要其他設置。添加其他行為,這次是在`Manual Configuration`部分。在`Rule Type`中選擇`File Exists`,然后確保啟用了`Match any file`選項:

在`Handler`選項卡中,選擇`List & Send`作為處理程序:

編輯`Default`行為以啟用URL重寫引擎。將處理程序更改為`Redirection`,然后將以下正則表達式添加到引擎`^(.\*)$`:

最后,確保行為具有以下順序:

在瀏覽器中執行應用程序:

- 常規
- Welcome
- 貢獻
- 生成回溯
- 測試重現
- 單元測試
- 入門
- 安裝
- Web服務器設置
- WAMP
- XAMPP
- 教程
- 基礎教程
- 教程:創建一個簡單的REST API
- 教程:V?kuró
- 提升性能
- 教程:INVO
- 開發環境
- Phalcon Compose (Docker)
- Nanobox
- Phalcon Box (Vagrant)
- 開發工具
- Phalcon開發者工具的安裝
- Phalcon開發者工具的使用
- 調試應用程序
- 核心
- MVC應用
- 微應用
- 創建命令行(CLI)應用程序
- 依賴注入與服務定位
- MVC架構
- 服務
- 使用緩存提高性能
- 讀取配置
- 上下文轉義
- 類加載器
- 使用命名空間
- 日志
- 隊列
- 數據庫
- 數據庫抽象層
- Phalcon查詢語言(PHQL)
- ODM(對象文檔映射器)
- 使用模型
- 模型行為
- ORM緩存
- 模型事件
- 模型元數據
- 模型關系
- 模型事務
- 驗證模型
- 數據庫遷移
- 分頁
- 前端
- Assets管理
- 閃存消息
- 表單
- 圖像
- 視圖助手(標簽)
- 使用視圖
- Volt:模板引擎
- 業務邏輯
- 訪問控制列表(ACL)
- 注解解析器
- 控制器
- 調度控制器
- 事件管理器
- 過濾與清理
- 路由
- 在session中存儲數據
- 生成URL和路徑
- 驗證
- HTTP
- Cookies管理
- 請求環境
- 返回響應
- 安全
- 加密/解密
- 安全
- 國際化
- 國際化
- 多語言支持