CakePHP 2.x CookBook 中文版 第二章 安裝

安裝

CakePHP 很容易安裝。最小安裝只要有一個 web 服務器和一份 Cake 的副本,就足夠了!本手冊主要聚焦於在 Apache 上安裝 Cake(由於 Apache 最通用), 你也能夠在 LightHTTPD 或者 Microsoft IIS 之類的 web 服務器上配置運行 Cake。 php

必備的軟件

  • HTTP 服務器。例如: Apache。 mod_rewrite 是推薦選擇的, 但不是必須的。
  • PHP 5.2.8 或更高版本。

數據庫引擎不是必須的,可是咱們認爲不少應用程序將使用它。CakePHP 支持以下數據庫引擎: css

  • MySQL (4 或更高版本)
  • PostgreSQL
  • Microsoft SQL Server
  • SQLite

注意:全部內置的驅動都須要 PDO。你須要肯定你已經正確安裝了 PDO 擴展。 html

許可

CakePHP 的許但是基於 MIT license 的。這意味着你可以在保持版權聲明完整的前提下自由的修改、分發和重發布其源碼。 你也能夠在商業或封閉源代碼的應用程序中包含 CakePHP。 nginx

下載 CakePHP

有兩種主要途徑用來得到 CakePHP 的最新副本。你能夠從主站下載(有三種格式:zip/tar.gz/tar.bz2), 或者從 git 倉庫中檢出代碼。 git

要想下載最新的主版本,訪問主站 http://www.cakephp.org 而且點擊 「Download Now」 連接。 程序員

所有當前版本都寄存在 Github 上。Github 收藏了所有的 CakePHP,而且還包含了許多的 CakePHP 的插件。 有效的 CakePHP 版本在 Github downloads 。 github

另外,你也能夠獲取最新的帶有所有 bug 修正的每分鐘提交的源代碼。 這能夠訪問 Github 倉庫並克隆副本: web

git clone git://github.com/cakephp/cakephp.git

權限

CakePHP 的一些不一樣操做須要使用 app/tmp 目錄:模塊描述、視圖緩存和 session 信息等。 sql

所以,你須要肯定 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 應用程序的三個主要組成部分:

  1. CakePHP 核心庫, 在 /lib/Cake 文件夾。
  2. 應用程序代碼,在 /app 文件夾。
  3. 應用程序的 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,而不是用戶的或特定站點的)。

  1. 肯定 .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>
  2. 肯定已經正確地加載了 mod_rewrite。應該能看到以下的內容:

    LoadModule rewrite_module libexec/apache2/mod_rewrite.so

    在不少系統裏,這些默認是被註釋掉的(行的起始是一個 #),這時你只須要刪除起始的 # 符號。

    修改完成後,重啓 Apache 並肯定設置已經生效。

    檢測你的 .htaccess 是否是真的在正確的目錄中。

    在拷貝過程可能會發生這樣一種狀況,由於某些操做系統隱藏了以 . 開頭的文件,以至看不到它的拷貝副本。

  3. 肯定你的 CakePHP 副原本自本站的 Downloads 一節指定的站點或 Git 倉庫,並已經被正確地解壓(經過檢查 .htaccess 文件)。

    Cake 根目錄(須要拷貝到你的文檔,重定向每件事物到 Cake app):

    <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ app/webroot/ [L] RewriteRule (.*) app/webroot/$1 [L] </IfModule>

    Cake app 目錄(拷貝到應用程序的頂級目錄)

    <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ webroot/ [L] RewriteRule (.*) webroot/$1 [L] </IfModule>

    Cake webroot 目錄(拷貝到應用程序的 webroot 目錄)

    <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php [QSA,L] </IfModule>

    如 果 CakePHP 站點仍然有 mod_rewrite 方面的問題,可能須要試着編輯虛擬主機的設置。 在 ubuntu 中,編輯 /etc/apache2/sites-available/default 文件(在它的頒發依賴中能夠找到)。 在這個文件中,將 AllowOverrideNone 改爲 AllowOverride All,你將會有:

    <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 看起來就會像下面這樣:

    <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>

    修改的具體狀況依賴於實際的設置,還能夠包含與 CakePHP 無關的其它內容。更多信息請參考 Apache 在線文檔。

漂亮的 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 規則。步驟以下:

  1. 使用 Microsoft’s Web Platform Installer (微軟 web 平臺安裝器)安裝 URL Rewrite 2.0 模塊。
  2. 在 CakePHP 文件夾創建一個文件,命名爲 web.config。
  3. 使用記事本或其它 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');
相關文章
相關標籤/搜索