linux搭建hexo个人博客

徐峰
2023-12-01

安装node环境

node.js官网 :

https://nodejs.org/zh-cn/download/

资源下载链接 :
wget https://nodejs.org/dist/v16.13.1/node-v16.13.1-linux-x64.tar.xz

创建目录并解压 :

[root@localhost ~]# cd /usr/local/
[root@localhost local]# mkdir node
[root@localhost local]# cd node/
[root@localhost node]# tar -xJvf /root/node-v16.13.1-linux-x64.tar.xz -C ./

配置node系统环境变量

[root@localhost ~]# vim ~/.bash_profile

在文件末尾添加 :
# Nodejs
export PATH=/usr/local/node/node-v16.13.1-linux-x64/bin:$PATH

刷新文件并测试是否配置成功 : 
[root@localhost ~]# source ~/.bash_profile
[root@localhost ~]# node -v
v16.13.1
[root@localhost ~]# npm -version
8.1.2
[root@localhost ~]# npx -v
8.1.2
[root@localhost bin]# npm version
{
  npm: '8.1.2',
  node: '16.13.1',
  v8: '9.4.146.24-node.14',
  uv: '1.42.0',
  zlib: '1.2.11',
  brotli: '1.0.9',
  ares: '1.18.1',
  modules: '93',
  nghttp2: '1.45.1',
  napi: '8',
  llhttp: '6.0.4',
  openssl: '1.1.1l+quic',
  cldr: '39.0',
  icu: '69.1',
  tz: '2021a',
  unicode: '13.0',
  ngtcp2: '0.1.0-DEV',
  nghttp3: '0.1.0-DEV'
}
[root@localhost bin]# 

配置node 为淘宝镜像源 :

[root@localhost ~]# npm install -g cnpm --registry=https://registry.npm.taobao.org
[root@localhost ~]# cnpm -v

安装git

官方网址 :

https://git-scm.com/

安装包下载链接 :

https://mirrors.edge.kernel.org/pub/software/scm/git/

资源下载 : 
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.26.2.tar.gz

yum直接安装法(不建议使用):
[root@VM-24-17-centos /]# yum -y install git
yum安装卸载法 :
rpm -qa | grep git 
删除关于git的一切 : 
yum -y remove git-2.27.0-1.el8.x86_64
...

提前安装所需要的依赖 :

yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc-c++ perl-ExtUtils-MakeMaker

创建文件夹以及解压文件 :

[root@VM-24-17-centos ~]# tar zxvf git-2.26.2.tar.gz

编译安装 :

[root@VM-24-17-centos ~]# cd git-2.26.2/
[root@VM-24-17-centos git-2.26.2]# make prefix=/usr/local/git all
[root@VM-24-17-centos git-2.26.2]# make prefix=/usr/local/git install

将git加入环境变量 :

编辑配置文件 : 
[root@VM-24-17-centos git-2.26.2]# vim /etc/profile
[root@VM-24-17-centos git-2.26.2]# source /etc/profile
[root@VM-24-17-centos git-2.26.2]# git --version

安装nginx

​ 官网访问地址 :

http://nginx.org/en/download.html

资源下载链接 :
wget http://nginx.org/download/nginx-1.21.4.tar.gz

创建安装目录并解压 :

[root@localhost ~]# mkdir -p /usr/local/nginx
[root@localhost ~]# tar zxvf nginx-1.21.4.tar.gz
[root@localhost ~]# mv nginx-1.21.4 /usr/local/nginx/

预先安装额外的依赖 :

[root@localhost ~]# yum -y install pcre-devel
[root@localhost ~]# yum -y install openssl openssl-devel

编译安装nginx :

[root@localhost ~]# cd /usr/local/nginx/nginx-1.21.4/
[root@localhost nginx-1.21.4]# ./configure
[root@localhost nginx-1.21.4]# make && make install

安装完成后,nginx的执行文件位置位于/usr/local/nginx/sbin/nginx

启动nginx :

[root@localhost ~]# /usr/local/nginx/sbin/nginx 

停止nginx :

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop

如果修改了配置⽂件后想重新加载Nginx,可执⾏:

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

注意其配置⽂件位于:

/usr/local/nginx/conf/nginx.conf

最后使用浏览器访问IP验证;

通过npm下载hexo安装文件

hexo官方网址 :

https://hexo.io/zh-cn/

安装hexo :

[root@VM-24-17-centos ~]# cnpm install -g hexo-cli

