安裝
CakePHP 很容易安裝。最小安裝隻要有一個 web 伺服器和一份 Cake 的副本,就足夠了!本手冊主要聚焦于在 Apache 上安裝 Cake(因為 Apache 最通用), 你也可以在 LightHTTPD 或者 Microsoft IIS 之類的 web 伺服器上配置運作 Cake。
必備的軟體
- HTTP 伺服器。例如: Apache。 mod_rewrite 是推薦選擇的, 但不是必須的。
- PHP 5.2.8 或更高版本。
資料庫引擎不是必須的,但是我們認為很多應用程式将使用它。CakePHP 支援如下資料庫引擎:
- MySQL (4 或更高版本)
- PostgreSQL
- Microsoft SQL Server
- SQLite
注意:所有内置的驅動都需要 PDO。你需要确定你已經正确安裝了 PDO 擴充。
許可
CakePHP 的許可是基于 MIT license 的。這意味着你能夠在保持版權聲明完整的前提下自由的修改、分發和重釋出其源碼。 你也可以在商業或封閉源代碼的應用程式中包含 CakePHP。
下載下傳 CakePHP
有兩種主要途徑用來獲得 CakePHP 的最新副本。你可以從主站下載下傳(有三種格式:zip/tar.gz/tar.bz2), 或者從 git 倉庫中檢出代碼。
要想下載下傳最新的主版本,通路主站 http://www.cakephp.org 并且點選 “Download Now” 連結。
全部目前版本都寄存在 Github 上。Github 收藏了全部的 CakePHP,并且還包含了許多的 CakePHP 的插件。 有效的 CakePHP 版本在 Github downloads 。
另外,你也可以擷取最新的帶有全部 bug 修正的每分鐘送出的源代碼。 這可以通路 Github 倉庫并克隆副本:
git clone git://github.com/cakephp/cakephp.git
權限
CakePHP 的一些不同操作需要使用 app/tmp 目錄:子產品描述、視圖緩存和 session 資訊等。
是以,你需要确定 web 伺服器使用者擁有 cake 安裝目錄下的 app/tmp 目錄和它的全部子目錄的寫權限。
設定
CakePHP 可以極其簡單地布署在 web 伺服器的文檔根目錄中,也可以以你所期望的方式靈活複雜地進行布署。 這一節将覆寫 CakePHP 的三種主要安裝類型: 開發、産品和進階。
- 開發:最容易進行。應用程式的 URLs 包括 CakePHP 的安裝目錄名,安全級别低。
- 産品:包括配置 web 伺服器的文檔根目錄的能力、整潔的 URL,非常安全。
- 進階: 帶有許多配置,允許你将 CakePHP 的關鍵目錄放在檔案系統的不同位置,可以在多個 CakePHP 應用程式間共享同一個 CakePHP 核心庫檔案夾。
開發
開發安裝是設定 Cake 的最快方法。 本例将幫助你安裝一個 CakePHP 應用程式,并使其在http://www.example.com/cake_2_0/ 位址上生效。 我們假定你的文檔根目錄是在 /var/www/html 。
将 Cake 壓縮檔案的内容解壓到 /var/www/html。現在在文檔根目錄中有了一個以下載下傳的版本為字尾命名的檔案夾(例如 cake_2.0.0)。 将這個檔案夾改名為 cake_2_0. 你的開發安裝在檔案系統看起來結構就像下面這樣:
/var/www/html/
cake_2_0/
app/
lib/
plugins/
vendors/
.htaccess
index.php
README
如果 web 伺服器配置正确,現在就可以通過 http://www.example.com/cake_2_0/ 通路你的 Cake 應用程式了。
多個應用程式使用同一份 CakePHP
如果你開發幾個應用程式,常常會感覺需要在它們之間共享一份 CakePHP 核心副本。有幾個辦法能達到這個目的。最簡單的是使用 PHP 的 include_path。 首先,克隆 CakePHP 到一個目錄。本示例中,我們使用 ~/projects :
git clone git://github.com/cakephp/cakephp.git ~/projects/cakephp
這會把 CakePHP 克隆至 ~/projects 目錄。如果不想使用 git,你可以下載下傳 zip 包,其餘的步驟是相同的。 接下來,你需要找到并編輯你的 php.ini 檔案。 在 *nix 系統中,常常會是 /etc/php.ini,可以使用 php -i 查找 ‘載入的配置檔案’。 一旦你找到正确的 ini 檔案,編輯 include_path 選項,使其包含 ~/projects/cakephp/lib。 例如:
include_path = .:/home/mark/projects/cakephp/lib:/usr/local/php/lib/php
然後,啟動 web 服務,在 phpinfo() 中能夠看到這些修改的結果.
注解
如果使用 windows 系統,包含路徑的分隔符就是 ;(半角分号),而不是 :(半角冒号)。
完成了 include_path 設定,應用程式就能夠自動找到 CakePHP。
産品
産品安裝是配置 Cake 的更靈活的方法。 這種方式允許整個域作為 CakePHP 應用程式。 本示例将幫助你在檔案系統的任意位置安裝 Cake,并且使其在 http://www.example.com 生效。 需要注意的是,這種安裝可能需要改變 Apache web 伺服器的 DocumentRoot 。
将 Cake 壓縮包的内容解壓到你選擇的目錄。在本救命中,我們假定你選擇将 Cake 安裝到 /cake_install。 你的産品安裝的檔案夾結構類似下面這樣:
/cake_install/
app/
webroot/ (this directory is set as the ``DocumentRoot``
directive)
lib/
plugins/
vendors/
.htaccess
index.php
README
程式員需要将 Apache 域名的 DocumentRoot 指向到安裝目錄:
DocumentRoot /cake_install/app/webroot
如果 web 伺服器配置正确,将可以在 http://www.example.com 通路你的 Cake 應用程式。
進階安裝和伺服器精确配置
進階安裝
某些情況下,你希望将 CakePHP 的各個目錄放在檔案系統的不同位置。 這可能是由于共享主機的限制,或者是多個應用程式想要共享同一份 Cake 庫。 這一節描述了如何跨檔案系統鋪設 CakePHP 目錄。
首先,需要了解 Cake 應用程式的三個主要組成部分:
- CakePHP 核心庫, 在 /lib/Cake 檔案夾。
- 應用程式代碼,在 /app 檔案夾。
- 應用程式的 web 根目錄,通常在 /app/webroot 檔案夾。
這些檔案中的每一個都可以被放置在檔案系統的任意位置,web 根目錄例外,它需要通過 web 伺服器通路。 你甚至可以将 web 根目錄移出 app 檔案件,隻要你告訴 Cake 它在哪兒。
為了配置 CakePHP 安裝,你需要修改如下幾個檔案。
- /app/webroot/index.php
- /app/webroot/test.php (如果你使用 測試 特性。)
有三個内容需要編輯: ROOT, APP_DIR, 和 CAKE_CORE_INCLUDE_PATH 。
- ROOT 用來設定包含 app 檔案夾的路徑。
- APP_DIR 用來設定 app 檔案夾的(基本)名稱。
- CAKE_CORE_INCLUDE_PATH 用來設定 CakePHP 庫檔案夾的路徑。
讓我們通過運作一個示例向你展示實踐中是如何進行進階安裝的。假設我想設定 CakePHP 按如下方式工作:
- CakePHP 核心庫放置在 /usr/lib/cake 檔案夾。
- 我的應用程式 web 根目錄放置在 /var/www/mysite/ 檔案夾。
- 我的應用程式的 app 檔案夾放置在 /home/me/myapp 檔案夾。
對于這種類型的安裝,我需要編輯 webroot/index.php 檔案(本示例中,内容添加在 /var/www/mysite/index.php 的尾部),看上去就像下面這樣:
// /app/webroot/index.php (節選,注釋已經被移除)
if (!defined('ROOT')) {
define('ROOT', DS . 'home' . DS . 'me');
}
if (!defined('APP_DIR')) {
define ('APP_DIR', 'myapp');
}
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
define('CAKE_CORE_INCLUDE_PATH', DS . 'usr' . DS . 'lib');
}
推薦使用 DS 常量作為檔案路徑的分隔符。這可以防止因使用了錯誤的分隔符造成的那些檔案找不到的問題,并且使代碼的可移植性更高。
Apache 和 mod_rewrite(以及 .htaccess)
當 CakePHP 被建構成基于 mod_rewrite 工作時(通常都是這樣),我們注意到一些使用者努力地去做一切能使他們的系統更好的運作的事情。
有幾件可能會使運作良好的事,你應該嘗試一下。 首先看看 httpd.conf (确定你編輯的是系統的 httpd.conf,而不是使用者的或特定站點的)。
- 确定 .htaccess 被允許覆寫,并且 DocumentRoot 的 AllowOverride 被正确設定為 All。你應該看到如下的内容:
# Each directory to which Apache has access can be configured with respect # to which services and features are allowed and/or disabled in that # directory (and its subdirectories). # # First, we configure the "default" to be a very restrictive set of # features. # <Directory /> Options FollowSymLinks AllowOverride All # Order deny,allow # Deny from all </Directory>
- 确定已經正确地加載了 mod_rewrite。應該能看到如下的内容:
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
在很多系統裡,這些預設是被注釋掉的(行的起始是一個 #),這時你隻需要删除起始的 # 符号。
修改完成後,重新開機 Apache 并确定設定已經生效。
檢測你的 .htaccess 是不是真的在正确的目錄中。
在拷貝過程可能會發生這樣一種情況,因為某些作業系統隐藏了以 . 開頭的檔案,以緻看不到它的拷貝副本。
-
确定你的 CakePHP 副本來自本站的 Downloads 一節指定的站點或 Git 倉庫,并已經被正确地解壓(通過檢查 .htaccess 檔案)。
Cake 根目錄(需要拷貝到你的文檔,重定向每件事物到 Cake app):
Cake app 目錄(拷貝到應用程式的頂級目錄)<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ app/webroot/ [L] RewriteRule (.*) app/webroot/$1 [L] </IfModule>
Cake webroot 目錄(拷貝到應用程式的 webroot 目錄)<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ webroot/ [L] RewriteRule (.*) webroot/$1 [L] </IfModule>
如果 CakePHP 站點仍然有 mod_rewrite 方面的問題,可能需要試着編輯虛拟主機的設定。 在 ubuntu 中,編輯 /etc/apache2/sites-available/default 檔案(在它的頒發依賴中可以找到)。 在這個檔案中,将 AllowOverrideNone 改成 AllowOverride All,你将會有:<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php [QSA,L] </IfModule>
<Directory /> Options FollowSymLinks AllowOverride All </Directory> <Directory /var/www> Options Indexes FollowSymLinks MultiViews AllowOverride All Order Allow,Deny Allow from all </Directory>
在 Mac OSX,或者其它解決方案中,使用虛拟主機工具確定虛拟主機指向了你的 web 根目錄。
對于更多的主機服務(GoDaddy,1and1),你的 web 伺服器實際上使用了使用者目錄來服務,并且已經應用了 mod_rewrite。如果你把 CakePHP 安裝在使用者目錄中(http://example.com/~username/cakephp/) 或其它已經應用了 mod_rewrite 的 URL 結構,你需要在 CakePHP 所用的 .htaccess(/.htaccess,/app/.htaccess, /app/webroot/.htaccess) 中添加 RewriteBase 語句。
也能加入 RewriteEngine 指令到同一節中,那麼 web 根目錄的 .htaccess 看起來就會像下面這樣:
修改的具體情況依賴于實際的設定,還可以包含與 CakePHP 無關的其它内容。更多資訊請參考 Apache 線上文檔。<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /path/to/cake/app RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php [QSA,L] </IfModule>
漂亮的 URL 與 nginx
nginx 是一款流行的伺服器軟體,它比 Apache 使用的系統資源更少。 它的缺點是不能像 Apache 那樣使用 .htaccess 檔案, 它必須在可通路站點的配置中建立一些重寫的 URL。 依賴于你的配置,你必須編輯這些,至少你需要讓 PHP 以 FastCGI 執行個體的方式運作。
server {
listen 80;
server_name www.example.com;
rewrite ^(.*) http://example.com$1 permanent;
}
server {
listen 80;
server_name example.com;
# root directive should be global
root /var/www/example.com/public/app/webroot/;
access_log /var/www/example.com/log/access.log;
error_log /var/www/example.com/log/error.log;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$uri&$args;
}
location ~ \.php$ {
include /etc/nginx/fcgi.conf;
fastcgi_pass 127.0.0.1:10005;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
基于IIS7 的 URL 重寫(Windows 主機)
IIS7原生是不支援 .htaccess的。一些插件可以添加這種支援,也可以用 CakePHP 自帶的 rewrite 向 IIS 導入 htaccess 規則。步驟如下:
- 使用 Microsoft’s Web Platform Installer (微軟 web 平台安裝器)安裝 URL Rewrite 2.0 子產品。
- 在 CakePHP 檔案夾建立一個檔案,命名為 web.config。
- 使用記事本或其它 xml 安全的編輯器,将如下代碼拷貝到 web.config:...
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="/" />
</rule>
<rule name="Imported Rule 3" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<action type="Rewrite" url="/{R:1}" />
</rule>
<rule name="Imported Rule 4" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
也可以使用 IIS Rewrite 子產品的導入功能從 CakePHP 的根目錄、/app/ 及 /app/webroot目錄中的 .htaccess 直接導入規則——雖然一些 IIS 的編輯必須要得到這些工作。使用這種方式導入規則,IIS 将自動建立 web.config 檔案。
一旦帶有正确的對 IIS 友好的 rewrite 規則的 web.config 檔案,CakePHP 的連結、js、css 和路由都會正确的工作。
漂亮的 URLs 和 Lighttpd
注意: 以下内容為2.0幫助檔案中的内容,2.3已經删除了這一部分。 –譯者注
雖然 Lighttpd 包含 rewrite 子產品,但是它不同于 Apache 的 mod_rewrite。為了在使用 Lighty 擷取“漂亮的 URLs”,你有兩個選擇。一是使用 mod_rewrite,二是通過使用 LUA 腳本和 mod_magnet。
使用 mod_rewrite:獲得“漂亮的 URLs”的最快的方法是在 Lighty 的配置中添加如下腳本。再編輯一下URL,就應該沒問題了。需要注意的是當 CakePHP 安裝在子檔案夾時這種方法無效。
$HTTP["host"] =~ "^(www\.)?example.com$" {
url.rewrite-once = (
# if the request is for css|files etc, do not pass on to Cake
"^/(css|files|img|js)/(.*)" => "/$1/$2",
"^([^\?]*)(\?(.+))?$" => "/index.php/$1&$3",
)
evhost.path-pattern = "/home/%2-%1/www/www/%4/app/webroot/"
}
使用 mod_magnet:和 CakePHP、Lighttpd 一起使用“漂亮的 URLs”,在 /etc/lighttpd/cake中放置如下 LUA 腳本。
-- little helper function
function file_exists(path)
local attr = lighty.stat(path)
if (attr) then
return true
else
return false
end
end
function removePrefix(str, prefix)
return str:sub(1,#prefix+1) == prefix.."/" and str:sub(#prefix+2)
end
-- prefix without the trailing slash
local prefix = ''
-- the magic ;)
if (not file_exists(lighty.env["physical.path"])) then
-- file still missing. pass it to the fastcgi backend
request_uri = removePrefix(lighty.env["uri.path"], prefix)
if request_uri then
lighty.env["uri.path"] = prefix .. "/index.php"
local uriquery = lighty.env["uri.query"] or ""
lighty.env["uri.query"] = uriquery .. (uriquery ~= "" and "&" or "") .. "url=" .. request_uri
lighty.env["physical.rel-path"] = lighty.env["uri.path"]
lighty.env["request.orig-uri"] = lighty.env["request.uri"]
lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
end
end
-- fallthrough will put it back into the lighty request loop
-- that means we get the 304 handling for free. ;)
注意: 如果要在子檔案夾中運作 CakePHP 安裝,要在上面的腳本中設定 prefix = ‘subdirectory_name’。
然後通知 Lighttpd 你的虛拟主機是什麼:
$HTTP["host"] =~ "example.com" {
server.error-handler-404 = "/index.php"
magnet.attract-physical-path-to = ( "/etc/lighttpd/cake.lua" )
server.document-root = "/var/www/cake-1.2/app/webroot/"
# Think about getting vim tmp files out of the way too
url.access-deny = (
"~", ".inc", ".sh", "sql", ".sql", ".tpl.php",
".xtmpl", "Entries", "Repository", "Root",
".ctp", "empty"
)
}
點燃它
讓我們再來看看 CakePHP 的最佳實踐。 依靠這些配置,再把你的浏覽器指向 http://example.com/ 或者http://example.com/cake_install/ 。 現在,将會看到 CakePHP 的預設首頁,并有一條告知你目前資料庫配置狀态的消息。
恭喜!你已經完成了 建立第一個 CakePHP 應用程式的 課程。
沒能正常工作?如果你看到了關于時區錯誤的資訊,取消 app/Config/core.php 中如下這一行的注釋
/**
* Uncomment this line and correct your server timezone to fix
* any date & time related errors.
*/
date_default_timezone_set('UTC');