CakePHP 很容易安装。最小安装只要有一个 web 服务器和一份 Cake 的副本,就足够了!本手册主要聚焦于在 Apache 上安装 Cake(由于 Apache 最通用), 你也能够在 LightHTTPD 或者 Microsoft IIS 之类的 web 服务器上配置运行 Cake。 php
数据库引擎不是必须的,可是咱们认为不少应用程序将使用它。CakePHP 支持以下数据库引擎: css
注意:全部内置的驱动都须要 PDO。你须要肯定你已经正确安装了 PDO 扩展。 html
CakePHP 的许但是基于 MIT license 的。这意味着你可以在保持版权声明完整的前提下自由的修改、分发和重发布其源码。 你也能够在商业或封闭源代码的应用程序中包含 CakePHP。 nginx
有两种主要途径用来得到 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 的三种主要安装类型: 开发、产品和高级。
开发安装是设置 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 核心副本。有几个办法能达到这个目的。最简单的是使用 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 应用程序的三个主要组成部分:
这些文件中的每个均可以被放置在文件系统的任意位置,web 根目录例外,它须要经过 web 服务器访问。 你甚至能够将 web 根目录移出 app 文件件,只要你告诉 Cake 它在哪儿。
为了配置 CakePHP 安装,你须要修改以下几个文件。
有三个内容须要编辑: ROOT, APP_DIR, 和 CAKE_CORE_INCLUDE_PATH 。
让咱们经过运行一个示例向你展现实践中是如何进行高级安装的。假设我想设置 CakePHP 按以下方式工做:
对于这种类型的安装,我须要编辑 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 常量做为文件路径的分隔符。这能够防止因使用了错误的分隔符形成的那些文件找不到的问题,而且使代码的可移植性更高。
当 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):
<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 在线文档。
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原生是不支持 .htaccess的。一些插件能够添加这种支持,也能够用 CakePHP 自带的 rewrite 向 IIS 导入 htaccess 规则。步骤以下:
<?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 和路由都会正确的工做。
注意: 如下内容为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');