准备工作。
个人博客如果希望拥有一个属于自己的独特网址,这就需要购买域名。域名可以通过域名供应商购买就可以,(比如:阿里 ) 我自己买了个.top的域名。10年150元不到,一般还是负担起的。。当然也不是非必需的,看个人需求了。
GitHub这个是零成本的,到官网注册个账号就可以使用了。这里我们需要创建一个仓库;
比如我的github账号为 zhangshan,那我的repository的名字如下:
zhangshan.github.io
- Node、git 环境需要准备好。
查看我的版本,不一定非要和我一致,但版本不能太低了。
➜ ~ node -v
v14.14.0
➜ ~ npm -v
6.14.8
➜ ~ git --version
git version 2.24.1 (Apple Git-126)
安装Hexo
➜ ~ npm install -g hexo-cli
在安装过程中出现了个问题,搜索了下网上的解决方案。
....
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/opt/node-v10.16.0-linux-x64/lib/node_modules'
npm ERR! { [Error: EACCES: permission denied, access '/opt/node-v10.16.0-linux-x64/lib/node_modules']
npm ERR! stack:
npm ERR! 'Error: EACCES: permission denied, access \'/opt/node-v10.16.0-linux-x64/lib/node_modules\'',
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
....
解决:
创建目录,用于存放npm 全局安装二进制执行文件
mkdir ~/.npm-global
配置npm以使用新的目录路径
npm config set prefix '~/.npm-global'
将目录加入环境变量中,打开~/.bash_profile文件,添加信息
~ open ~/.bash_profile
# 添加npm的相关信息
export NPM_GLOBAL=~/.npm-global/bin
PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}:$M2_HOME/bin:$NPM_GLOBAL"
export PATH
完整配置截图:注意不要覆盖了你的配置文件。
别忘了执行配置文件的生效更新操作
~ source ~/.bash_profile
然后我们在执行 npm install -g hexo-cli 命令安装即可。
安装完成后,我们需要在本地创建工作空间
# 指定本地文件夹路径:比如 /User/xxx/githbu/hexo/blogs
hexo init /User/xxx/githbu/hexo/blogs
cd /User/xxx/githbu/hexo/blogs
npm install
# 文件夹主要结构:
|--- node_modules
|--- scaffolds
|--- source
|--- _drafts
|--- _posts
|--- themes
|--- _config.yml
|--- package.json
|--- package-lock.json
说明:
- _config.yml是博客网站的配置文件
- source文件夹是存放用户资源
- _post文件是存在markdown文件
- theme文件夹是用来存放主题
Git的相关配置
# 设置全局github的用户名、邮箱地址
git config --global user.name "自己的github用户名"
git config --global user.email "自己的github注册邮箱"
# 创建公匙
# 执行命令后,会让填写相关信息,我们默认回车即可。
ssh-keygen -t rsa -C "你的GitHub注册邮箱"
之后,会在~/.ssh/文件夹下创建密匙,即 ~/.ssh/id_rsa_pub .复制文件下的全部内容
打开github添加ssh kye页面,点击'New SSH key'新建SSH key. 这里Title自己取,key就是~/.ssh/id_rsa_pub文件下的全部内容.
测试配置是否成功。
➜ ~ ssh git@github.com
PTY allocation request failed on channel 0
Hi suBoMan! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
将Hexo静态页面部署到Github
打开自己设置的Hexo的博客文件夹,打开 _config.yml文件,在最后添加以下内容:
# 配置hexo把blog部署到github仓库里
deploy:
type: git
repository: git@github.com:username/username.github.io.git
branch: master
安装部署插件
npm install hexo-deployer-git --save
这时候,输入下面三条命令,完成最终部署.
hexo clean
hexo g
hexo d
➜ zhangshan-github hexo clean
INFO Validating config
➜ zhangshan-github hexo g
INFO Validating config
INFO Start processing
INFO Files loaded in 167 ms
(node:9185) Warning: Accessing non-existent property 'lineno' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:9185) Warning: Accessing non-existent property 'column' of module exports inside circular dependency
(node:9185) Warning: Accessing non-existent property 'filename' of module exports inside circular dependency
(node:9185) Warning: Accessing non-existent property 'lineno' of module exports inside circular dependency
(node:9185) Warning: Accessing non-existent property 'column' of module exports inside circular dependency
(node:9185) Warning: Accessing non-existent property 'filename' of module exports inside circular dependency
INFO Generated: archives/2020/index.html
INFO Generated: archives/index.html
INFO Generated: archives/2020/12/index.html
INFO Generated: fancybox/blank.gif
INFO Generated: index.html
INFO Generated: fancybox/helpers/fancybox_buttons.png
INFO Generated: fancybox/fancybox_loading.gif
INFO Generated: fancybox/fancybox_sprite.png
INFO Generated: fancybox/fancybox_loading@2x.gif
INFO Generated: fancybox/fancybox_overlay.png
INFO Generated: fancybox/fancybox_sprite@2x.png
INFO Generated: js/script.js
INFO Generated: fancybox/jquery.fancybox.css
INFO Generated: fancybox/helpers/jquery.fancybox-buttons.css
INFO Generated: fancybox/helpers/jquery.fancybox-buttons.js
INFO Generated: fancybox/helpers/jquery.fancybox-media.js
INFO Generated: fancybox/helpers/jquery.fancybox-thumbs.js
INFO Generated: fancybox/helpers/jquery.fancybox-thumbs.css
INFO Generated: css/style.css
INFO Generated: 2020/12/04/hello-world/index.html
INFO Generated: css/fonts/fontawesome-webfont.ttf
INFO Generated: css/fonts/FontAwesome.otf
INFO Generated: css/fonts/fontawesome-webfont.woff
INFO Generated: css/fonts/fontawesome-webfont.eot
INFO Generated: fancybox/jquery.fancybox.pack.js
INFO Generated: fancybox/jquery.fancybox.js
INFO Generated: css/images/banner.jpg
INFO Generated: css/fonts/fontawesome-webfont.svg
INFO 28 files generated in 617 ms
➜ zhangshan-github hexo d
INFO Validating config
INFO Deploying: git
INFO Setting up Git deployment...
Initialized empty Git repository in /Users/zhangshan/devDocuments/zhangshan-github/.deploy_git/.git/
[master (root-commit) c239623] First commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 placeholder
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
INFO Copying files from extend dirs...
[master 50cc69e] Site updated: 2020-12-04 14:42:53
29 files changed, 5825 insertions(+)
create mode 100644 2020/12/04/hello-world/index.html
create mode 100644 archives/2020/12/index.html
create mode 100644 archives/2020/index.html
create mode 100644 archives/index.html
create mode 100644 css/fonts/FontAwesome.otf
create mode 100644 css/fonts/fontawesome-webfont.eot
create mode 100644 css/fonts/fontawesome-webfont.svg
create mode 100644 css/fonts/fontawesome-webfont.ttf
create mode 100644 css/fonts/fontawesome-webfont.woff
create mode 100644 css/images/banner.jpg
create mode 100644 css/style.css
create mode 100644 fancybox/blank.gif
create mode 100644 fancybox/fancybox_loading.gif
create mode 100644 fancybox/fancybox_loading@2x.gif
create mode 100644 fancybox/fancybox_overlay.png
create mode 100644 fancybox/fancybox_sprite.png
create mode 100644 fancybox/fancybox_sprite@2x.png
create mode 100644 fancybox/helpers/fancybox_buttons.png
create mode 100644 fancybox/helpers/jquery.fancybox-buttons.css
create mode 100644 fancybox/helpers/jquery.fancybox-buttons.js
create mode 100644 fancybox/helpers/jquery.fancybox-media.js
create mode 100644 fancybox/helpers/jquery.fancybox-thumbs.css
create mode 100644 fancybox/helpers/jquery.fancybox-thumbs.js
create mode 100644 fancybox/jquery.fancybox.css
create mode 100644 fancybox/jquery.fancybox.js
create mode 100644 fancybox/jquery.fancybox.pack.js
create mode 100644 index.html
create mode 100644 js/script.js
delete mode 100644 placeholder
Enumerating objects: 46, done.
Counting objects: 100% (46/46), done.
Delta compression using up to 4 threads
Compressing objects: 100% (36/36), done.
Writing objects: 100% (46/46), 507.68 KiB | 1.67 MiB/s, done.
Total 46 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), done.
remote: This repository moved. Please use the new location:
remote: git@github.com:zhangshan/zhangshan.github.io.git
To github.com:zhangshan/zhangshan.github.io.git
* [new branch] HEAD -> master
Branch 'master' set up to track remote branch 'master' from 'git@github.com:zhangshan/zhangshan.github.io.git'.
INFO Deploy done: git
# 本地访问:
➜ ~ hexo s