成功在BAE上部署ghost 5.0

2014-09-06
3 min read

这周摸索着网站的建设,终于在今天成功上线!这里要谢谢ghost中文网和群里的网友,他的博客在这opengiser。他们的帮助太重要了。现在把过程记录下来,共同学习。


  1. 下载安装包 总共需要下载2个东西。
  1. 新建BAE工程 在百度开发云平台的管理控制台下一次点击:开发者服务管理->创建工程->创建->应用引擎-添加部署-创建。这样nodejs的环境就建好了。这里一起把数据库也建立起来。这里我使用的是mysql,点击应用引擎中的扩展服务即可,添加新服务当然使用免费版的咯。

  2. 配置 接下来就是在本地把源码配置好咯。将工程clone下来后,用ghost源码覆盖掉。在本地运行命令npm install,后将下载好的mysql覆盖掉node_modules下原有的。然后打开根目录的config.example.js将database段修改为如下:

      production: {
          database: {
              client: 'mysql',
              connection: {
                  host: 'sqld.duapp.com',
                  port: 4050,
                  user: '#####', //你的ak
                  password: '#####', //你的sk
                  database: '#####',//数据库名
                  charset: 'utf8'
              },
              debug: false
          },
          server: {
              host: '127.0.0.1',
              port: '18080'
          }       
     }
    

    最后再修改根目录的config.example.js。修改启动命令,将"start": “node index"改为"start”: “node index.js”。并去掉依赖dependencies、optionalDependencies、devDependencies这三项。

  3. 修改5.0中与BAE不兼容的部分 按照q友悟道所说需要注释掉core/server/index.js中305行的ghostStartMessages()。原因是这个方法的内部与BAE不兼容。

  4. 图像存储问题 在package.json中的dependencies添加七牛的依赖包,在config.example.js中production和添加:

     qiniu: {
         bucketname: '#####', //七牛云的目录名
         ACCESS_KEY: '#####', //七牛云的ak
         SECRET_KEY: '#####', //七牛云的sk
         root: '/image/',
         prefix: 'http://' //七牛的空间域名
     }
    

最后在core/server/storage做两个操作 + 覆盖index.js文件

        var errors = require('../errors'),
        storage;

        var qiniuConfig  = require('../config/').qiniu;

        function get_storage() {
            // TODO: this is where the check for storage apps should go
            // Local file system is the default
            var storageChoice = qiniuConfig? 'qiniu':'localfilesystem';
            if (storage) {
                return storage;
            }
            try {
                // TODO: determine if storage has all the necessary methods
                storage = require('./' + storageChoice);
            } catch (e) {
                errors.logError(e);
            }
            return storage;
        }
        module.exports.get_storage = get_storage;
+ 并添加一个qiniu.js文件
    
        // # Local File System Image Storage module
        // The (default) module for storing images, using the local file system

        var _       = require('lodash'),
            express = require('express'),
            fs      = require('fs-extra'),
            nodefn  = require('when/node/function'),
            path    = require('path'),
            when    = require('when'),
            config = require('../config'),
            errors  = require('../errors'),
            baseStore   = require('./base'),
            crypto = require('crypto'),
            qiniu        = require('qiniu'),
            qiniuConfig  = config.qiniu,
            qiniuStore;
            qiniu.conf.ACCESS_KEY = qiniuConfig.ACCESS_KEY;
            qiniu.conf.SECRET_KEY = qiniuConfig.SECRET_KEY;
            qiniu.conf.USER_AGENT = 'Ghost 0.4.2';
            var putPolicy = new qiniu.rs.PutPolicy(qiniuConfig.bucketname),
            uptoken = putPolicy.token();
            qiniuStore = _.extend(baseStore, {
            // ### Save
            // Saves the image to storage (the file system)
            // - image is the express image object
            // - returns a promise which ultimately returns the full url to the uploaded image
            'save': function (image) {
                var saved = when.defer(),
                md5sum = crypto.createHash('md5'),
                ext = path.extname(image.name),
                targetDirRoot = qiniuConfig.root,
                targetFilename,
                key,
                extra = new qiniu.io.PutExtra();
                var savedpath = path.join(config.paths.imagesPath, image.name);
                nodefn.call(fs.copy, image.path, savedpath).then(function(){
                    return nodefn.call(fs.readFile, savedpath);
                    }).then(function(data) {
                    md5 = md5sum.update(data).digest('hex');
                    targetFilename = path.join(targetDirRoot, md5.replace(/^(\w{1})(\w{2})(\w+)$/, '$1/$2/$3')) + ext;
                    targetFilename = targetFilename.replace(/\\/g, '/');
                    key = targetFilename.replace(/^\//, '');
                    return nodefn.call(qiniu.io.put, uptoken, key, data, extra);
                    }).then(function () {
                    return nodefn.call(fs.unlink, savedpath).then(function(){
                    return nodefn.call(fs.unlink, image.path);
                    }).otherwise(errors.logError);
                    }).then(function () {
                    // prefix + targetFilename
                    var fullUrl = qiniuConfig.prefix + targetFilename;
                    return saved.resolve(fullUrl);
                    }).otherwise(function (e) {
                    errors.logError(e);
                    return saved.reject(e);
                    });
                    return saved.promise;
              },
              'exists': function (filename) {
                  // fs.exists does not play nicely with nodefn because the callback doesn't have an error argument
                  var done = when.defer();
                  fs.exists(filename, function (exists) {
                      done.resolve(exists);
                  });
                  return done.promise;
              },
              // middleware for serving the files
              'serve': function () {
                  var ONE_HOUR_MS = 60 * 60 * 1000,
                      ONE_YEAR_MS = 365 * 24 * ONE_HOUR_MS;
                  // For some reason send divides the max age number by 1000
                  return express['static'](config.paths.imagesPath, {maxAge: ONE_YEAR_MS});
              }
          });

          module.exports = qiniuStore;

最后注释掉fonts.googleapis相关的字体加载,就可以上传代码、发布咯。
  1. Next 个人觉得需要文章分类归档archive评论区,接下来就是搞定他们了。

!参考学习

  1. 开源GIS人上的ghost5.0部署文章
  2. 中文网教程
  3. BAE新手入门
comments powered by Disqus