Loading

Linux下Hexo+GitHub搭建个人博客

配置信息

[root@iZwz9c74ta983j746ynevpZ ~]# uname -a
Linux iZwz9c74ta983j746ynevpZ 3.10.0-1127.10.1.el7.x86_64 #1 SMP Wed Jun 3 14:28:03 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

更新源(可略过)

## 备份原 源文件
[root@iZwz9c74ta983j746ynevpZ ~]# mv /etc/yum.repos.d /etc/yum.repos.d.bak
[root@iZwz9c74ta983j746ynevpZr ~]# mkdir /etc/yum.repos.d

## 阿里云
[root@iZwz9c74ta983j746ynevpZ ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo --no-check-certificate

[root@iZwz9c74ta983j746ynevpZ ~]# yum clean all

[root@iZwz9c74ta983j746ynevpZ ~]# yum makecache

[root@iZwz9c74ta983j746ynevpZ ~]# yum update -y

Hexo搭建

安装git

[root@iZwz9c74ta983j746ynevpZ ~]# yum install -y git

node.js安装

node.js安装包下载

网址: http://nodejs.cn/download/

阿里云镜像: https://npm.taobao.org/mirrors/node

下载对应二进制安装包:

[root@iZwz9c74ta983j746ynevpZ ~]# wget https://npm.taobao.org/mirrors/node/v14.6.0/node-v14.6.0-linux-x64.tar.gz --no-check-certificate

node.js安装部署

[root@iZwz9c74ta983j746ynevpZ ~]# tar -zxf node-v14.6.0-linux-x64.tar.gz 
[root@iZwz9c74ta983j746ynevpZ ~]# mv node-v14.6.0-linux-x64 /opt/nodejs

# 创建软连接
[root@iZwz9c74ta983j746ynevpZ ~]# ln -s /opt/nodejs/bin/node /usr/local/bin/node 
[root@iZwz9c74ta983j746ynevpZ ~]# ln -s /opt/nodejs/bin/npm /usr/local/bin/npm 

Hexo安装部署

Hexo安装

[root@iZwz9c74ta983j746ynevpZ ~]# npm install hexo-cli -g
/opt/nodejs/bin/hexo -> /opt/nodejs/lib/node_modules/hexo-cli/bin/hexo
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.1.2 (node_modules/hexo-cli/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ hexo-cli@4.0.0
added 61 packages from 315 contributors in 46.255s

若安装hexo报错如下:

[root@iZwz9c74ta983j746ynevpZ ~]# npm install hexo-cli -g
npm ERR! code CERT_NOT_YET_VALID
npm ERR! errno CERT_NOT_YET_VALID
npm ERR! request to https://registry.npmjs.org/hexo-cli failed, reason: certificate is not yet valid

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2010-03-31T20_55_08_156Z-debug.log
[root@iZwz9c74ta983j746ynevpZ ~]# npm install hexo-cli -g --no-check-certificate
npm ERR! code CERT_NOT_YET_VALID
npm ERR! errno CERT_NOT_YET_VALID
npm ERR! request to https://registry.npmjs.org/hexo-cli failed, reason: certificate is not yet valid

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2010-03-31T20_55_35_518Z-debug.log

原因是https的自签名失败,临时解决办法:关闭ssl

[root@iZwz9c74ta983j746ynevpZ ~]# npm config set strict-ssl false

Hexo部署

将 hexo 命令添加到全局,采用软连接方式。

[root@iZwz9c74ta983j746ynevpZ ~]# ln -s /opt/nodejs/lib/node_modules/hexo-cli/bin/hexo /usr/local/bin/hexo

Hexo博客环境部署

初始化Hexo博客根目录

强烈建议先换npm源!!!见:更换npm源

[root@iZwz9c74ta983j746ynevpZ ~]# mkdir /var/hexoblog
[root@iZwz9c74ta983j746ynevpZ ~]# hexo init /var/hexoblog/
INFO  Cloning hexo-starter https://github.com/hexojs/hexo-starter.git
Submodule 'themes/landscape' (https://github.com/hexojs/hexo-theme-landscape.git) registered for path 'themes/landscape'
Cloning into 'themes/landscape'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 1071 (delta 1), reused 5 (delta 1), pack-reused 1063
Receiving objects: 100% (1071/1071), 3.22 MiB | 12.00 KiB/s, done.
Resolving deltas: 100% (586/586), done.
Submodule path 'themes/landscape': checked out '73a23c51f8487cfcd7c6deec96ccc7543960d350'
INFO  Install dependencies
[ .................] - fetchMetadata: sill pacote range manifest for kind-of@^3.0.2 fetched in 386ms
[ .................] - fetchMetadata: sill pacote range manifest for kind-of@^3.0.2 fetched in 386ms
[ .................] - fetchMetadata: sill pacote range manifest for kind-of@^3.0.2 fetched in 386ms
[ .................] - fetchMetadata: sill pacote range manifest for kind-of@^3.0.2 fetched in 386ms
[ .................] - fetchMetadata: sill pacote range manifest for kind-of@^3.0.2 fetched in 386ms
[ .................] - fetchMetadata: sill pacote range manifest for kind-of@^3.0.2 fetched in 386ms

卡住在这......,猜测是npm源问题,更换国内源

更换npm源

## 查看 默认源
[root@iZwz9c74ta983j746ynevpZ hexoblog]# npm config get registry
https://registry.npmjs.org/

## 更换阿里源
[root@iZwz9c74ta983j746ynevpZ hexoblog]# npm config set registry https://registry.npm.taobao.org
[root@iZwz9c74ta983j746ynevpZ hexoblog]# npm config get registry
https://registry.npm.taobao.org/

若init失败,须清空文件夹下内容,ls -a 查看还有一个影藏文件,再次初始化。

[root@iZwz9c74ta983j746ynevpZ hexoblog]# rm ./* -rf
[root@iZwz9c74ta983j746ynevpZ hexoblog]# ls -a
.  ..  .gitignore
[root@iZwz9c74ta983j746ynevpZ hexoblog]# rm .gitignore -f

启动环境

生成静态文件

[root@iZwz9c74ta983j746ynevpZ hexoblog]# hexo g
[root@iZwz9c74ta983j746ynevpZ hexoblog]# ls -l 
total 132
-rw-r--r--   1 root root  2367 Jul 28 15:54 _config.yml
-rw-r--r--   1 root root 25111 Jul 28 15:59 db.json
drwxr-xr-x 189 root root  4096 Jul 28 15:54 node_modules
-rw-r--r--   1 root root   581 Jul 28 15:59 package.json
-rw-r--r--   1 root root 74694 Jul 28 15:54 package-lock.json
drwxr-xr-x   7 root root  4096 Jul 28 15:59 public		## 存放生成的静态文件
drwxr-xr-x   2 root root  4096 Jul 28 15:54 scaffolds
drwxr-xr-x   3 root root  4096 Jul 28 15:54 source
drwxr-xr-x   3 root root  4096 Jul 28 15:54 themes

启动服务

[root@iZwz9c74ta983j746ynevpZ hexoblog]# hexo s
INFO  Start processing
INFO  Hexo is running at http://localhost:4000 . Press Ctrl+C to stop.

浏览器访问

http://localhost:4000
或
http://服务器IP地址:4000/

GitHub配置

创建github.io仓储

新建仓库,仓库名为:your_github_user_name.github.io

GitHub Pages(默认已开启)

选择主题

浏览器访问

https://your_github_user_name.github.io

添加SSH keys(已有忽略)

# 生成SSH Key,等待输入按下 Enter 回车键三次
ssh-keygen -t rsa  

# 复制公钥 添加到 github
cat ~/.ssh/id_rsa.pub

测试GitHub连接

[root@iZwz9c74ta983j746ynevpZ hexoblog]# ssh -T git@github.com
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Hi JakeLin0fly! You've successfully authenticated, but GitHub does not provide shell access.

设置Github账户信息

git config --global user.name "GitHub用户名" 
git config --global user.email "GitHub邮箱"

Hexo部署到GitHub Pages

Hexo _config.yml修改

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type: git
  repo: git仓库地址
  branch: master

同步到GitHub

需要安装插件 hexo-deployer-git

npm install --save hexo-deployer-git

Hexo命令,详细命令参考:https://hexo.io/docs/commands

hexo cl	## 删除 public/  db.json
hexo g	## 构建项目 主要是生成 public/  db.json
hexo d	## 部署到配置文件中设置的仓库
posted @ 2021-01-25 13:41  JakeLin  阅读(262)  评论(0编辑  收藏  举报