为什么要写博客
作为一只程序猿,踩到坑是一件非常正常的事,当我们踩到坑的时候就会花心思去研究它,可能我们能够在当时把问题弄懂并把问题给解决掉。可是过一段时间我们又遇到了同样的坑的时候,难道还要再去 百度 Google 重新搜索一遍吗?这样做效率难免太低了,倒不如在第一次解决问题的时候就把解决方法写到我们的博客了,当我们再一次遇到相同的坑的时候翻一翻我们之前写的博客就能快速的把问题给解决掉,何乐而不为。而且我们学习新技术的时候也可以将当时学到的内容写到我们的博客,再次遇到的时候我们就可以找回当时学习的思路,继续学习。废话不多说,马上开始行动起来,搭建博客!
声明:本文在Windows下进行操作的,Mac以及其它操作系统请做参考 多图警告!
如果你觉得太麻烦,欢迎 Fork/Star 我的 GitHub 但是环境搭建以及怎么使用命令还是要看一看的。
1. 环境搭建
首先需要下载两个东西
- node.js
- git
具体的下载,安装就不用多说了,基本上下载完默认安装即可,安装的路径最好先记住。Git 安装的时候会弹出下面的窗口,我们选择第二个即可。这样我们在Windows的命令窗口也可以进行Git操作了。
Check Version
1.1 有问题看这里
PATH打开我们刚才安装软件的路径,例如我的路径“D:\Program Files\nodejs”、“D:\Program Files\Git”。
复制我们刚才安装的路径,打开计算机>右键单击属性,选择高级系统设置>选择环境变量>双击 PATH >将我们安装的路径追加到变量值之后 !注意分号以及确定保存
这个时候再试一下 node -v 、npm -v 及 git --version 这三个命令,一般都不会有问题的了。
2. 配置 GitHub
2.1 注册 GitHub
先到GitHub官网Sign up(注册)一个账号。
2.2 SSH授权
注册好账号之后我们可以随意的查看其他人的项目,甚至是clone下载,但是要提交代码就必须完成SSH授权,如果可以不用授权就提交代码的话,那么Github岂不是乱了套。
2.2.1.生成SSH key
打开Git Bash,输入ssh-keygen -t rsa然后按三下回车,如下图所示
2.2.2.在GitHub上添加SSH Key
首先点击右上角的倒三角进入Settings
SSH key 添加成功之后,输入ssh -T git@github.com进行测试,如果出现以下提示证明添加成功了。
这一步一般没什么问题,有问题的话留言评论(顺便来一波打赏)就好了。直接进入下一步!
3. 创建 GitHub 仓库
需要特别注意的是,项目名称一定要使用 你的名字 + .github.io
这一步也没什么问题,如果有问题,一定是你没有给我打赏(∩_∩)
4. 设置本地博客的配置
4.1 安装Hexo
在你认为合适的地方创建一个文件夹,然后在文件夹空白处按住 Shift+鼠标右键,然后点击在此处打开命令行窗口。(同样要记住啦,下文中会使用在当前目录打开命令行来代指上述的操作)
在命令行输入npm install -g hexo
如果出现与上图一样的情况的话,就说明你离成功就近在咫尺了。
4.2 初始化Hexo
同样是在命令窗口中,继续输入 hexo init,等待下载好了之后输入 hexo s
这时候我们就可以打开浏览器了,在地址栏中输入 http://localhost:4000/ 我们就可以看到如下图的界面,这个就是我们的博客。没错,我们的博客就这样建好了。不过这个只是我们本地的博客,下面就要考虑怎么把我们的本地博客上传到我们的GitHub上了。
接下来先看一下我们的博客文章放在哪里。打开我们的文件夹下面的source文件夹,你会发现里面有一个_posts文件夹,再进入就会看到一片初始化的文章hello-world.md也就是上图显示在页面的文章。如果我们想新建文章的话,可以通过命令窗口输入hexo new 'filename'我们的文件夹下面就会生成一个新的md文件,然后我们打开编辑就可以了。
4.3 发布博客
首先复制我们的GitHub项目地址,如下图。
deploy 是部署的意思,type: git 就是使用 git 进行部署,repo: github仓库地址
注意:repo 原本是没有的,在最后自己加上就好。冒号之后有一个空格 冒号之后有一个空格 冒号之后有一个空格
接下来回到命令窗口,输入 npm install hexo-deployer-git --save
5. 个性化设置(更换主题)
有木有觉得这个博客的默认主题特别的丑,如果不觉得可以忽略这一步(哈哈)。
这里以我使用的主题为例。
第一步去找我们想要的主题,然后下载下来。我用的是next主题,在命令窗口输入git clone https://github.com/iissnan/hexo-theme-next themes/next
然后打开配置文件,找到 theme 将原来默认的 landscape 替换 next。然后在命令窗口输入 hexo clean 、hexo g 及 hexo s,先看一下本地博客是什么样子,确认好了在输入 hexo d 部署到GitHub
每一个主题都有一个使用文档,next的使用文档为 http://theme-next.iissnan.com/getting-started.html 我们可以为我们的主题修改名字,添加评论等等,具体的你们就自己去研究了。如果你们觉得太麻烦的话,欢迎大家直接 Fork 我的,地址为 https://github.com/AD-feiben/hexo 当然里面的也有大家要修改的地方。
如果文章对你有所帮助,那么请您点一下❤
由于本人水平有限,如有错误,欢迎大家指正。如果你在操作过程中发现一些没有讲到的错误或者问题,欢迎在评论留言,一起探讨,共同学习进步!
有钱的来波赞赏,没钱的帮忙宣传一下(上不了首页!)
网友评论
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in fancybox/jquery.fancybox.css.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in fancybox/jquery.fancybox.js.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in fancybox/jquery.fancybox.pack.js.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in index.html.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in js/script.js.
The file will have its original line endings in your working directory.
On branch master
nothing to commit, working tree clean
bash: /dev/tty: No such device or address
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'https://github.com': No error
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/do cs/troubleshooting.html
Error: bash: /dev/tty: No such device or address
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'https://github.com': No error
at ChildProcess.<anonymous> (E:\FisherBlog\blogMain\node_modules\hexo-deploy er-git\node_modules\hexo-util\lib\spawn.js:37:17)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at ChildProcess.cp.emit (E:\FisherBlog\blogMain\node_modules\hexo-deployer-g it\node_modules\hexo-util\node_modules\cross-spawn\lib\enoent.js:40:29)
at maybeClose (internal/child_process.js:829:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:211:5)
$ hexo deploy
INFO Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
INFO Copying files from extend dirs...
warning: LF will be replaced by CRLF in 2017/06/13/hello-world/index.html.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in archives/2017/06/index.html.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in archives/2017/index.html.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in archives/index.html.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in css/style.css.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in fancybox/helpers/jquery.fancybox-buttons .css.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in fancybox/helpers/jquery.fancybox-buttons .js.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in fancybox/helpers/jquery.fancybox-media.j s.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in fancybox/helpers/jquery.fancybox-thumbs. css.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in fancybox/helpers/jquery.fancybox-thumbs. js.
npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\
node_modules\\npm\\bin\\npm-cli.js" "install" "hexo-deployer-git" "--save"
npm ERR! node v4.6.0
npm ERR! npm v2.15.9
npm ERR! shasum check failed for C:\Users\ironGe\AppData\Local\Temp\npm-9704-f4a
f3e92\registry.npmjs.org\moment\-\moment-2.17.0.tgz
npm ERR! Expected: a4c292e02aac5ddefb29a6eed24f51938dd3b74f
npm ERR! Actual: b2b646a4ee8f93754abbf5c589fc7892e9c97b1c
npm ERR! From: https://registry.npmjs.org/moment/-/moment-2.17.0.tgz
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
然而接下来的事,让我高兴不起来,最后一步deploy部署的时候报错了。
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'https://github.com': Invalid argument
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/do
cs/troubleshooting.html
Error: bash: /dev/tty: No such device or address
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'https://github.com': Invalid argument
at ChildProcess.<anonymous> (D:\Hexo2\node_modules\hexo-deployer-git\node_mo
dules\hexo-util\lib\spawn.js:37:17)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at ChildProcess.cp.emit (D:\Hexo2\node_modules\hexo-deployer-git\node_module
s\hexo-util\node_modules\cross-spawn\lib\enoent.js:40:29)
at maybeClose (internal/child_process.js:829:16)
at Socket.<anonymous> (internal/child_process.js:319:11)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at Pipe._onclose (net.js:486:12)
FATAL bash: /dev/tty: No such device or address
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'https://github.com': Invalid argument
Error: bash: /dev/tty: No such device or address
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'https://github.com': Invalid argument
at ChildProcess.<anonymous> (D:\Hexo2\node_modules\hexo-deployer-git\node_mo
dules\hexo-util\lib\spawn.js:37:17)
at emitTwo (events.js:87:13)
at ChildProcess.emit (events.js:172:7)
at ChildProcess.cp.emit (D:\Hexo2\node_modules\hexo-deployer-git\node_module
s\hexo-util\node_modules\cross-spawn\lib\enoent.js:40:29)
at maybeClose (internal/child_process.js:829:16)
at Socket.<anonymous> (internal/child_process.js:319:11)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at Pipe._onclose (net.js:486:12)