安装
2.1 安装 Composer
在 Windows 中,你需要下载并运行 Composer-Setup.exe; 在 Linux 和 Mac OS X 中可以运行如下命令:
curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer
果遇到任何问题或者想更深入地学习 Composer, 请参考Composer 文档
注意:
- 在安装 Yii 期间,Composer 需要从 Github API 请求很多信息。
- 为 Composer 配置 Github 访问令牌,去创建,点击 Generate new token 按钮,选择 repo 项,点击 Create token 生成
- 生成 token 后,运行命令添加令牌到 Composer 全局配置,<oauthtoken> 替换为你实际的 token
composer config -g github-oauth.github.com <oauthtoken>
- 为 Composer 配置阿里云镜像:
- 全局配置(推荐)
- 所有项目都会使用该镜像地址:
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
- 取消配置:
composer config -g --unset repos.packagist
- 项目配置
- 仅修改当前工程配置,仅当前工程可使用该镜像地址:
composer config repo.packagist composer https://mirrors.aliyun.com/composer/
- 取消配置:
composer config --unset repos.packagist
2.2 安装 Yii2.0 高级模板
安装 Composer 后,您可以通过在 Web 可访问的文件夹下运行以下命令来 安装 Yii 应用程序模板:
composer create-project --prefer-dist yiisoft/yii2-app-advanced myapp
Composer 下载完成后,到 myapp 目录里,修改当前项目 composer.json 配置为阿里云镜像。有VPN代理的可优先使用代理,因为asset-packagist.org 下载 Bower 资源包时会很慢。
"repositories": { "packagist": { "type": "composer", "url": "https://mirrors.aliyun.com/composer/" }, "1": { "type": "composer", "url": "https://asset-packagist.org" } }
2.3 初始化 Yii
myapp 目录下运行 composer install 安装 Yii2.0 依赖。 安装完成后,运行 php init 初始化,选择 0 安装开发环境。
php init Yii Application Initialization Tool v1.0 Which environment do you want the application to be initialized in? [0] Development [1] Production Your choice [0-1, or "q" to quit] 0 Initialize the application under 'Development' environment? [yes|no] yes Start initialization ... generate backend/config/codeception-local.php generate backend/config/main-local.php generate backend/config/params-local.php generate backend/config/test-local.php generate backend/web/index-test.php generate backend/web/index.php generate backend/web/robots.txt generate common/config/codeception-local.php generate common/config/main-local.php generate common/config/params-local.php generate common/config/test-local.php unchanged console/config/main-local.php unchanged console/config/params-local.php unchanged console/config/test-local.php generate frontend/config/codeception-local.php generate frontend/config/main-local.php generate frontend/config/params-local.php generate frontend/config/test-local.php generate frontend/web/index-test.php generate frontend/web/index.php generate frontend/web/robots.txt unchanged yii unchanged yii_test unchanged yii_test.bat generate cookie validation key in backend/config/main-local.php generate cookie validation key in common/config/codeception-local.php generate cookie validation key in frontend/config/main-local.php chmod 0777 backend/runtime chmod 0777 backend/web/assets chmod 0777 console/runtime chmod 0777 frontend/runtime chmod 0777 frontend/web/assets chmod 0755 yii chmod 0755 yii_test ... initialization completed.
2.4 配置 Web 服务器
以 phpStudy 为例在 vhosts-conf 里添加站点
<VirtualHost *:80> DocumentRoot "E:\works\yii\myapp\frontend\web" ServerName yii.my ServerAlias www.yii.my <Directory "E:\works\yii\myapp\frontend\web"> Options FollowSymLinks ExecCGI AllowOverride All Order allow,deny Allow from all Require all granted </Directory> </VirtualHost>
以管理员身份打开 C:\Windows\System32\drivers\etc\hosts 文件,在里面添加域名解析
# Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # # For example: # # 102.54.94.97 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost 127.0.0.1 yii.my 127.0.0.1 www.yii.my
浏览器中打开 http://www.yii.my 看到如下图说明安装成功: