bubifengyun 2016-03-06 07:33:37 4961次浏览 1条评论 4 5 1

参考网页

本文用到的代码绝大部分来自 https://github.com/michaelweixi/Yii2Blog
表示由衷地感谢。

申请服务器资源

这一部分,基本全部照抄参考网页的内容,请移步参考网页

转发个网站

假设我们已经做好了一个网站,是yii2.0.6的basic版本,不妨设文件夹如下

zhinengfengrenji
|-web
-|-index.php
|-config
(略)

等等。

做法如下

一、创建config.yaml

参考网页

为了配置打开即进入./web/index.php主页,我们需要如下配置./zhinengfengrenji/config.yaml文件

name: zhinengfengrenji
version: 1

handle:
- directoryindex: ./web/index.php

二、配置mysql

参考网页:

参照上面的网页,假设我们已经导入一个数据库,名字为app_zhinengfengrenji,

对于./zhinengfengrenji/config/web.php中db的配置如下。

    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host='.SAE_MYSQL_HOST_M.';port='.SAE_MYSQL_PORT.';dbname=app_zhinengfengrenji',
  
    'username' => SAE_MYSQL_USER,
    'password' => SAE_MYSQL_PASS,
    'charset' => 'utf8',

三、修改可读写能力

注释掉./zhinengfengrenji/config/web.php中,关于log的部分,我们没办法写入runtime
还有一个./zhinengfengrenji/web/assets/文件夹,没有写权限。解决方案如下。

<?php

$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
	'language' => 'zh-CN',	
	'defaultRoute' => 'post/home',
    'runtimePath' => SAE_TMP_PATH,
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'wxstyle',
        ],
        'assetManager' => [
            'basePath' => SAE_TMP_PATH,
            'baseUrl' => SAE_TMP_PATH,
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // 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,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 0 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                    'logFile' => SAE_TMP_PATH,
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
    ],

    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = [
        'class' => 'yii\debug\Module',
    ];

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
   		'allowedIPs' => ['127.0.0.1', '::1', '192.168.1.*', '192.168.178.20'],
    ];
}

return $config;

解释

  • 'runtimePath' => SAE_TMP_PATH,,其中SAE_TMP_PATH`是一个可写入地址,详情见http://www.sinacloud.com/doc/sae/php/runtime.html
  • 为了让生成的runtimePath, assets, log等可写,就采用了这么一个笨方法,把他们全部放到SAE_TMP_PATH里。
  • assets文件的处理,因为要显示样式,还必须有他,所以这个不可以任性,还必须复制他们。在下一节讲。

四、添加css,js文件

由于上面产生的assets文件夹里的css,js文件无法访问,只好手动添加,很是不爽。于是想到在./assets/AppAsset.php文件里添加。代码如下。

    public $css = [
        'css/site.css',
        'css/bootstrap.css',
    ];
    public $js = [
        'js/yii.js',
        'js/bootstrap.min.js',
        '//ajax.useso.com/ajax/libs/jquery/2.1.1/jquery.min.js',
    ];

其中 yii.js等文件是调试的时候在本地./web/assets/文件夹下产生的,然后复制到./web/js,./web/css文件夹下的。
最后使用git提交这些改变。中间跳过好多内容,有不懂之处,欢迎联系bubifengyun@sina.com

给你个网址

本例子的网址如下:
http://zhinengfengrenji.applinzi.com/

试着做了一个最简单的玩意。期待高手给予指点。谢谢。

最后不得已,改为wordpress直接部署。轻便多了。

觉得很赞
您需要登录后才可以评论。登录 | 立即注册