使用 Debian 從 0 開始搭建 hexo 博客

Hexo 是一個快速、簡潔且高效的博客框架。Hexo 使用 Markdown(或其他渲染引擎)解析文章,在幾秒內,即可利用靚麗的主題生成靜態網頁。

Hexo 產生的靜態文件只要放到任何支持 html 的空間或者服務器均可訪問。主要的選擇方案有以下兩種

  1. GitHub Pages
  2. VPS
GitHub Pages 本用於介紹託管在GitHub的項目,不過,由於他的空間免費穩定,用來做搭建一個博客再好不過了。
每個帳號只能有一個倉庫來存放個人主頁,而且倉庫的名字必須是username/username.github.io,這是特殊的命名約定。你可以通過http://username.github.io 來訪問你的個人主頁。)

Github Pages 好處是完全免費,搭建並部署到教程可以參考 Hexo+Github Pages搭建個人獨立博客。我個人的方案是第二種,部署到自己的服務器。

安裝 Hexo

更新軟件包

apt-get update
apt-get upgrade

安裝依賴

Hexo 依賴於 Node.js 和 Git,需要先安裝。

安裝 Git

apt install git-core -y

查看 git 版本

git --version

安裝 Node.js

使用以下命令安裝 Node.js

wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh

更新

source ~/.profile

安裝 Node.js

nvm install stable

查看node.js版本

node --version

查看npm版本

npm --version

安裝 Hexo

npm install hexo-cli -g

在 hexo 目錄下初始化 hexo 博客,也可以是任意你想要的名字

hexo init hexo

進入博客根目錄,並且安裝相關插件依賴等

cd hexo
npm install

安裝完成後需要用一下命令

hexo g # 渲染 Source 目錄下文件問靜態頁面
hexo s # 本地跑一個 server 來看博客效果。

然後可以在 http://localhost:4000/ 查看運行效果。

hexo

配置服務器環境

服務器環境我選擇使用 Debian + Nginx 環境。

必須先執行 cd 把目錄切換到root後才能執行下面操作

安裝 Nginx

Nginx 是一個高性能的 HTTP 和反向代理服務器,同時也是一個 IMAP/POP3/SMTP 代理服務器。

執行命令安裝Nginx

apt-get install nginx

啓動 Nginx

Nginx 安裝完成後需要手動啓動

service nginx start

配置完成後,訪問使用瀏覽器服務器 ip ,如果能看到以下界面,表示運行成功。

Imgur

順便提下 Nginx 配置參數

start nginx

service nginx start

stop nginx

service nginx stop

other parameters

reload        restart       start         status        stop

配置虛擬主機

虛擬主機(Virtual Host)可以在一臺服務器上綁定多個域名,架設多個不同的網站,一般在開發機或者要部署多個小網站的服務器上需要配置虛擬主機。

創建新的網站目錄

Nginx 默認把網頁文件存在 /var/www/html 目錄。

/var/www/html/ 目錄下創建 index.html 文件。寫上以下內容,用於測試虛擬主機運行情況。

<html>
    <head>
        <title>Welcome to Blog!</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

創建虛擬主機配置文件

在 /etc/nginx/conf.d/ 創建虛擬主機配置文件 hexo.conf,並且填寫以下代碼

server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name www.abc.com abc.com;
    location / {
    try_files $uri $uri/ =404;
    }
}

如果需要部署SSL,先把SSL證書和Key上傳到VPS你喜歡的文件夾,並且填寫以下代碼

server {
    listen 80 default backlog=2048;
    listen 443 ssl;
    server_name abc.com;
    if ($scheme = http ) {
    return 301 https://www.$host$request_uri;
    }

    #ssl on;    #註釋掉
    ssl_certificate /etc/nginx/ssl/ssl.crt;
    ssl_certificate_key /etc/nginx/ssl/ssl.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
    ssl_prefer_server_ciphers on;
    location / {
    root /var/www/html;     #根目錄的相對位置
    index index.html index.htm;
    }
}

重啓 Nginx 服務器,使服務器設定生效

service nginx restart

如果執行 service nginx restart時提示如下錯誤

Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.,

執行 nginx -t

如果提示下面這句錯誤命令

nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/default:22
nginx: configuration file /etc/nginx/nginx.conf test failed

那麼執行 cd /etc/nginx/sites-enabled進入sites-enabled文件夾

執行rm default刪除default

再執行nginx -t出現下面命令

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

執行下面的命令重啓nginx

service nginx restart

查看 nginx 的狀態

service nginx status

OK,Nginx重新加載成功,可以繼續了

進入瀏覽器輸入自己的域名,能看到以下結果就表示虛擬主機配置成功。

Imgur

部署 Hexo 到服務器

Hexo 可以使用 git 方式部署。由於第一步已經安裝完成Git,所以直接繼續

配置服務器環境

創建空白 git 倉庫,並且設置 git hook

cd ~
mkdir hexo.git && cd hexo.git
git init --bare

/root/hexo.git/hooks 創建配置文件 post-receive,並且填寫以下代碼

#!/bin/bash
GIT_REPO=/root/hexo.git  #git倉庫
TMP_GIT_CLONE=/tmp/hexo
PUBLIC_WWW=/var/www/html #網站目錄
rm -rf ${TMP_GIT_CLONE}
git clone $GIT_REPO $TMP_GIT_CLONE
rm -rf ${PUBLIC_WWW}/*
cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}

賦予腳本的執行權限

cd /root/hexo.git/hooks
chmod +x post-receive

配置本機環境

在博客目錄下運行下面命令,安裝 git 部署工具。

cd
cd hexo
npm install hexo-deployer-git --save

修改博客的配置文件 _config.yml,修改deploy選項:

deploy:
  type: git
  message: update
  repo: [email protected]:/root/hexo.git
  branch: master

然後運行 hexo g -d 部署本地渲染網頁到服務器上。

錯誤

如果提示下面這句錯誤命令

INFO  Deploying: git
INFO  Setting up Git deployment...
Initialized empty Git repository in /root/hexo/.deploy_git/.git/

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@debian.(none)')
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html
Error: Spawn failed
    at ChildProcess.<anonymous> (/root/hexo/node_modules/hexo-util/lib/spawn.js:52:19)
    at ChildProcess.emit (events.js:197:13)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:254:12)

解決方案: 見提示就知道, 要您填上你得註冊的郵箱和暱稱,例如:

git config --global user.email "[email protected]" #自行更換自己的郵箱
git config --global user.name "Your Name" #自行更換自己的暱稱
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章