Ubuntu+Hexo+Github搭建個人博客


Ubuntu+Hexo+Github搭建個人博客

目錄

1. 簡介

以前部署的Hexo博客是在Windows上搭建的,在Windows系統使用很方便,碰到一些問題也都能夠解決;現在安裝了Ubuntu-18.04.1系統,需要重新使用Hexo搭建博客;為了兼容以前的windows博客,還需要將以前Windows系統的博客遷移到Ubuntu系統下使用;

環境

# uname -a
Linux xiami 5.3.0-40-generic #32~18.04.1-Ubuntu SMP Mon Feb 3 14:05:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

搭建博客需要安裝的軟件:

git
node.js
hexo

2. Git安裝及配置

2.1 安裝Git

使用命令安裝Git工具:

# sudo apt install git

查看是否安張成功:

# git --version
git version 2.17.1

2.2 創建Git倉庫

打開GitHub,點擊“New repository”,創建一個新倉庫,用來專門存放博客日志信息;倉庫名要按照格式:賬戶名.github.io,比如:Mshrimp.github.io;否則,后邊的操作會出現問題;創建倉庫時勾選上“
Initialize this repository with a README”;

進入創建好的倉庫Mshrimp.github.io,點擊右側的“Settings”,向下拉找到Github Pages,會看到網站是:https://mshrimp.github.io/,點擊就可以訪問,也可以通過外網訪問,這時這個博客項目已經部署到網站上了,但是是個空的網站,沒有內容;這個網址是博客的默認地址,如果有興趣可以自己購買域名換成想要的地址。

Image.png

Image2.png

2.3 配置git倉庫

如果是第一次使用git,就需要先配置git環境,否則可以跳過;

$ git config --global user.name "Mshrimp"
$ git config --global user.email "******@outlook.com"

在沒有配置git環境之前,~/.ssh是不存在的

$ cd ~/.ssh
bash: cd: /c/Users/Kevin-TP/.ssh: No such file or directory

使用ssh-keygen生成私鑰和公鑰

$ ssh-keygen -t rsa -C "chiyuan.ma@outlook.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Kevin-TP/.ssh/id_rsa):
Created directory '/c/Users/Kevin-TP/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Kevin-TP/.ssh/id_rsa.
Your public key has been saved in /c/Users/Kevin-TP/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:pHNkvs9RsOToxmFH6gnkOb7j/dlRSc4c6TkOvGQ6fcc chiyuan.ma@outlook.com
The key's randomart image is:
+---[RSA 3072]----+
|                 |
|               . |
|      . + +   +  |
|     o B * + * + |
|      B S + * X  |
|     . X = * = o |
|      . B + + o E|
|      .+ o = o . |
|     .o...= .    |
+----[SHA256]-----+

從以上的操作打印可以知道,生成的密鑰和公鑰的保存路徑

Your identification has been saved in /c/Users/Kevin-TP/.ssh/id_rsa.
Your public key has been saved in /c/Users/Kevin-TP/.ssh/id_rsa.pub.

查看生成的密鑰和公鑰

$ cd ~/.ssh
$ ls
id_rsa  id_rsa.pub
$ cat id_rsa.pub
ssh-rsa 
......

2.4 添加公鑰

把本地公鑰添加到github中;在GitHub中,點擊右側圖像下拉選項,選擇“Settings”,在“SSH and GPG keys”中,點擊“New SSH key”,並將~/.ssh/id_rsa.pub文件里的內容復制上去,保存退出;

Image4.png

Image5.png

使用“ssh -T git@github.com”命令,測試添加ssh是否成功;

$ ssh -T git@github.com
......
Hi Mshrimp! You've successfully authenticated, but GitHub does not provide shell access.

3. Node安裝

命令行安裝:

# sudo apt-get install nodejs
# sudo apt install npm

查看nodejs工具是否安裝成功:

# nodejs -v
v8.10.0

4. Hexo安裝及配置

先創建一個hexo操作的文件目錄

[圖片上傳失敗...(image-6e25ed-1586142446763)]

如果使用的是Linux系統,可以直接在命令行中輸入命令操作,如果是windows系統,用管理員權限打開“命令提示符”,使用命令在電腦上安裝hexo;或者,在hexo目錄上右鍵,選擇“Git Bash Here”,用git bash工具打開hexo目錄,在git bash中使用命令操作;

