在android上装hexo

静态网站的一大缺陷就是发布内容很不方便,就算只想修改一小处都必须先配置好所有的运行环境。如果能在手机上安装hexo,随时随地打开hexo写博客是不是很酷~~

安装linux deploy

首先,保证android设备已经root,并安装busybox。最重要的,记得备份!!或者在试验机上先尝试一下。安装系统这种事保不准手机就变砖了orz
开始下载ru.meefik.linuxdeploy,程序包本身很小,只一个7M左右的文件,可以从菜市场或者github下载安装,目前最新版是1.5.1。安装完成后保证存储卡有至少5G的剩余空间,打开无线网络,清理内存。打开应用后点下载图标进行基本设置

选项说明
发行版Debian根据喜好
发行版版本jessie默认的是wheezy,而目前的稳定版是jessie
架构armhf自行判断
镜像地址http://debian.bjtu.edu.cn/debian/我这儿满速下载
镜像大小1536内存大可以设置大点,默认好像是4G
选择组件只保留SSH 服务器手机上用桌面环境太奇葩了
图形界面去掉勾选同上
自定义挂载勾选在挂载点里选择sdcard0

点安装。静心等待,程序首先会在SD卡上开辟镜像区域,在我这里执行得有点慢,不要以为程序卡死了= =。然后就是一长串刷屏,这个等待时间不会很长,视网络而定。安装完成后点启动,如果一切正常,你就拥有了一个arm版的Debian系统。
不需要额外配置,进入系统直接可以联网,另外最好在开启linux前打开手机网络。如果android里开启了http proxy,那么Debian直接就是走代理了。用任意ssh客户端进行通信,手机上推荐用juiceSSH,默认用户名android,密码changeme,下面的配置操作我选择在PC上用putty弄,手机上实在不适合干这个。

安装git以及node.js

首先安装git

1
2
3
4
$ sudo apt-get install git-core
$ git config --global user.email "xxxx@xx.com"
$ git config --global user.name "xxxx"
$ ssh-keygen -t rsa -C "xxxx@xx.com"

然后是nvm,可能不是最新版本

1
$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash

目前最新支持armv7的node.js版本是5.9.0,所以

1
2
3
4
5
$ nvm install v5.9.0
//检查安装结果
$ nvm ls
//使用cnpm加快下载速度
$ npm install -g cnpm --registry=https://registry.npm.taobao.org

最后安装hexo

1
2
3
$ cnpm install -g hexo
//试试hexo有没有安装成功
$ hexo version

安装onedrive-d并同步(方法一)

首先需要一个onedrive空间,用来同步不同设备上的hexo文件夹。linux上目前没办法指定同步的文件夹,所以强烈建议注册一个新账号。如果可以,立刻开始同步你电脑上的hexo文件夹,应为这个过程实在太慢了(起码我这里是这样)。
以下步骤如果在你的linux系统中已经存在请跳过:
首先安装python环境及依赖,参考github上的readme一般情况是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//安装python3,目前arm版本最新只到3.2.3
android@localhost:~/onedrive-d$ sudo apt-get install python3
//安装python3-pip,大概有几百兆,耐心等待
android@localhost:~/onedrive-d$ sudo apt-get install python3-pip
//安装inotify-tools,用来监视文件夹改变
android@localhost:~/onedrive-d$ sudo apt-get install inotify-tools
//省略安装onedrive-d的步骤,开始配置
android@localhost:~/onedrive-d$ onedrive-pref
Loading configuration ... OK
[2015-07-05 15:43:54,632] DEBUG: thread_mgr: started.
Setting up onedrive-d...
//应用授权,打开网址一路点接受,在下面粘贴最终跳转到的网址
(STEP 1/4) Do you want to authorize sign in with your OneDrive account? [Y/n] y

You will need to visit the OneDrive sign-in page in a browser,
log in and authorize onedrive-d, and then copy and paste the
callback URL, which should start with "https://login.live.com/oauth20_desktop.srf".
The callback URL is the URL where the sign-in page finally goes blank.

Please visit the sign-in URL in your browser:
https://login.live.com/oauth20_authorize.srf?locale=XXXXXXXX

Please paste the callback URL:
https://login.live.com/oauth20_desktop.srf?code=XXXXX
[2015-07-05 15:45:55,813] DEBUG: MainThread: config saved.
onedrive-d has been successfully authorized.
//指定本地文件夹,可以直接按回车跳过
(STEP 2/4) Do you want to specify path to local OneDrive repository? [Y/n] y
Please enter the abs path to sync with your OneDrive (default: /home/android/OneDrive):
[2015-07-05 15:47:23,518] DEBUG: MainThread: config saved.
Path successfully set.
//设定上传设置,回车跳过
(STEP 3/4) Do you want to change the numeric settings? [Y/n] y
How many seconds to wait for before retrying a network failure (current: 10)?

Files larger than what size (in MiB) will be uploaded blocks by blocks? (current: 4.0)?

When a file is uploaded blocks by blocks, what is the block size (in KiB)? (current: 512.0)?
[2015-07-05 15:49:01,715] DEBUG: MainThread: config saved.
//指定本地不同步的文件,回车跳过
(STEP 4/4) Do you want to edit the ignore list file? [Y/n] n
Skipped. You can manually edit "/home/android/.onedrive/ignore_v2.ini" at your convenience.

All steps are finished.
//完成之后开始同步
android@localhost:~/onedrive-d$ onedrive-d start

事实上,如果从空文件夹开始同步,速度慢的无法忍受。最终还是在linux deploy上挂载手机sd卡,手动复制hexo文件夹到Debian中,注意,最好是连着文件属性一起复制。

这种办法的优点是自动同步,缺点是网络差的时候传输数据很便秘。

利用远程git仓库实现同步(方法二 推荐)

参考这篇博文,首先查找除根目录及.deploy_git以外的.git文件夹以及.gitignore文件,删除。不想删一定要保证node_modules以及db.json能够同步。建议手动复制.deploy_git到Debian的hexo文件夹下,否则重新生成的话会在git push的时候全部重新上传。或者执行git init并添加远程仓库地址,同步完成之后再从Debian上clone到本地。切换到Debian上,clone刚刚上传的hexo文件夹,执行hexo init && cnpm i安装模块。注意,配置文件可能被初始化,再次执行git reset --hard还原。然后是安装hexo插件,如果同步时没有保留package.json的话。最后,clone已经部署的博客到.deploy_git文件夹,这里有个bug,可能会导致手动clone下来的仓库push失败。完成后试试hexo s是否能正常运行。

用Git同步的方法好处是出错率低,缺点是每次修改都要执行上传下载的操作。

开始码字

到这里我们已经完成了hexo的安装,接下来cd到hexo目录,执行hexo s看看是否成功。最后打开浏览器,如果一切正常意味着你可以在手机上写博客啦!
第一次执行hexo s -g时间稍长,我的79篇日志共花了42秒。注意,如果使用第二种方法,每次都需要用git提交两次。要预览的话就这样

1
2
3
4
5
6
\\启动本地服务并放到后台
$ hexo s&
\\开始编辑文章
$ vim xxxxx
\\关闭服务
$ kill %1

最后,这篇博文就是通过手机发出的。

参考

-Ubuntu環境下,如何安裝nvm以及nodejs
-Ubuntu+hexo+github搭建个人博客环境及hexo配置
-利用git解决hexo博客多PC间同步问题
-node.js
-hexo