首页 > PHP开发 > Yii2 > Yii2.0教程入门篇 —— 安装配置
2015
01-27

Yii2.0教程入门篇 —— 安装配置

说明: Yii2.0需要PHP5.4+版本

下载&安装

使用Composer安装

composer global require "fxp/composer-asset-plugin:1.0.0-beta4"
composer create-project --prefer-dist yiisoft/yii2-app-basic basic

下载文件安装

下载地址:http://www.yiiframework.com/download/

或者从github下载最新版本:https://github.com/yiisoft/yii2

解压下载到的压缩文件,进入apps/basic/config/web.php,修改cookieValidationKey:

// !!! insert a secret key in the following (if it is empty) - this is required by cookie   
validation 'cookieValidationKey' => 'enter your secret key here',

验证安装:

http://localhost/basic/web/index.php

(PS:假定使用basic版本)

如果安装成功的话,出现如下界面:

yii2-start-app-installed

否则进入basic目录,运行如下命令:

php requirements.php

查看是否满足系统需求。

配置web服务器:

Apache
DocumentRoot "path/to/basic/web"

<Directory "path/to/basic/web">
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php
</Directory>
Nginx
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 main;
    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?$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.conf;
        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;
    }
}

编程技巧