为何使用Gitee而不是GitHub
目前国内访问GitHub速度慢,还可能被墙,所以Gitee来构建个人博客。Gitee类似国内版的GitHub,访问速度有保证。
环境要求
- Git
- NodeJs 默认你已经安装了Git和NodeJS(推荐使用cnpm)
开始搭建
1. 安装Hexo
代码语言:javascript复制打开 shell 终端,输入命令`npm install -g hexo`
代码语言:javascript复制$ sudo cnpm install -g hexo
Password:
Downloading hexo to /usr/local/lib/node_modules/hexo_tmp
Copying /usr/local/lib/node_modules/hexo_tmp/_hexo@3.7.1@hexo to /usr/local/lib/node_modules/hexo
Installing hexo's dependencies to /usr/local/lib/node_modules/hexo/node_modules
[1/27] abbrev@^1.0.7 installed at node_modules/_abbrev@1.1.1@abbrev
[2/27] archy@^1.0.0 installed at node_modules/_archy@1.0.0@archy
[3/27] hexo-i18n@^0.2.1 installed at node_modules/_hexo-i18n@0.2.1@hexo-i18n
[4/27] js-yaml@^3.6.1 existed at node_modules/_js-yaml@3.12.0@js-yaml
[5/27] bluebird@^3.4.0 installed at node_modules/_bluebird@3.5.1@bluebird
[6/27] chalk@^2.3.1 installed at node_modules/_chalk@2.4.1@chalk
[7/27] hexo-front-matter@^0.2.2 installed at node_modules/_hexo-front-matter@0.2.3@hexo-front-matter
[8/27] minimatch@^3.0.4 installed at node_modules/_minimatch@3.0.4@minimatch
[9/27] pretty-hrtime@^1.0.2 installed at node_modules/_pretty-hrtime@1.0.3@pretty-hrtime
[10/27] hexo-util@^0.6.3 installed at node_modules/_hexo-util@0.6.3@hexo-util
[11/27] hexo-cli@^1.1.0 installed at node_modules/_hexo-cli@1.1.0@hexo-cli
[12/27] resolve@^1.5.0 installed at node_modules/_resolve@1.8.1@resolve
[13/27] strip-indent@^2.0.0 installed at node_modules/_strip-indent@2.0.0@strip-indent
[14/27] strip-ansi@^4.0.0 installed at node_modules/_strip-ansi@4.0.0@strip-ansi
[15/27] text-table@^0.2.0 installed at node_modules/_text-table@0.2.0@text-table
[16/27] tildify@^1.2.0 existed at node_modules/_tildify@1.2.0@tildify
[17/27] titlecase@^1.1.2 installed at node_modules/_titlecase@1.1.2@titlecase
[18/27] moment@^2.19.4 installed at node_modules/_moment@2.22.2@moment
[19/27] moment-timezone@^0.5.14 installed at node_modules/_moment-timezone@0.5.21@moment-timezone
[20/27] hexo-log@^0.2.0 installed at node_modules/_hexo-log@0.2.0@hexo-log
[21/27] swig-extras@0.0.1 installed at node_modules/_swig-extras@0.0.1@swig-extras
[22/27] lodash@^4.17.5 installed at node_modules/_lodash@4.17.10@lodash
[23/27] cheerio@0.22.0 installed at node_modules/_cheerio@0.22.0@cheerio
fsevents@1.2.4 download from binary mirror: {"module_name":"fse","module_path":"./lib/binding/{configuration}/{node_abi}-{platform}-{arch}/","remote_path":"./v{version}/","package_name":"{module_name}-v{version}-{node_abi}-{platform}-{arch}.tar.gz","host":"https://cdn.npm.taobao.org/dist/fsevents"}
[24/27] nunjucks@^3.1.2 installed at node_modules/_nunjucks@3.1.3@nunjucks
[25/27] swig-templates@^2.0.2 installed at node_modules/_swig-templates@2.0.2@swig-templates
[26/27] hexo-fs@^0.2.0 installed at node_modules/_hexo-fs@0.2.3@hexo-fs
[27/27] warehouse@^2.2.0 installed at node_modules/_warehouse@2.2.0@warehouse
execute post install 2 scripts...
[1/2] scripts.postinstall nunjucks@^3.1.2 run "node postinstall-build.js src"
[1/2] scripts.postinstall nunjucks@^3.1.2 finished in 109ms
[2/2] scripts.install hexo-fs@0.2.3 › chokidar@1.7.0 › fsevents@^1.0.0 run "node install"
[fsevents] Success: "/usr/local/lib/node_modules/hexo/node_modules/_fsevents@1.2.4@fsevents/lib/binding/Release/node-v64-darwin-x64/fse.node" already installed
Pass --update-binary to reinstall or --build-from-source to recompile
[2/2] scripts.install hexo-fs@0.2.3 › chokidar@1.7.0 › fsevents@^1.0.0 finished in 306ms
deprecate titlecase@^1.1.2 no longer maintained
Recently updated (since 2018-08-01): 1 packages (detail see file /usr/local/lib/node_modules/hexo/node_modules/.recently_updates.txt)
2018-08-06
→ resolve@1.8.1 › path-parse@^1.0.5(1.0.6) (14:32:36)
All packages installed (280 packages installed from npm registry, used 4s, speed 1.65MB/s, json 236(1.51MB), tarball 5.8MB)
[hexo@3.7.1] link /usr/local/bin/hexo@ -> /usr/local/lib/node_modules/hexo/bin/hexo
注:如果提示权限错误,命令前加sudo
,cnpm
是淘宝的开源镜像,国内访问比npm
快。
2. 初始化Hexo
在你的电脑上创建Hexo
文件夹,在shell终端中切换到Hexo
目录,输入命令 hexo init
$ cd /work/Hexo
/work/Hexo $ hexo init
INFO Cloning hexo-starter to /work/Hexo
Cloning into '/work/Hexo'...
remote: Counting objects: 65, done.
remote: Total 65 (delta 0), reused 0 (delta 0), pack-reused 65
Unpacking objects: 100% (65/65), done.
Submodule 'themes/landscape' (https://github.com/hexojs/hexo-theme-landscape.git) registered for path 'themes/landscape'
Cloning into '/work/Hexo/themes/landscape'...
^Cwarning: Clone succeeded, but checkout failed.
You can inspect what was checked out with 'git status'
and retry the checkout with 'git checkout -f HEAD'
INFO See you again
初始化完成后Hexo
的目录结构:
3. 获取博客主题
在shell中输入命令:git clone https://gitee.com/xiuxiuing/hexo-theme-even themes/even
/work/Hexo $ git clone https://gitee.com/xiuxiuing/hexo-theme-even themes/even
Cloning into 'themes/even'...
remote: Counting objects: 136, done.
remote: Compressing objects: 100% (123/123), done.
remote: Total 136 (delta 4), reused 128 (delta 2)
Receiving objects: 100% (136/136), 264.27 KiB | 556.00 KiB/s, done.
Resolving deltas: 100% (4/4), done.
克隆完成后,在/Hexo/themes
目录下,可以看到 landscape
和even
两个文件夹。
我们所要使用的主题都是放在这个目录下,Hexo默认使用的是landscape
主题,由于第二步Hexo初始化时主题没有clone成功,所以我们这一步克隆了even
主题,接下来会使用even
主题进行演示。
想获取更多主题,可在网站:https://hexo.io/themes/ 选择自己喜欢的主题,按照此步的步骤clone下来。
4. _config.yml对博客进行基础配置
_config.yml文件修改,保存
5. 本地预览博客
编译项目,输入命令:hexo g
运行项目,输入命令:hexo s
$ hexo g
INFO Start processing
INFO Files loaded in 186 ms
INFO Generated: archives/2018/08/index.html
INFO Generated: archives/index.html
INFO Generated: archives/2018/index.html
INFO Generated: index.html
INFO Generated: 2018/08/08/hello-world/index.html
INFO 5 files generated in 242 ms
$ hexo s
INFO Start processing
INFO Hexo is running at http://localhost:4000/. Press Ctrl C to stop.
在浏览器中输入http://localhost:4000/`
就可以看到效果啦
部署博客到Gitee上
1. 创建Gitee账号
到码云:https://gitee.com/ 上申请注册账号,码云类似国内版的GitHub,所以操作界面跟GitHub差不多,多了一些国产化的东西,这里不做更多介绍了,自己研究下。
2. 创建项目
创建完成后,在项目中复制项目地址。
3. 在_config.yml中配置Git
代码语言:javascript复制deploy:
type: git
repo: https://gitee.com/xiuxiuing/blog.git
branch: master
注意:冒号后面一定要有空格,否则不能正确识别。
4. 发布到Gitee
输入命令npm install hexo-deployer-git --save
安装自动部署发布工具
输入命令hexo clean && hexo g && hexo d
发布博客,首次发布需要在shell中输入账号和密码。
5. Gitee Pages设置
在项目的服务中选择Pages选项
选择 master分支,点击 部署/更新
稍等一会儿博客就发布成功啦,访问博客地址:https://xiuxiuing.gitee.io/blog,就可预览在线博客啦!!! 如果博客的样式不对,则需要在_config.yml中配置下博客地址和路径:
代码语言:javascript复制url: https://xiuxiuing.gitee.io/blog/
root: /blog
再执行命令hexo clean && hexo g && hexo d
就可以啦。
至此,我们的博客就搭建完成啦!!!
在/Hexo/source/_posts
目录下就可以写我们的博客啦!!!
个人博客效果参考:https://xiuxiuing.gitee.io/blog/