Total Pageviews

Wednesday 12 June 2013

Hexo 服务器端的布署以及 与Dropbox 同步

步骤

  1. 安装 LNMP
    我还是喜欢用 LNMP 一键安装包,自己配置 nginx 什么的我还是觉得有点麻烦,而且平时也会用到一些 php。具体安装方法请参考 lnmp.org 的指引-
    http://lnmp.org/install.html。
  2. 安装 git
    用 git 来拖些源文件比较方便:
     yum -y update
     yum install git-core
    
  3. 安装 node
    就我的经验来看,Hexo 用低版本的 node 貌似兼容更好一些,我这里用的是 v0.8.14,基本上照抄 hexo 官方文档了:
     git clone git://github.com/creationix/nvm.git ~/nvm
    
    修改 .bash_profile,在其中加入:
     . ~/nvm/nvm.sh
    
    然后就可以用 nvm 安装 node 了:
     . ~/.bash_profile
     nvm install v0.8.14
     nvm use v0.8.14
     nvm alias default v0.8.14
    
  4. 安装 hexo
    因为各种原因,我还是一直在用老的 0.5.12 版本
     npm install -g hexo@0.5.12
    
  5. 安装和启动 Dropbox
    首先如果你还没有 Dropbox 的账户的话,可以先注册一个.
    因为 Dropbox 默认是安装在 ~ 的,出于安全考虑,我新建了一个 dropbox 用户,把 Dropbox 安装在这个用户的 $HOME 目录下:
     adduser dropbox
     passwd dropbox
    
    这时再打开一个进程登入 dropbox 这个用户进行安装
     cd ~ && wget -O - "https://www.dropbox.com/download?plat=lnx.x86" | tar xzf -
    
    启动:
     ~/.dropbox-dist/dropboxd
    
    首次启动需要验证,会自动提供链接,拷贝到浏览器中,登录你的账户后进行验证。成功后 /home/dropbox 下面就会有 Dropbox 这个文件夹并开始同步了,第一次同步的时间可能会长一些。
    这时可以下载官方提供的 cli 用来方便控制和查看 Dropbox 的状态。
     wget https://www.dropbox.com/download?dl=packages/dropbox.py
     chmod +x ./dropbox.py
     ./dropbox.py help
    
  6. 在 Dropbox 中建立 hexo 站点目录
    这时可以回到 root 用户下了。
     cd /home/dropbox/Dropbox
     hexo init hexo
    
    如果之前的 Dropbox 同步正常的话,这时在本地也会看到 hexo 文件夹了,可以把本地中之前的配置文件、theme 和 _posts 什么的都放进去同步回服务器端。
    Plugin 什么的我也是用的较早的版本:
     npm install hexo-generator-feed@0.0.8 hexo-generator-sitemap@0.0.6 hexo-renderer-discount@0.0.1 --save
    
    然后就可以生成一下试试看:
     hexo generate -w
    
    上面这个命令加入了 -w 的参数(–watch),在生成完静态站点之后 hexo 会继续监视文件的变化(按 Ctrl+C 可以停止监视);这时可以在本地尝试修改一下 _post 中之前的文章并保存,服务器端收到 Dropbox 更新的修改后就会自动重新生成了。
    但是 watch 真的很耗资源,我测试的情况是要吃掉 150MB 的内存,再加上 Dropbox 这个内存大户,512MB 内存也能吃的很干净;另一方面这个版本的 watch 也不是很稳定,经常掉链子,所以我采用的是后面提到的 incrond 的方式。
    此外我也对 hexo 动了点小手术,让它不再输出 console 信息,否则用脚本运行时会报错(好吧,我没找到更好的解决办法=_=)。这需要修改下面这个文件,注释掉其中所有 stdout 和 console.log 的语句:
     vi /root/nvm/v0.8.14/lib/node_modules/hexo/lib/cli/generate.js
    
  7. runhexo.bash 脚本
    建立一个 bash 脚本,它会帮我执行 hexo generate 并用 rsync 的方式把 public 目录的内容同步到 nginx 指向的站点目录里。我把它放在了 /root/ 下:
     touch /root/runhexo.bash
     chmod +x /root/runhexo.bash
    
    脚本内容如下:
     #!/usr/bin/env bash
     exec 200<$0
     flock -n 200 || exit 1
     sleep 10
     . ~/nvm/nvm.sh
     cd /home/dropbox/Dropbox/hexo && hexo generate && rsync -a --delete /home/dropbox/Dropbox/hexo/public/ /home/wwwroot/public/
    
  8. 安装 incrond 及同步
    接下来我们需要一个程序来帮助监控 hexo 文件的变化,因为前面所述的原因,我选择了 incrond
    安装:
    yum install incron
    
    启动:
    service incrond start
    chkconfig incrond on
    
    和 crond 和 crontab 一样,incrond 也可以用 incrontab 来配置,不过它默认是用 vim 编辑,需要改一下:
    echo 'editor = vi' >> /etc/incron.conf
    
    这时就可以用 incrontab -e 来编辑配置了,在其中添加:
    /home/dropbox/Dropbox/hexo/source/_posts/ IN_MOVE /root/runhexo.bash
    
    我只是监视了 _posts 这个文件夹,毕竟平时用的最多的就是更新和编辑文章,当然。
  9. 添加 nginx 配置
    用 lnmp 一键安装包提供的 vhost.sh 添加一个新的虚拟主机,按提示添加域名,修改 /usr/local/nginx/conf/vhost/YOURDOMAIN.conf 文件将其中的 root 指向 public 文件夹 /home/wwwroot/public
    我没有把 root 直接指向 Dropbox 中的 hexo/source/public,虽然这样可能更方便,但出于安全的考虑我还是用 rsync 了。如果无所谓的话,你需要给 public 和它的每个上级修改权限,这样才能避免 403 错误。:
     chmod -R 755 /home/dropbox/Dropbox/hexo/public
     chown www:www /home/dropbox/Dropbox/hexo/public
     chmod +x /home/ /home/dropbox/ /home/dropbox/Dropbox/ /home/dropbox/Dropbox/hexo/
    

不足

  • 因为生成静态需要一定时间,用这个方法通过 Dropbox 修改文件如果太过频繁的话,可能会出现更新不及时的情况。
  • 内存占用还是大了些.
----------------------------------------------------------------------------------
 Dropbox + nginx for Simple Webhosting (free DropPages clone)

When my girlfriend wanted a website for her bakery, I decided to set her up with a custom domain pointing to my VPS with a simple means for her to update it while keeping flexibility. A really cool setup I heard about a few months ago was syncing your dropbox with a web server so you can edit the files locally and they’re automatically updated on your web server. Think of it as the next step in hosting a website in your public folder.
DropPages did everything I wanted it to do, but it wasn’t free, and it meant sharing the site with a third party, so I decided to do it myself! Here’s what I did:
  1. Get two dropbox accounts, one for your computer and one for your server (alternatively you can share the same account between the both of them, but that means an unnecessary backup of your entire dropbox on a web server, which may not be practical).
  2. Share a folder between the two accounts and put an index.html file in it. I’ll call the folder “website.com”.
  3. Install Dropbox via command line on your server (you’ll also want the CLI python script). You should now have a folder on your web server at ~/Dropbox/website.com
  4. Add this to your nginx config file (/etc/nginx/nginx.conf):
    server{
      listen 80;
      server_name website.com;
      root /home/USERNAME/Dropbox/website.com;
      index index.html;
    }
    
    And restart nginx:
    sudo /etc/init.d/nginx restart
    Now here’s where we get into permission nightmares. At this point, if you go to website.com, you’re probably going to see “403: Permission denied.” If you look into the nginx log file (location specified in nginx.conf; for me it’s /var/log/nginx/error.log) you’ll see a whole bunch of:
    2012/02/25 06:18:00 [error] 11037#0: *3 "/root/Dropbox/website.com/index.html" is forbidden (13: Permission denied), client: 123.123.12.123, server: website.com, request: "GET / HTTP/1.1", host: "website.com"
    
    This was really annoying to fix, but after two days of googling I found a solution. `chmod -R 755` and `chown nginx` the website.com directory and make every parent directory executable (security nightmare?): `chmox +x /home /home/USERNAME /home/USERNAME/Dropbox`. This post and these comments helped. Restart nginx again and you should be good to go.
  5. Make sure the dropbox daemon is running with `dropbox.py status` and `dropbox.py start` and you should now be the proud owner of a free DropPages clone。
---------------------------------