频道栏目
首页 > 资讯 > PHP框架 > 正文

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

16-01-07        来源:[db:作者]  
收藏   我要投稿

安装

 

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 应用程序的三个主要组成部分:

  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');

相关TAG标签
上一篇:CakePHP 2.x CookBook 中文版 第三章 入门
下一篇:CakePHP 2.x CookBook 中文版 第一章 欢迎
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站