在yii2官网,可以看到有两个版本,一个是base template,一个是advanced tempalte,高级模板更加强大一些,初始化后有一个公用的common,三个控制入口,frontend,backend,console,分别对应前台,后台,控制台,很适合线上有一定规模的网站,譬如电商网站,我们用frontend做前端显示,backend做后台,console做一些命令行的数据处理,cron定时处理脚本等,这里,我所要说的是,如何从零安装yii2的高级模板。

composer堪称神器,类似于yum安装,自己解决包依赖,所以,安装的过程中也是用composer来安装。

1.安装composer

  1. curl -sS https://getcomposer.org/installer | php
  2. mv composer.phar /usr/local/bin/composer
  3. composer self-update

第一行为下载composer,具体执行log:

  1. [root@iZ942k2d5ezZ ~]# curl -sS https://getcomposer.org/installer | php
  2. All settings correct for using Composer
  3. Downloading 1.0.2...
  4.  
  5. Composer successfully installed to: /root/composer.phar
  6. Use it: php composer.phar

 

2.初始安装:fxp/composer-asset-plugin

  1. composer global require "fxp/composer-asset-plugin:~1.1.1"

3.安装yii2

  1. composer create-project --prefer-dist yiisoft/yii2-app-advanced yii-application

安装log如下:

  1. [root@iZ942k2d5ezZ test]# composer create-project --prefer-dist yiisoft/yii2-app-advanced yii-application
  2. Running composer as root/super user is highly discouraged as packages, plugins and scripts cannot always be trusted
  3. Installing yiisoft/yii2-app-advanced (2.0.8)
  4. - Installing yiisoft/yii2-app-advanced (2.0.8)
  5. Downloading: 100%
  6.  
  7. Created project in yii-application
  8. Loading composer repositories with package information
  9. Updating dependencies (including require-dev)
  10. - Installing yiisoft/yii2-composer(2.0.4)
  11. Loading from cache
  12.  
  13. - Installing swiftmailer/swiftmailer (v5.4.1)
  14. Loading from cache
  15.  
  16. - Installing bower-asset/jquery (2.2.3)
  17. Loading from cache
  18.  
  19. - Installing bower-asset/yii2-pjax (v2.0.6)
  20. Loading from cache
  21.  
  22. - Installing bower-asset/punycode (v1.3.2)
  23. Loading from cache
  24.  
  25. - Installing bower-asset/jquery.inputmask (3.2.7)
  26. Loading from cache
  27.  
  28. - Installing cebe/markdown (1.1.0)
  29. Loading from cache
  30.  
  31. - Installing ezyang/htmlpurifier (v4.7.0)
  32. Downloading: 100%
  33.  
  34. - Installing yiisoft/yii2 (2.0.8)
  35. Downloading: 100%
  36.  
  37. - Installing yiisoft/yii2-swiftmailer (2.0.5)
  38. Loading from cache
  39.  
  40. - Installing yiisoft/yii2-codeception (2.0.5)
  41. Loading from cache
  42.  
  43. - Installing bower-asset/bootstrap (v3.3.5)
  44. Loading from cache
  45.  
  46. - Installing yiisoft/yii2-bootstrap (2.0.6)
  47. Loading from cache
  48.  
  49. - Installing yiisoft/yii2-debug (2.0.6)
  50. Loading from cache
  51.  
  52. - Installing bower-asset/typeahead.js (v0.11.1)
  53. Loading from cache
  54.  
  55. - Installing phpspec/php-diff (v1.1.0)
  56. Loading from cache
  57.  
  58. - Installing yiisoft/yii2-gii (2.0.5)
  59. Loading from cache
  60.  
  61. - Installing fzaninotto/faker (v1.5.0)
  62. Loading from cache
  63.  
  64. - Installing yiisoft/yii2-faker (2.0.3)
  65. Loading from cache
  66.  
  67. fzaninotto/faker suggests installing ext-intl (*)
  68. Writing lock file
  69. Generating autoload files

 

执行完成上面的,yii2就下载好了,我们需要初始化

4. yii 高级模板初始化

进入yii-application目录(上面用composer安装的目录),执行:

  1. ./init

执行log如下:

  1. [root@iZ942k2d5ezZ yii-application]# ./init
  2. Yii Application Initialization Tool v1.0
  3.  
  4. Which environment do you want the application to be initialized in?
  5.  
  6. [0] Development
  7. [1] Production
  8.  
  9. Your choice [0-1, or "q" to quit] 0
  10.  
  11. Initialize the application under 'Development' environment? [yes|no] yes
  12.  
  13. Start initialization ...
  14.  
  15. generate common/config/main-local.php
  16. generate common/config/params-local.php
  17. generate frontend/config/main-local.php
  18. generate frontend/config/params-local.php
  19. generate frontend/web/index.php
  20. generate frontend/web/index-test.php
  21. generate console/config/main-local.php
  22. generate console/config/params-local.php
  23. generate backend/config/main-local.php
  24. generate backend/config/params-local.php
  25. generate backend/web/index.php
  26. generate backend/web/index-test.php
  27. generate tests/codeception/config/config-local.php
  28. generate yii
  29. generate cookie validation key in backend/config/main-local.php
  30. generate cookie validation key in frontend/config/main-local.php
  31. chmod 0777 backend/runtime
  32. chmod 0777 backend/web/assets
  33. chmod 0777 frontend/runtime
  34. chmod 0777 frontend/web/assets
  35. chmod 0755 yii
  36. chmod 0755 tests/codeception/bin/yii
  37.  
  38. ... initialization completed.
  39.  
  40. [root@iZ942k2d5ezZ yii-application]#

到这里就安装了yii2高级模板的安装,配置nginx

前台地址:frontend/web

后台地址:backend/web

访问的时候,在linux下面需要将web目录下面的assets文件夹设置成可写。

这样就完成了yii2 advanced的安装了