最新消息:觉得本站不错的话 记得收藏哦 博客内某些功能仅供测试 讨论群:135931704 快养不起小站了 各位有闲钱就打赏下把 My Email weicots#gmail.com Please replace # with @

安装 yii2 (Yii Framework 2)框架

YII ajiang-tuzi 5556浏览

通过 Composer 安装

如果还没有安装 Composer,你可以按 getcomposer.org 中的方法安装。 在 Linux 和 Mac OS X 中可以运行如下命令:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

在 Windows 中,你需要下载并运行 Composer-Setup.exe。
如果遇到任何问题或者想更深入地学习 Composer, 请参考 Composer 文档(英文),Composer 中文。
如果你已经安装有 Composer 请确保使用的是最新版本, 你可以用 composer self-update 命令更新 Composer 为最新版本。
Composer 安装后,切换到一个可通过 Web 访问的目录,执行如下命令即可安装 Yii :

composer global require "fxp/composer-asset-plugin:^1.2.0"  
composer create-project --prefer-dist yiisoft/yii2-app-basic basic
composer global require “fxp/composer-asset-plugin:^1.2.0″ 此包处于更新中

第一条命令安装 Composer asset plugin, 它是通过 Composer 管理 bower 和 npm 包所必须的,此命令全局生效,一劳永逸。 第二条命令会将 Yii 安装在名为 basic 的目录中,你也可以随便选择其他名称。

Note: 在安装过程中 Composer 可能会询问你 GitHub 账户的登录信息,因为可能在使用中超过了 GitHub API (对匿名用户的)使用限制。因为 Composer 需要为所有扩展包从 GitHub 中获取大量信息,所以超限非常正常。(译注:也意味着作为程序猿没有 GitHub 账号,就真不能愉快地玩耍了)登陆 GitHub 之后可以得到更高的 API 限额,这样 Composer 才能正常运行。更多细节请参考 Composer 文档(该段 Composer中文文档期待您的参与)。

Token获得方式 :https://help.github.com/articles/creating-an-access-token-for-command-line-use/

Tip: 如果你想安装 Yii 的最新开发版本,可以使用以下命令代替, 它添加了一个 stability 选项(中文版):

composer create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic basic
注意,Yii 的开发版(dev 版)不应该用于生产环境中,它可能会破坏运行中的代码。

通过归档文件安装

通过归档文件安装 Yii 包括三个步骤:
从 yiiframework.com 下载归档文件。
将下载的文件解压缩到 Web 目录中。
修改 config/web.php 文件,给 cookieValidationKey 配置项 添加一个密钥(若你通过 Composer 安装,则此步骤会自动完成):

// !!! 在下面插入一段密钥(若为空) - 以供 cookie validation 的需要
'cookieValidationKey' => '在此处输入你的密钥',

其他安装方式

上文介绍了两种安装 Yii 的方法, 安装的同时也会创建一个立即可用的 Web 应用程序。 对于小的项目或用于学习上手,这都是一个不错的起点。
但是其他的安装方式也存在:
如果你只想安装核心框架,然后从零开始构建整个属于你自己的应用程序模版, 可以参考从头构建自定义模版一节的介绍。
如果你要开发一个更复杂的应用,可以更好地适用于团队开发环境的, 可以考虑安装高级应用模版。
验证安装的结果

After installation is done, either configure your web server (see next section) or use the built-in PHP web server by running the following console command while in the project web directory:
php yii serve
Note: By default the HTTP-server will listen to port 8080. However if that port is already in use or you wish to serve multiple applications this way, you might want to specify what port to use. Just add the --port argument:
php yii serve --port=8888

安装完成后,就可以使用浏览器通过如下 URL 访问刚安装完的 Yii 应用了:

http://localhost:8080/
Yii 安装成功

你应该可以在浏览器中看到如上所示的 “Congratulations!” 页面。如果没有, 请通过以下任意一种方式,检查当前 PHP 环境是否满足 Yii 最基本需求:
通过浏览器访问 URL http://localhost/basic/requirements.php
执行如下命令:

cd basic
php requirements.php

你需要配置好 PHP 安装环境,使其符合 Yii 的最小需求。主要是需要 PHP 5.4 以上版本。 如果应用需要用到数据库,那还要安装 PDO PHP 扩展 和相应的数据库驱动(例如访问 MySQL 数据库所需的 pdo_mysql)。

配置 Web 服务器

Info: 如果你现在只是要试用 Yii 而不是将其部署到生产环境中, 本小节可以跳过。
通过上述方法安装的应用程序在 Windows,Max OS X, Linux 中的 Apache HTTP 服务器 或 Nginx HTTP 服务器且PHP版本为5.4或更高都可以直接运行。 Yii 2.0 也兼容 Facebook 公司的 HHVM, 由于 HHVM 和标准 PHP 在边界案例上有些地方略有不同,在使用 HHVM 时需稍作处理。
在生产环境的服务器上,你可能会想配置服务器让应用程序可以通过 URL http://www.example.com/index.php 访问而不是 http://www.example.com/basic/web/index.php。 这种配置需要将 Web 服务器的文档根目录指向 basic/web 目录。 可能你还会想隐藏掉 URL 中的 index.php,具体细节在 URL 解析和生成一章中有介绍, 你将学到如何配置 Apache 或 Nginx 服务器实现这些目标。

Info: 将 basic/web 设置为文档根目录,可以防止终端用户访问 basic/web 相邻目录中 的私有应用代码和敏感数据文件。 禁止对其他目录的访问是一个不错的安全改进。
Info: 如果你的应用程序将来要运行在共享虚拟主机环境中, 没有修改其 Web 服务器配置的权限,你依然可以通过调整应用的结构来提升安全性。 详情请参考共享主机环境 一章。
推荐使用的 Apache 配置
在 Apache 的 httpd.conf 文件或在一个虚拟主机配置文件中使用如下配置。 注意,你应该将 path/to/basic/web 替换为实际的 basic/web 目录。

# 设置文档根目录为 “basic/web”
DocumentRoot "path/to/basic/web"
<Directory "path/to/basic/web">
    # 开启 mod_rewrite 用于美化 URL 功能的支持(译注:对应 pretty URL 选项)
    RewriteEngine on
    # 如果请求的是真实存在的文件或目录,直接访问
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # 如果请求的不是真实文件或目录,分发请求至 index.php
    RewriteRule . index.php

    # ...其它设置...
</Directory>

推荐使用的 Nginx 配置
为了使用 Nginx,你应该已经将 PHP 安装为 FPM SAPI 了。 使用如下 Nginx 配置,将 path/to/basic/web 替换为实际的 basic/web 目录, mysite.local 替换为实际的主机名以提供服务。

server {
    charset utf-8;
    client_max_body_size 128M;

    listen 80; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name mysite.local;
    root        /path/to/basic/web;
    index       index.php;

    access_log  /path/to/basic/log/access.log;
    error_log   /path/to/basic/log/error.log;

    location / {
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # uncomment to avoid processing of calls to non-existing static files by Yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass   127.0.0.1:9000;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $uri =404;
    }

    location ~ /\.(ht|svn|git) {
        deny all;
    }
}

使用该配置时,你还应该在 php.ini 文件中设置 cgi.fix_pathinfo=0 , 能避免掉很多不必要的 stat() 系统调用。
还要注意当运行一个 HTTPS 服务器时,需要添加 fastcgi_param HTTPS on; 一行, 这样 Yii 才能正确地判断连接是否安全。

安装记录

//安转 composer
#  curl -s http://getcomposer.org/installer | php
All settings correct for using Composer
Downloading 1.2.1...
Download failed: file_get_contents(https://getcomposer.org/download/1.2.1/compos
er.phar.sig): failed to open stream: HTTP request failed!
Downloading 1.2.1...

Composer successfully installed to: D:\xmpp\htdocs\yii\composer.phar
Use it: php composer.phar

//composer 帮助信息
# php composer.phar -h
Usage:
  help [options] [--] [<command_name>]

Arguments:
  command                        The command to execute
  command_name                   The command name [default: "help"]