[root@VM-24-17-centos ~]# hexo -v
hexo-cli: 4.3.0
os: linux 3.10.0-1160.45.1.el7.x86_64 CentOS Linux 7 (Core)
node: 16.13.1
v8: 9.4.146.24-node.14
uv: 1.42.0
zlib: 1.2.11
brotli: 1.0.9
ares: 1.18.1
modules: 93
nghttp2: 1.45.1
napi: 8
llhttp: 6.0.4
openssl: 1.1.1l+quic
cldr: 39.0
icu: 69.1
tz: 2021a
unicode: 13.0
ngtcp2: 0.1.0-DEV
nghttp3: 0.1.0-DEV
[root@VM-24-17-centos ~]#

搭建hexo博客文件

生成一个blog文件目录 :

[root@VM-24-17-centos ~]# mkdir /usr/local/blog
[root@VM-24-17-centos ~]# cd /usr/local/blog/
[root@VM-24-17-centos blog]# hexo init
生成hexo博客文件
[root@VM-24-17-centos blog]# hexo g

配置Nginx :

修改Nginx conf文件夹中的nginx.conf文件

[root@VM-24-17-centos conf]# pwd
/usr/local/nginx/conf
[root@VM-24-17-centos conf]# ll
total 68
-rw-r--r-- 1 root root 1077 Dec 23 15:17 fastcgi.conf
-rw-r--r-- 1 root root 1077 Dec 23 15:17 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 Dec 23 15:17 fastcgi_params
-rw-r--r-- 1 root root 1007 Dec 23 15:17 fastcgi_params.default
-rw-r--r-- 1 root root 2837 Dec 23 15:17 koi-utf
-rw-r--r-- 1 root root 2223 Dec 23 15:17 koi-win
-rw-r--r-- 1 root root 5349 Dec 23 15:17 mime.types
-rw-r--r-- 1 root root 5349 Dec 23 15:17 mime.types.default
-rw-r--r-- 1 root root 2674 Dec 23 15:32 nginx.conf
-rw-r--r-- 1 root root 2656 Dec 23 15:17 nginx.conf.default
-rw-r--r-- 1 root root  636 Dec 23 15:17 scgi_params
-rw-r--r-- 1 root root  636 Dec 23 15:17 scgi_params.default
-rw-r--r-- 1 root root  664 Dec 23 15:17 uwsgi_params
-rw-r--r-- 1 root root  664 Dec 23 15:17 uwsgi_params.default
-rw-r--r-- 1 root root 3610 Dec 23 15:17 win-utf
[root@VM-24-17-centos conf]# vim nginx.conf

将http--->下面的server--->location----> root的路劲修改成hexo的public文件路径
        location / {
            root   /usr/local/blog/public;
            index  index.html index.htm;
        }

启动nginx

[root@VM-24-17-centos sbin]# pwd
/usr/local/nginx/sbin
[root@VM-24-17-centos sbin]# ll
total 5064
-rwxr-xr-x 1 root root 5184192 Dec 23 15:17 nginx
[root@VM-24-17-centos sbin]# ./nginx

浏览器访问测试;

至此初步使用服务器搭建hexo个人博客成功;

编辑以及生成博客文章

在blog文件夹下面编辑 : 
[root@VM-24-17-centos blog]# hexo n "xxxxx"
INFO  Validating config
INFO  Created: /usr/local/blog/source/_posts/xxxxx.md
[root@VM-24-17-centos blog]# cd source/_posts/
[root@VM-24-17-centos blog]# vim xxxxx.md

退回至blog文件夹并清理下文件 : 
[root@VM-24-17-centos ~]# cd /usr/local/blog/
[root@VM-24-17-centos blog]# hexo clean

生成博客文件 : 
[root@VM-24-17-centos blog]# hexo g

重新启动 : 
[root@VM-24-17-centos sbin]# ./nginx -s reload

安装shoka主题

先安装必要hexo插件

md 文件渲染器,压缩 css/js/html (必须)

先删除默认的渲染器
npm un hexo-renderer-marked --save
或者
yarn remove hexo-renderer-marked

安装
npm i hexo-renderer-multi-markdown-it --save
或者
yarn add hexo-renderer-multi-markdown-it

给生成的 css 文件们添加浏览器前缀

npm install hexo-autoprefixer --save

站内搜索功能

npm install hexo-algoliasearch --save

文章或站点字数及阅读时间统计

npm install hexo-symbols-count-time

生成 Feed 文件

npm install hexo-feed --save-dev

配置一下_config.yml文件

# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/

# Site
title: Hexo
subtitle: ''
description: ''
keywords:
author: liu chang
language: zh-CN
timezone: 'Asia/Shanghai'

# URL
## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project'
url: http://example.com
permalink: :year/:month/:day/:title/
permalink_defaults:
pretty_urls:
  trailing_index: true # Set to false to remove trailing 'index.html' from permalinks
  trailing_html: true # Set to false to remove trailing '.html' from permalinks

# Directory
source_dir: source
public_dir: public
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
i18n_dir: :lang
skip_render:

# Writing
new_post_name: :title.md # File name of new posts
default_layout: post
titlecase: false # Transform title into titlecase
external_link:
  enable: true # Open external links in new tab
  field: site # Apply to the whole site
  exclude: ''
filename_case: 0
render_drafts: false
post_asset_folder: false
relative_link: false
future: true
highlight:
  enable: false
  line_number: true
  auto_detect: true
  tab_replace: ''
  wrap: true
  hljs: false
prismjs:
  enable: false
  preprocess: true
  line_number: true
  tab_replace: ''

# Home page setting
# path: Root path for your blogs index page. (default = '')
# per_page: Posts displayed per page. (0 = disable pagination)
# order_by: Posts order. (Order by date descending by default)
index_generator:
  path: ''
  per_page: 10
  order_by: -date

# Category & Tag
default_category: uncategorized
category_map:
tag_map:

# Metadata elements
## https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta
meta_generator: true

# Date / Time format
## Hexo uses Moment.js to parse and display date
## You can customize the date format as defined in
## http://momentjs.com/docs/#/displaying/format/
date_format: YYYY-MM-DD
time_format: HH:mm:ss
## updated_option supports 'mtime', 'date', 'empty'
updated_option: 'mtime'

# Pagination
## Set per_page to 0 to disable pagination
per_page: 10
pagination_dir: page

# Include / Exclude file(s)
## include:/exclude: options only apply to the 'source/' folder
include:
exclude:
ignore:

# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: shoka

# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
  type: ''

# edit for Theme.shoka
autoprefixer:
  exclude:
    - '*.min.css'

markdown:
  render: # 渲染器设置
    html: false # 过滤 HTML 标签
    xhtmlOut: true # 使用 '/' 来闭合单标签 (比如 <br />)。
    breaks: true # 转换段落里的 '\n' 到 <br>。
    linkify: true # 将类似 URL 的文本自动转换为链接。
    typographer:
    quotes: '“”‘’'
  plugins: # markdown-it插件设置
    - plugin:
        name: markdown-it-toc-and-anchor
        enable: true
        options: # 文章目录以及锚点应用的class名称,shoka主题必须设置成这样
          tocClassName: 'toc'
          anchorClassName: 'anchor'
    - plugin:
        name: markdown-it-multimd-table
        enable: true
        options:
          multiline: true
          rowspan: true
          headerless: true
    - plugin:
        name: ./markdown-it-furigana
        enable: true
        options:
          fallbackParens: "()"
    - plugin:
        name: ./markdown-it-spoiler
        enable: true
        options:
          title: "你知道得太多了"

minify:
  html:
    enable: true
    stamp: false
    exclude:
      - '**/json.ejs'
      - '**/atom.ejs'
      - '**/rss.ejs'
  css:
    enable: true
    stamp: false
    exclude:
      - '**/*.min.css'
  js:
    enable: true
    stamp: false
    mangle:
      toplevel: true
    output:
    compress:
    exclude:
      - '**/*.min.js'

# algolia:
#   appId:
#   apiKey:
#   adminApiKey:
#   chunkSize: 5000
#   indexName:
#   fields:
#     - title #必须配置
#     - path #必须配置
#     - categories #推荐配置
#     - content:strip:truncate,0,4000
#     - gallery
#     - photos
#     - tags

feed:
    limit: 20
    order_by: "-date"
    tag_dir: false
    category_dir: false
    rss:
        enable: true
        template: "themes/shoka/layout/_alternate/rss.ejs"
        output: "rss.xml"
    atom:
        enable: true
        template: "themes/shoka/layout/_alternate/atom.ejs"
        output: "atom.xml"
    jsonFeed:
        enable: true
        template: "themes/shoka/layout/_alternate/json.ejs"
        output: "feed.json"

执行

hexo clean
hexo g

最后重启nginx即可;

卸载hexo

看你是用cnpm安装的还是npm

cnpm uninstall -g hexo-cli

剩下就是把自己的域名解析成这个公网的IP

 类似资料: