要求
1、PHP 5.4.0及以上
2、composer安装完成
3、有网
安装
在安装完composer后,可以执行以下命令:
composer create-project --prefer-dist --stability=dev yiisoft/yii2-app-advanced yii-application
这个命令是指将yii2安装在当前文件夹的yii-application文件夹中,文件夹的名字可以自定义,然后等着就行。
它使用asset-packagist通过composer管理bower和npm包依赖关系。
提示:安装过程中可能会出现需要token的场景,访问GitHub【generate new token】 新建一个tokne,选择默认新建就行,然后就会得到一个token,然后输入这个值就ok了
安装完成:
准备申请
进入安装的项目目录(yii-application),找到init脚本
执行init脚本
init
选择开发环境(dev)
0
是否确定选择
yes
如图:
以上是开发环境的选择与初始化。
yii2高级版分为前后台,可以直接配置两个域名访问到前后台的web目录
设置Web服务器的文档根目录:
- 用于前端
/path/to/yii-application/frontend/web/
和使用URLhttp://frontend.test/
- 用于后端
/path/to/yii-application/backend/web/
和使用URLhttp://backend.test/
对于Apache,可以是一下内容:
< VirtualHost *:80>
ServerName frontend.test
DocumentRoot “ / path / to / yii-application / frontend / web / ”
< Directory “/ path / to / yii-application / frontend / web /”>
# use mod_rewrite for pretty URL support
RewriteEngine on
#如果目录或文件存在,请直接使用请求
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond % {REQUEST_FILENAME} !-d
#否则将请求转发给index.php
RewriteRule 。 的index.php
#使用的index.php作为索引文件
的DirectoryIndex index.php文件
# ...其他设置...
#阿帕奇2.4
要求的所有批准
## Apache 2.2
#订单允许,拒绝
#允许来自所有
</ Directory >
</ VirtualHost >
< VirtualHost *:80>
ServerName backend.test
DocumentRoot “ / path / to / yii-application / backend / web / ”
< Directory “/ path / to / yii-application / backend / web /”>
# use mod_rewrite for pretty URL support
RewriteEngine on
#如果目录或文件存在,请直接使用请求
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond % {REQUEST_FILENAME} !-d
#否则将请求转发给index.php
RewriteRule 。 的index.php
#使用的index.php作为索引文件
的DirectoryIndex index.php文件
# ...其他设置...
#阿帕奇2.4
要求的所有批准
## Apache 2.2
#订单允许,拒绝
#允许来自所有
</ Directory >
</ VirtualHost >
对于Nginx,可以是以下内容:
服务器 {
charset utf-8;
client_max_body_size 128M ;
听 80 ; ## listen for ipv4
#listen [::]:80 default_server ipv6only = on; ##听ipv6
server_name frontend.test;
root / path / to / yii-application / frontend / web /;
index index.php;
access_log /path/to/yii-application/log/frontend-access.log;
error_log /path/to/yii-application/log/frontend-error.log;
location / {
#将所有不是真实文件的内容重定向到index.php
try_files $ uri $ uri / /index.php $ is_args $ args ;
}
#uncomment
避免处理对Yii #location~ \的不存在的静态文件的调用。(js | css | png | jpg | gif | swf | ico | pdf | mov | fla | zip | rar)$ { #try_files $ uri = 404;
#}
#error_page 404 /404.html;
#deny访问/ assets目录
位置的php文件〜^ / assets /.* \ .php $ {
deny all ;
}
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 ;
}
位置〜* / \。{
全部否认 ;
}
}
服务器 {
charset utf-8;
client_max_body_size 128M ;
听 80 ; ## listen for ipv4
#listen [::]:80 default_server ipv6only = on; ##听ipv6
server_name backend.test;
root / path / to / yii-application / backend / web /;
index index.php;
access_log /path/to/yii-application/log/backend-access.log;
error_log /path/to/yii-application/log/backend-error.log;
location / {
#将所有不是真实文件的内容重定向到index.php
try_files $ uri $ uri / /index.php $ is_args $ args ;
}
#uncomment
避免处理对Yii #location~ \的不存在的静态文件的调用。(js | css | png | jpg | gif | swf | ico | pdf | mov | fla | zip | rar)$ { #try_files $ uri = 404;
#}
#error_page 404 /404.html;
#deny访问/ assets目录
位置的php文件〜^ / assets /.* \ .php $ {
deny all ;
}
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 ;
}
位置〜* / \。{
全部否认 ;
}
}
更改hosts文件以将域指向服务器
Windows:c:\Windows\System32\Drivers\etc\hosts
linux:/etc/hosts
分别添加:
127.0.0.1 frontend.test
127.0.0.1 backend.test
配置项目数据库
首先创建数据库yii-application,然后编辑yii-application\common\config\main-local.php,定义数据库名称,用户名密码等
然后进入项目根目录yii-application执行命令
yii.bat migrate
遇到是否询问输入yes
migrate是yii2y用户数据迁移的指令。执行完这个指令,数据库会出现两个表,如图:
migration表用来记录数据迁移,user表用来存储用户信息。