Options:
      --xml                      To output help as XML
      --format=FORMAT            The output format (txt, xml, json, or md) [defa
ult: "txt"]
      --raw                      To output raw command help
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as workin
g directory.
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for norma
l output, 2 for more verbose output and 3 for debug

Help:
  The help command displays help for a given command:

    php composer.phar help list

  You can also output the help in other formats by using the --format option:

    php composer.phar help --format=xml list

  To display the list of available commands, please use the list command.
//================================以下内容可能被墙 建议使用全局代理=====================================//
//安装 fxp/composer-asset-plugin:^1.2.0
# php composer.phar global require "fxp/composer-asset-plugin:^1.2.0"
Changed current directory to C:/Users/Administrator/AppData/Roaming/Composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing fxp/composer-asset-plugin (v1.2.1)
    Downloading: Failed
    Downloading: 100%

Writing lock file
Generating autoload files


# php composer.phar create-project --prefer-dist yiisoft/yii2-app-basic basic
Installing yiisoft/yii2-app-basic (2.0.9)
  - Installing yiisoft/yii2-app-basic (2.0.9)
    Downloading: 100%

Created project in basic
Loading composer repositories with package information
Updating dependencies (including require-dev)
Reading bower.json of bower-asset/jquery.inputmask (3.1.56)
Could not fetch https://api.github.com/repos/RobinHerbots/Inputmask/commits/4551
607fef63fd4bcd675479a8c347b668b915eb, please create a GitHub OAuth token to go o
ver the API rate limit
Head to https://github.com/settings/tokens/new?scopes=repo&description=Composer+
on+CN-20160707PGJG+2016-10-09+0657
to retrieve a token. It will be stored in "C:/Users/Administrator/AppData/Roamin
g/Composer/auth.json" for future use by Composer.
Token (hidden):
//Token获得方式 :https://help.github.com/articles/creating-an-access-token-for-command-line-use/
Token stored successfully.
  - Installing yiisoft/yii2-composer (2.0.4)
    Downloading: 100%

  - Installing swiftmailer/swiftmailer (v5.4.3)
    Downloading: 100%

  - Installing bower-asset/jquery (2.2.4)
    Downloading: 100%

  - Installing bower-asset/yii2-pjax (v2.0.6)
    Downloading: 100%

  - Installing bower-asset/punycode (v1.3.2)
    Downloading: 100%

  - Installing bower-asset/jquery.inputmask (3.2.7)
    Downloading: 100%

  - Installing cebe/markdown (1.1.1)
    Downloading: 100%

  - Installing ezyang/htmlpurifier (v4.8.0)
    Downloading: 100%

  - Installing yiisoft/yii2 (2.0.9)
    Downloading: 100%

  - Installing yiisoft/yii2-swiftmailer (2.0.6)
    Downloading: 100%

  - Installing yiisoft/yii2-codeception (2.0.5)
    Downloading: 100%

  - Installing bower-asset/bootstrap (v3.3.7)
    Downloading: 100%

  - Installing yiisoft/yii2-bootstrap (2.0.6)
    Downloading: 100%

  - Installing yiisoft/yii2-debug (2.0.6)
    Downloading: 100%

  - Installing bower-asset/typeahead.js (v0.11.1)
    Downloading: 100%

  - Installing phpspec/php-diff (v1.1.0)
    Downloading: 100%

  - Installing yiisoft/yii2-gii (2.0.5)
    Downloading: 100%

  - Installing fzaninotto/faker (v1.6.0)
    Downloading: 100%

  - Installing yiisoft/yii2-faker (2.0.3)
    Downloading: 100%

Writing lock file
Generating autoload files
> yii\composer\Installer::postCreateProject
chmod('runtime', 0777)...done.
chmod('web/assets', 0777)...done.
chmod('yii', 0755)...done.

参考与引用
yii 2 学习笔记 基础
官方Yii 2.0 安装手册

转载请注明:(●--●) Hello.My Weicot » 安装 yii2 (Yii Framework 2)框架

蜀ICP备15020253号-1