前言
自从上次,被同事责备了一次写博客(将思路带出来了)的事情,马上就有动手搭本地博客的念头. 那样,技术笔记随时随地都可以写,都可以归档了,自己查阅技术笔记的效果也符合平时看资料的习惯。
虽然现在公网上的博客平台,都有私密博客这个设置,但是博客里面带的附件(图片 )和demo工程都是没法私密的。e.g. csdn 人家现在下载频道的下载权都是要卖钱的,怎么会添加私密附件这种功能呢。
想在本地服务器上搭建一个轻量(不要装数据库的那种,以后好迁移本地博客资料到其他新硬盘或备份资料),私人化(不需要自动发布到公网, 不需要买公网服务器,不需要托管到第三方),安装部署简单的博客,去网上查资料,看来看去,还是Hexo符合需求。
实验
环境
本地服务器 : dell t430 + debian9.6
官方文档
https://hexo.io/zh-cn/docs/
安装nodejs
apt-get update
root@debian9:~# apt search nodejs | grep nodejs
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
Pluggable transport to circumvent IP address blocking - nodejs proxy
unobtrusive notification system for nodejs
nodejs/stable 4.8.2~dfsg-1 amd64
nodejs-dbg/stable 4.8.2~dfsg-1 amd64
nodejs-dev/stable 4.8.2~dfsg-1 amd64
nodejs-legacy/stable 4.8.2~dfsg-1 all
apt-get install nodejs
安装git
apt-get install git
验证git是否已经安装
root@debian9:~# git --version
git version 2.11.0
安装nvm
cd /home/soft_install
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
...
=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
查看nvm启动的脚本
root@debian9:~# cat /root/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# Note: PS1 and umask are already set in /etc/profile. You should not
# need this unless you want different defaults for root.
# PS1='${debian_chroot:+($debian_chroot)}\h:\w\$ '
# umask 022
# You may uncomment the following lines if you want `ls' to be colorized:
# export LS_OPTIONS='--color=auto'
# eval "`dircolors`"
# alias ls='ls $LS_OPTIONS'
# alias ll='ls $LS_OPTIONS -l'
# alias l='ls $LS_OPTIONS -lA'
#
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
安装nodejs
重新克隆一个终端,用root身份登录,就可以使用nvm命令了
nvm install stable
如果上面这句下载时,卡住了,杀死再来一次。可以另外开一个控制台,运行 wget https://nodejs.org/dist/v11.4.0/node-v11.4.0-linux-x64.tar.xz 看看是不是网络问题。
验证nodejs是否已经安装
root@debian9:/home/lostspeed/my_blog# node -v
v11.4.0
使用 npm 安装 Hexo
安装hexo命令行接口
npm install -g hexo-cli
初始化本地博客
cd /home/lostspeed/my_blog
hexo init /home/lostspeed/my_blog
root@debian9:/home/lostspeed/my_blog# ls -l
总用量 168
-rw-r--r-- 1 root root 1765 12月 15 22:44 _config.yml // 配置文件
drwxr-xr-x 286 root root 12288 12月 15 22:46 node_modules
-rw-r--r-- 1 root root 443 12月 15 22:44 package.json // 应用程序信息
-rw-r--r-- 1 root root 137055 12月 15 22:46 package-lock.json
drwxr-xr-x 2 root root 4096 12月 15 22:44 scaffolds // 模板目录
drwxr-xr-x 3 root root 4096 12月 15 22:44 source // 用户资源
drwxr-xr-x 3 root root 4096 12月 15 22:44 themes // 主题
安装其他依赖包
有可能初始化博客目录时,有错误发生,需要进入博客目录,再安装其他依赖包.
cd /home/lostspeed/my_blog
npm install
生成博客本地站点
hexo g
生成/home/lostspeed/my_blog/public
public目录是生成的本地博客网站数据
启动博客服务
启动博客服务后,就可以在局域网内的其他开发机上,访问刚刚建立的新本地博客。
root@debian9:/home/lostspeed/my_blog# hexo s
INFO Start processing
INFO Hexo is running at http://localhost:4000 . Press Ctrl+C to stop.
服务启动后,指出博客端口为4000
这个博客服务在控制台上启动时是前台的,关了SecureCRT控制台就没有了。要保持控制台开着,可以新启动控制台去做其他任务。
从其他计算机的浏览器上访问自己的本地博客
在浏览器上输入网址 http://192.168.2.222:4000/
可以看到本地博客可以访问了,后续写新博客的说明,在本地博客站点上也有说明.
配置文件
配置文件名 = _config.yml
不同资源都有配置文件,在不同的资源目录里面
root@debian9:/home/lostspeed/my_blog# pwd
/home/lostspeed/my_blog
root@debian9:/home/lostspeed/my_blog# find . -name _config.yml
./node_modules/hexo/node_modules/hexo-cli/assets/themes/landscape/_config.yml
./node_modules/hexo/node_modules/hexo-cli/assets/_config.yml
./themes/landscape/_config.yml
./_config.yml
写新本地博客文章
命令
hexo new "新博客文章标题"
e.g.
root@debian9:/home/lostspeed/my_blog# hexo new "my first local blog test"
INFO Created: /home/lostspeed/my_blog/source/_posts/my-first-local-blog-test.md
编辑文章
直接编译md文件,这步,可以用winscp将.md下载下来,在windows上用图形化的md工具来编辑,e.g. Visual Studio Code, 写完再回传到服务器的文章位置。hexo g, hexo s, http://192.168.2.222:4000/, 就能看到更新的博客内容了
markdown模板
title: my first local blog test by hexo
date: 2018-12-16 15:13:00
categories:
- markdown
tags:
- markdown
- template
确认被hexo编译的markdown语法是否正确或被支持
hexo g
如果markdown语法不对, 会报错。如果语法不被支持(但是为标准的markdown语法,不报错)
这是大标题
如果是控制标记, e.g. —, #, *, 都要在第0列来写, 写完后要空一格,才能写内容
中文行不行? 可以
/home/lostspeed/my_blog/ 是本地博客站点的根目录
/home/lostspeed/my_blog/source/my_img 作为图像目录,路径为/my_img
就是说图像的相对位置是先对于/home/lostspeed/my_blog/source/ 的
在博客中要显示图片时,指定的图片位置为/my_img/x.png
博客中引起相对路径的图像
如果链接前有!, 说明这个连接是图像,要显示出来
如果链接前没有!, 说明这个链接只是一个普通的链接
插入链接: [连接的名称](real url)
e.g. csdn主站
这是2级标题
是几级标题就加几个#开头, 空1格,标题名称 e.g. ##### 这是5级标题
清除无用tag和categories
这是脚本代码
` 这个标记好奇怪,键盘上找不到...
hexo clean
hexo d -g
标记代码块用成对的 来标记 标记时,第一个
空1格,后面为代码块的种类 e.g. c, c++, java, asm
这是c代码
int main(int argc, char** argv)
{
return EXIT_SUCCESS;
}
这是c++代码
class cls_test
{
public:
cls_test() {}
virtual ~cls_test() {}
void inc_cnt() {m_cnt++;}
int get_cnt() {return m_cnt;}
private:
int m_cnt; // 计数
}
这是java代码
private int bit4_to_hex_char(int in)
{
if ((in >= 0) && (in <= 9)) {
in += '0';
} else if ((in >= 10) && (in <= 15)) {
in -= 0x0a;
in += 'A';
} else {
in = '.';
}
return in;
}
这是汇编代码
mov ecx, eax
inc eax
如果要对代码块显示行数,在代码块类型之后,加上 {.line-numbers}
这是c++代码
class cls_test
{
public:
cls_test() {}
virtual ~cls_test() {}
void inc_cnt() {m_cnt++;}
int get_cnt() {return m_cnt;}
private:
int m_cnt; // 计数
}
列表以*打头, 以tab键作为列表的层级缩进
-
1级列表
这是属于1级列表的文字描述-
2级列表a
这是属于2级列表的文字描述 -
2级列表b
这是属于2级列表的文字描述- 3级列表a
这是属于2级列表的文字描述
- 3级列表a
-
单选框
用 “- [x] 内容” 或 “- [ ] 内容” 表示
- this is a complete item
- this is an incomplete item
行内容的不同显示效果, 成对的~~, *, **来围住文字,符号和内容跟之间不能有空格
这是被删除的文字
这是斜体的文字
这是粗体的文字
表格
表格有行合并和列合并的符号, 简单用一下就好
列前后用|分隔,向上合并用^, 向右合并用>
hexo对表格支持不好
列1 | 列2 | 列3 |
---|---|---|
c1_1 | ||
c2_2 | ||
> | c3_2 | |
> | c5_2 | |
^ |
备注
在csdn上写博客,用到的markdown语法模板准备的差不多了,更高级的语法,以后再去查。