Hexo+Next(1)安装使用

Hexo下载与安装

前置条件

需要先安装gitnpm,可以选用最新的版本,具体的安装过程就不用多说了。

安装hexo-cli

hexonpm里面的一个模块,可以直接通过npm安装hexo-cli

1
$ npm install -g hexo-cli

再在terminal里面输入hexo,验证安装完毕。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Usage: hexo <command>

Commands:
help Get help on a command.
init Create a new Hexo folder.
version Display version information.

Global Options:
--config Specify config file instead of using _config.yml
--cwd Specify the CWD
--debug Display all verbose messages in the terminal
--draft Display draft posts
--safe Disable all plugins and scripts
--silent Hide output on console

For more help, you can use 'hexo help [command]' for the detailed information
or you can check the docs: http://hexo.io/docs/

没有Admin权限

使用-g命令可能需要admin权限,如果你的电脑没有admin权限,可以去掉-g,使用npm install hexo-cli

此时这个module就会安装到当前目录,比如/home/shuyi/目录执行这个指令,会得到/home/shuyi/node_modules文件夹和/home/shuyi/package-lock.json文件。

然后把/home/shuyi/node_modules/.bin添加到系统的路径里面,这样在terminal执行hexo命令的时候就能找到/home/shuyi/node_modules/.bin/hexo命令了。

配置代理

如果想使用proxy进行代理,可以在执行npm命令之前,给terminal设置代理set HTTPS_PROXY="http://proxyUrl:port",但是如果新开了一个terminal,需要重新设置代理。

如果想每次打开terminal都让代理自动配置好,windows可以修改环境变量,linux系统在.bashrc文件内设置代理。

禁止运行脚本的错误(windows)

如果在安装hexo-cli之后运行hexo触弦禁止运行脚本的错误,比如下面这种

1
2
3
PS D:\workSpace\blog\hexo-blog> hexo
hexo : 无法加载文件 C:\Users\user\AppData\Roaming\npm\hexo.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 a
bout_Execution_Policies。

这是PowerShell执行策略的问题,防止你执行不受信任的脚本。

先查询执行的策略,发现是限制执行。

1
2
PS D:\workSpace\blog\hexo-blog> get-ExecutionPolicy
Restricted

如果想绕过这个限制,可以直接使用windows的命令行cmd来执行命令。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
C:\Users\long>hexo
Usage: hexo <command>

Commands:
help Get help on a command.
init Create a new Hexo folder.
version Display version information.

Global Options:
--config Specify config file instead of using _config.yml
--cwd Specify the CWD
--debug Display all verbose messages in the terminal
--draft Display draft posts
--safe Disable all plugins and scripts
--silent Hide output on console

For more help, you can use 'hexo help [command]' for the detailed information
or you can check the docs: http://hexo.io/docs/

或者可以直接使用管理员身份修改PowerShell的策略,允许执行脚本。

1
2
3
4
5
6
PS C:\Windows\system32> set-ExecutionPolicy RemoteSigned

执行策略更改
执行策略可帮助你防止执行不信任的脚本。更改执行策略可能会产生安全风险,如 https:/go.microsoft.com/fwlink/?LinkID=135170
中的 about_Execution_Policies 帮助主题所述。是否要更改执行策略?
[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暂停(S) [?] 帮助 (默认值为“N”): Y

配置NEXT主题

创建博客

先使用hexo命令创建一个hexo的文件夹,以后所有的修改都在这个文件夹里面。

1
2
3
4
$ hexo init <hexo-site>
$ cd <hexo-site>
$ ls
_config.yml node_modules package-lock.json package.json scaffolds source themes

安装next主题

1
2
$ cd hexo-site
$ npm install hexo-theme-next

此时只是安装了next主题到仓库里面,并没有使用,还需要修改配置文件

修改配置

修改_config_yml文件,在theme字段设置为next。

1
theme: next

此时next主题就配置好了,启动之后就是next主题的博客了。

启动和部署

本地启动

1
$ hexo s --debug

s代表server,表示启动服务,--debug代表启动debug模式,可以看到启动的过程。

本地启动的时候,如果对文件进行了修改,比如编辑文字,不用重新启动,只用保存一下,刷新页面,最新的编辑就自动同步了。

如果需要修改启动端口,不想用4000,可以加上-p port指令

1
$ hexo s --debug -p 4001

image

生成制品

将项目编译生成可以部署的web前端文件。

1
$ hexo g

如果需要生成后直接部署,可以加上-d指令。

1
$ hexo g -d

在每次生成之前,可以用clean命令清理一下缓存文件db.json和以前生成的文件public

部署

hexo有很多用于部署的插件,可以部署到Github、Git还有Heroku等平台。

因为我本身有一个服务器,但是不想那么麻烦创建git用户,决定采用SFTP部署。

首先需要安装SFTP的插件。

1
$ npm install hexo-deployer-sftp --save

在_config.yml文件内配置SFTP参数,可以直接用ssh登录使用的参数。

1
2
3
4
5
6
7
8
9
10
deploy:
type: sftp
host: <host>
user: <user>
pass: <password> # 如果用public key登录,可以不用这个
remotePath: [remote path] # 推送的服务器上面的路径
port: [port] # 可以不填
privateKey: [path/to/privateKey] # ssh私钥的目录地址
passphrase: [passphrase] # 如果没有,可以不填
agent: [path/to/agent/socket] # ssh套接字的目录地址,可以不填

配置完成之后,就可以使用下面命令直接部署了。

1
$ hexo d