Cowman's Gitbook

Gitbook 安裝

  • 環境
    • Ubuntu 14.04 x64
  • 安裝 Node.js

    • 安裝官方源穩定的版本

      sudo apt-get update
      sudo apt-get install nodejs 
      sudo apt-get install npm
      
    • 安裝PPA(personal package archive)上的版本

      curl -sL https://deb.nodesource.com/setup | sudo bash -
      sudo apt-get install nodejs
      sudo apt-get install build-essential
      
  • 使用 Global 安裝 gitbook

    sudo npm install gitbook -g
    gitbook@1.5.0 /usr/lib/node_modules/gitbook
    ├── commander@2.2.0
    ├── gitbook-plugin-mathjax@0.0.6
    ├── gitbook-plugin-livereload@0.0.1
    ├── gitbook-plugin@0.0.2
    ├── gitbook-plugin-quizzes@1.0.0
    ├── kramed-text-renderer@0.2.1
    ├── graceful-fs@3.0.2
    ├── tmp@0.0.23
    ├── q@1.0.1
    ├── kramed@0.4.3
    ├── semver@2.2.1
    ├── resolve@0.6.3
    ├── lodash@2.4.1
    ├── highlight.js@8.3.0
    ├── lunr@0.5.2
    ├── send@0.2.0 (fresh@0.2.4, range-parser@1.0.2, mime@1.2.11, debug@2.1.1)
    ├── fs-extra@0.10.0 (jsonfile@1.2.0, rimraf@2.2.8, ncp@0.5.1, mkdirp@0.5.0)
    ├── fstream-ignore@0.0.7 (inherits@2.0.1, minimatch@0.2.14, fstream@0.1.31)
    ├── gaze@0.5.1 (globule@0.1.0)
    ├── gitbook-plugin-exercises@1.0.0
    ├── tiny-lr-fork@0.0.5 (debug@0.7.4, faye-websocket@0.4.4, qs@0.5.6, noptify@0.0.3)
    ├── swig@1.3.2 (optimist@0.6.1, uglify-js@2.4.16)
    └── npmi@0.1.1 (semver@4.2.0, npm@2.1.18)
    

    這邊記得 Global 一定要使用 sudo (root) 去執行,否則會發生錯誤

    npm install gitbook -g
    npm ERR! fetch failed https://registry.npmjs.org/fstream-ignore/-/fstream-ignore-0.0.7.tgz
    npm ERR! Error: 400 Bad Request
    npm ERR!     at WriteStream.<anonymous> (/usr/lib/node_modules/npm/lib/utils/fetch.js:58:12)
    npm ERR!     at WriteStream.emit (events.js:117:20)
    npm ERR!     at evalmachine.<anonymous>:1610:14
    npm ERR!     at /usr/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:102:5
    npm ERR!     at Object.oncomplete (evalmachine.<anonymous>:108:15)
    npm ERR! If you need help, you may report this *entire* log,
    npm ERR! including the npm and node versions, at:
    npm ERR!     <http://github.com/npm/npm/issues>
    
    npm ERR! System Linux 3.13.0-32-generic
    npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install" "gitbook" "-g"
    npm ERR! cwd /usr
    npm ERR! node -v v0.10.35
    npm ERR! npm -v 1.4.28
    npm ERR! not ok code 0
    
  • 注意事項

    如果安裝的是官方源的版本,gitbook 會被安裝到 "/usr/local/lib/node_modules/gitbook"

    如果安裝的是 PPA 的版本,gitbook 會被安裝到 "/usr/lib/node_modules/gitbook"

    在後續需要修改原始碼的時候會用到

  • 安裝 nodejs-legacy

    避免後續執行 gitbook 的時後發生錯誤 "/usr/bin/env: node: No such file or directory"

    apt-get install nodejs-legacy
    
  • 安裝 Calibre Tools 以便進行輸出電子書檔案

    如果未安裝即執行 gitbook pdf 等輸出電子書檔案時會出現錯誤 "Need to install ebook-convert from Calibre",因此需要先行安裝 Calibre 以便進行後續的輸出動作

    apt-get install Calibre
    

    當執行 gitbook pdf -v 出現 "RuntimeError: X server required. If you are running on a headless machine, use xvfb",所以也需要先安裝 xvfb 避免錯誤

    apt-get install xvfb
    updatedb
    

    同時參考 How to run gitbook on a headless server (make Calibre run in headless server)? 進行修改 node_modules/gitbook/lib/generate/ebook/index.js 檔案

          # Original
          var command = [
              "ebook-convert",
              path.join(that.options.output, "SUMMARY.html"),
              path.join(that.options.output, "index."+format),
              stringUtils.optionsToShellArgs(_options)
          ].join(" ");
    
          # edited
          var command = [
              "xvfb-run ebook-convert",
              path.join(that.options.output, "SUMMARY.html"),
              path.join(that.options.output, "index."+format),
              stringUtils.optionsToShellArgs(_options)
          ].join(" ");
    
  • 設定gitbook upstart service

    這邊是使用 Ubuntu 的 Upstart service 來管理 Gitbook 的服務

    vim /etc/init/gitbook.conf
    
    description "gitbook"
    
    start on started networking
    stop on runlevel [!2345]
    
    env gitbook_home=/usr/gitbook
    env gitbook_logs=/var/log/gitbook
    
    expect fork
    
    respawn
    
    pre-start script
     cd $gitbook_home
     mkdir $gitbook_logs     ||true
    end script
    
    script
     cd $gitbook_home
     gitbook serve   >> $gitbook_logs/gitbook.log 
                   2 >> $gitbook_logs/error.log
    end script
    

    後續便可以使用 service 來管理 Gitbook 的服務

    service gitbook [start | restart | stop]