使用 Debian 从 0 开始搭建 hexo 博客
Hexo 是一個快速、簡潔且高效的博客框架。Hexo 使用 Markdown(或其他渲染引擎)解析文章,在幾秒內,即可利用靚麗的主題生成靜態網頁。
Hexo 產生的靜態文件只要放到任何支持 html 的空間或者服務器均可訪問。主要的選擇方案有以下兩種
每個帳號只能有一個倉庫來存放個人主頁,而且倉庫的名字必須是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/ 查看運行效果。
配置服務器環境
服務器環境我選擇使用 Debian + Nginx 環境。
必須先執行 cd 把目錄切換到root后才能執行下面操作
安裝 Nginx
Nginx 是一個高性能的 HTTP 和反向代理服務器,同時也是一個 IMAP/POP3/SMTP 代理服務器。
執行命令安裝Nginx
apt-get install nginx啟動 Nginx
Nginx 安裝完成后需要手動啟動
service nginx start配置完成后,訪問使用瀏覽器服務器 ip ,如果能看到以下界面,表示運行成功。
順便提下 Nginx 配置參數
start nginx
service nginx startstop nginx
service nginx stopother 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 statusOK,Nginx重新加載成功,可以繼續了
進入瀏覽器輸入自己的域名,能看到以下結果就表示虛擬主機配置成功。
部署 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: gitmessage: updaterepo: root@1.1.1.1:/root/hexo.gitbranch: 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.Rungit config --global user.email "you@example.com"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 failedat 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 "you@example.com" #自行更換自己的郵箱 git config --global user.name "Your Name" #自行更換自己的昵稱總結
以上是生活随笔為你收集整理的使用 Debian 从 0 开始搭建 hexo 博客的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 51nod 1781 Pinball(线
- 下一篇: 如何通俗易懂地向别人解释React生命周