Yii2安装高级模板

2017-08-20  本文已影响516人  ahcj_11

标签(空格分隔): Yii2


1 使用 Composer 安装高级模板

[ahcj@localhost www]$ composer create-project --prefer-dist yiisoft/yii2-app-advanced baojia

如果还没有安装 Composer,请先安装 Composer

上面命令在 baojia 文件夹中安装高级模板,可以改成自己想要的文件夹。

Composer 借助 asset-packagist 管理 bower 和 npm 包依赖,在早先版本中使用 asset-plugin 来管理,但是她太慢了。

2 初始化应用

安装完成后,必须先初始化应用。

2.1 执行 init 程序

[ahcj@localhost baojia]$ ./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

如果使用脚本管理,可以在 非交互模式中使用。

path/to/php-bin/php /path/to/yii-application/init --env=Production --overwrite=All

2.2 修改数据库配置

创建数据库 baojia

MariaDB [baojia]> create database baojia;

修改 common/config/main-local.php 配置文件中的 components['db'] 参数, components['db'] => 'baojia'

<?php
return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=baojia',
            'username' => '***',
            'password' => '***',
            'charset' => 'utf8',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
    ],
];

2.3 执行数据迁移命令

[ahcj@localhost baojia]$ ./yii migrate
Yii Migration Tool (based on Yii v2.0.12)

Creating migration history table "migration"...Done.
Total 1 new migration to be applied:
    m130524_201442_init

Apply the above migration? (yes|no) [no]:yes
*** applying m130524_201442_init
    > create table {{%user}} ... done (time: 0.065s)
*** applied m130524_201442_init (time: 0.079s)


1 migration was applied.

Migrated up successfully.

2.4 配置虚拟主机

对于前台 /home/ahcj/www/baojia/frontend/web 使用域名 baojia.local
对于后台 /home/ahcj/www/baojia/frontend/web 使用域名 admin.baojia.local

新建 /usr/local/nginx/conf/conf.d/admin.baojia.local.conf

server {
    charset utf-8;
    client_max_body_size 128M;
    
    listen 80;
    
    server_name admin.baojia.local;
    root        /home/ahcj/www/baojia/backend/web/;
    index       index.php;

    access_log   log/admin.baojia.local.access.log;
    error_log    log/admin.baojia.local.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;
    
    # deny accessing php files for the /assets directory
    location ~ ^/assets/.*\.php$ {
       deny all;
    }

    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 ~* /\. {
        deny all;
    }
}

新建 /usr/local/nginx/conf/conf.d/baojia.local.conf

server {
    charset utf-8;
    client_max_body_size 128M;
    
    listen 80;
    
    server_name baojia.local;
    root        /home/ahcj/www/baojia/frontend/web/;
    index       index.php;

    access_log   log/baojia.local.access.log;
    error_log    log/baojia.local.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;
    
    # deny accessing php files for the /assets directory
    location ~ ^/assets/.*\.php$ {
       deny all;
     }

    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 ~* /\. {
        deny all;
    }
}

2.5 修改 hosts 文件

添加以下两行到 /etc/hosts(linux) 或 c:\Windows\System32\Drivers\etc\hosts(windows)

127.0.0.1   baojia.local
127.0.0.1   admin.baojia.local

2.6 重启 nginx,php-fpm

[root@localhost conf.d]# nginx -s reload
[root@localhost conf.d]# service php-fpm restart

现在可以完美访问啦~
前台 http://baojia.local/index.php
后台 http://admin.baojia.local/index.php

2.7 注册新用户并登陆

安装好的应用是没有 user 的,访问 http://baojia.local/index.php?r=site%2Fsignup, 注册新用户,接下来就可以登陆啦。

火狐截图_2017-08-20T05-57-43.639Z.png 火狐截图_2017-08-20T05-59-17.281Z.png
上一篇下一篇

猜你喜欢

热点阅读