4.1 安裝hexo

# npm install hexo-cli -g
# npm install hexo -g

檢查hexo是否安裝成功

$ hexo -v
hexo: 4.2.0
hexo-cli: 3.1.0
os: Linux 5.3.0-45-generic linux x64
http_parser: 2.7.1
node: 8.10.0
v8: 6.2.414.50
uv: 1.18.0
zlib: 1.2.11
ares: 1.14.0
modules: 57
nghttp2: 1.30.0
openssl: 1.0.2n
icu: 60.2
unicode: 10.0
cldr: 32.0.1
tz: 2017c

4.2 初始化hexo文件夾

# hexo init

看到“Start blogging with Hexo!”打印,說明初始化完成;

輸入npm install,安裝所需要的組件

# npm install

hexo已經安裝並初始化完成;

# ls
_config.yml  node_modules/  package.json  package-lock.json  scaffolds/  source/  themes/

到此,hexo環境安裝完成。

4.3 Hexo操作

$ hexo g #generate 生成靜態文件
$ hexo s #server 啟動服務器。
// 默認情況下,訪問網址為: [http://localhost:4000/]

在瀏覽器地址欄輸入“http://localhost:4000/”打開頁面,是一個空的博客網頁;

Image3.JPG

4.4 將git庫和hexo鏈接起來

配置Deployment

在hexo文件夾中,找到_config.yml文件,修改repository值(在末尾),repository值是github項目里的ssh;

deploy:
  type: git
  repository: git@github.com:Mshrimp/Mshrimp.github.io.git
  branch: master

HexoBlog部署到git,需要安裝hexo-deployer-git插件,在blog目錄下運行以下命令進行安裝;

$ npm install hexo-deployer-git --save

npm WARN babel-eslint@10.1.0 requires a peer of eslint@>= 4.12.1 but none is 
installed. You must install peer dependencies yourself.

+ hexo-deployer-git@1.0.0
added 1 package from 1 contributor, removed 4 packages and updated 14 packages in 
5.684s

修改根目錄下_config.yml文件后,需要使用$ hexo deploy部署一下,否則修改內容不會生效;

$ hexo deploy

至此,一個空的博客已經搭建完成,下一步,添加博客文章;

5. 更換主題

由於不太喜歡原來自帶的主題,找了一個比較好看的yilia主題,需要先從Github中將yilia主題的源碼下載到博客目錄的themes目錄下(感謝yilia主題作者的無私奉獻);

# git clone https://github.com/litten/hexo-theme-yilia.git themes/yilia

在博客根目錄下,修改_config.yml文件的themes:

themes: yilia

這個主題中的一些配置,可以根據需要自行修改,配置文件為themes/yilia/_config.yml;

效果如下:

themes-yilia.png

6. 博客遷移

6.1 常規遷移

以前部署的Hexo博客是在Windows上搭建的,現在安裝了Ubuntu-18.04.1,需要重新搭建博客;為了兼容以前的windows博客,需要使用以前的Hexo下的幾個文件夾:

_config.yml  package.json  source/  themes/

這時,在Ubuntu系統上創建文件夾hexo,以hexo為主目錄搭建博客環境:

# mkdir hexo
# cd hexo
# hexo init

將上邊四個文件或文件夾復制到hexo目錄替換:

# cp _config.yml  package.json  source  themes . -rf

之后就可以按照hexo命令進行操作了;

6.2 特殊情況

由於我的實際情況是,不但將hexo中的博客文件保存在了Github上,還將hexo生成的環境工程,以並保存在Github同一個工程的其他分支了,如:ubuntu分支;這樣我就可以一並保存所有文件了;

在這種特殊情況下,博客遷移操作原理上和常規遷移一樣,只是操作稍微不同;

將以前用的工程在Ubuntu環境中克隆下來,Mshrimp.github.io;

新建hexo目錄,並初始化hexo目錄:

# mkdir hexo
# cd hexo
# hexo init
# ls
_config.yml  node_modules/  package.json  package-lock.json  scaffolds/  source/  themes/

將hexo中生成的文件或文件夾復制到Mshrimp.github.io目錄替換:

node_modules/  package-lock.json  scaffolds/

這個操作,和常規遷移的區別是,復制的方向相反,其余都一樣;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM