日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用 Debian 从 0 开始搭建 hexo 博客

發布時間:2024/9/21 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用 Debian 从 0 开始搭建 hexo 博客 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

  • GitHub Pages
  • 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/ 查看運行效果。

    配置服務器環境

    服務器環境我選擇使用 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 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重新加載成功,可以繼續了

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

    部署 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 博客的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。