Yii2安裝高級模板

標簽(空格分隔): 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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容