Hexo、Stellar及twikoo安装及配置小记

我的服务器操作系统是FreeBSD 14.1,并在服务器上已经安装好nginx及git。

1. 安装 Node.js

在安装Hexo前需要先安装node.js与npm,可以使用以下命令查看安装哪个版本的node.js与npm:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@freebsd:~ # pkg search ^node
node-20.15.1_1 V8 JavaScript for client and server (meta port)
node-thrift-0.16.0_3 Node.js bindings for the Apache Thrift RPC system
node18-18.20.4 V8 JavaScript for client and server
node20-20.15.1 V8 JavaScript for client and server
node22-22.4.1 V8 JavaScript for client and server
node_exporter-1.6.1_7 Prometheus exporter for machine metrics
root@freebsd:~ #
root@freebsd:~ # pkg search ^npm
npm-10.8.1 Node package manager (meta port)
npm-node18-10.8.1 Node package manager
npm-node20-10.8.1 Node package manager
npm-node22-10.8.1 Node package manager
npmount-0.1.1.3_1 Command line tool for non-privileged [un]mount

我安装的是22版,使用以下命令:
1
2
root@freebsd:~ # pkg install node22
root@freebsd:~ # pkg install npm-node22

安装完毕后可以用以下命令查看版本信息:
1
2
3
4
root@freebsd:~ # node --version
v22.4.1
root@freebsd:~ # npm --version
10.8.3


2. 安装Hexo

可以按照hexo官网教程进行安装

1
2
root@freebsd:~ # npm install -g hexo-cli
root@freebsd:~ # npm install hexo-deployer-git --save


3. 私有部署及配置Hexo

  • 创建git仓库,并初始化blog.git为空目录
    1
    2
    3
    root@freebsd:~ # cd /home/git
    root@freebsd:~ # git init --bare blog.git
    root@freebsd:~ # chown -R git:git blog.git
  • 创建blog静态网站,并对站点进行相应配置
    1
    2
    3
    4
    root@freebsd:~ # cd /home/html
    root@freebsd:~ # hexo init blog
    root@freebsd:~ # cd blog
    root@freebsd:~ # npm install
  • 配置git hooks的post-receive文件
    1
    root@freebsd:~ # vim /home/git/blog.git/hook/post-receive
  • 并在该文件中添加如下一行
    1
    2
    #!/bin/sh
    git --work-tree=/home/html/blog --git-dir=/home/git/blog.git checkout -f
  • 保存并退出后,修改设置权限,同时将git仓库与之前创建的blog网站目录链接,并配置权限:
    1
    2
    3
    root@freebsd:~ # chmod +x /home/git/blog.git/hooks/post-receive
    root@freebsd:~ # chown -R git:git /home/html/blog
    root@freebsd:~ # chmod -R 755 /home/html/blog
  • 打开blog目录,编辑_config.yml文件的deploy部分:
    1
    2
    3
    4
    deploy:
    type: 'git'
    repository: /home/git/blog.git
    branch: master
  • 生成blog网站静态网页
    1
    2
    root@freebsd:~ # hexo g
    root@freebsd:~ # hexo d
  • 对/usr/local/etc/nginx/nginx.conf作相应的修改,并重启nginx
    1
    root@freebsd:~ # service nginx restart

4. 安装Hexo的stellar theme

  • 进入/home/html/blog目录,并安装hexo-theme-stellar
    1
    root@freebsd:/home/html/blog # npm i hexo-theme-stellar
  • 在blog/_config.yml 文件中找到并修改:
    1
    2
    3
    4
    url: https://hd.mould.club:64443/blog
    root: /blog/

    theme: stellar
  • 添加“霞鹜文楷屏幕阅读版”字体,在/home/html/blog/_config.stellar.yml配置文件中添加如下配置:
    1
    2
    3
    4
    5
    6
    7
    8
    inject:
    head:
    - <link rel="stylesheet" href="https://cdn.staticfile.org/lxgw-wenkai-screen-webfont/1.7.0/style.min.css" />
    script:

    style:
    font-family:
    body: '"LXGW WenKai Screen", system-ui, "Microsoft Yahei", "Segoe UI", -apple-system, Roboto, Ubuntu, "Helvetica Neue", Arial, "WenQuanYi Micro Hei", sans-serif'

5. 私有部署twikoo评论系统

  • 安装 Twikoo server,详细可查看官网
    1
    root@freebsd:/home/html/blog # npm i -g tkserver
  • 启动tkserver
    1
    2
    3
    4
    5
    6
    7
    8
    root@freebsd:/home/html/blog # tkserver
    9/28/2024, 11:07:24 PM Twikoo: Twikoo database stored at /home/html/blog/data
    9/28/2024, 11:07:24 PM Twikoo: Connecting to database...
    (node:48256) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
    9/28/2024, 11:07:24 PM Twikoo: Twikoo is using loki database
    9/28/2024, 11:07:24 PM Twikoo: Twikoo function started on host :: port 8080
    9/28/2024, 11:07:24 PM Twikoo: Connected to database
  • 访问http://ip:8080查看是否启动成功
    1
    {"code":100,"message":"Twikoo 云函数运行正常,请参考 https://twikoo.js.org/frontend.html 完成前端的配置","version":"1.6.39"}
  • 在blog目录下新建_config.stellar.yml,并在配置文件中添加
    1
    2
    3
    4
    comments:
    service: twikoo
    twikoo:
    envId: http://192.168.1.4:8080 # 后继修改成相应网址
  • 将tkserver配置成FreeBSD的rc.d服务脚本
    • 安装forever
      1
      root@freebsd:/home/html/blog # npm install forever -g
    • 在/usr/local/etc/rc.d目录中编写tkserver服务脚本
      1
      root@freebsd:/home/html/blog # vim /usr/local/etc/rc.d/tkserver
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      #!/bin/sh

      # Copyright (c) 2015, Randy Westlund. All rights reserved.
      #
      # Redistribution and use in source and binary forms, with or without
      # modification, are permitted provided that the following conditions are met:
      #
      # 1. Redistributions of source code must retain the above copyright notice,
      # this list of conditions and the following disclaimer.
      #
      # 2. Redistributions in binary form must reproduce the above copyright notice,
      # this list of conditions and the following disclaimer in the documentation
      # and/or other materials provided with the distribution.
      #
      # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
      # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
      # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
      # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
      # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
      # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
      # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
      # POSSIBILITY OF SUCH DAMAGE.


      # This is a FreeBSD rc script for starting a node process and keeping it alive.
      # It requires forever.js to be installed: 'npm install -g forever'.

      # In addition to node_enable, the following rc variables should be defined:

      # node_msg The name of your program, printed at start. Defaults to "node".
      # node_dir The directory where your node files live. Must be defined.
      # node_logdir The directory for logfiles. Defaults to ${node_dir}/logs.
      # node_user Passed to noded process as process.env.USER. Defaults to "www".
      # node_group Passed to noded process as process.env.GROUP. Defaults to "www".
      # node_user and node_group may be used to drop node's root
      # privileges after binding to ports.
      # node_app Application main script. Defaults to "/bin/www" (relative
      # to node_user's home
      # node_forever forever binary file path. Defaults to "/usr/local/bin/forever".
      # node_local_forever use local forever binary
      # (ie. node_user's home/node_modules/.bin/forever)
      # node_forever_log forever log file. Defaults to /var/log/forever.log.

      # PROVIDE: node
      # REQUIRE: LOGIN mongod
      # KEYWORD: shutdown

      . /etc/rc.subr

      name="tkserver"
      rcvar="${name}_enable"

      start_precmd="${name}_prestart"
      start_cmd="${name}_start"
      stop_cmd="${name}_stop"

      # node executable
      command="/usr/local/bin/${name}"
      pidfile="/var/run/${name}.pid"

      # forever needs a path for each command
      PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin

      # get rc vars
      load_rc_config $name
      : ${node_enable:="no"}
      : ${node_forever:="/usr/local/bin/forever"}
      : ${node_app:="/usr/local/lib/node_modules/tkserver/server.js"}
      : ${tkserver_dir:="/home/html/blog"}

      # any other checks go here
      tkserver_prestart()
      {
      echo "tkserver starting"
      }

      tkserver_start()
      {
      ${node_forever} start --pidFile ${pidfile} --workingDir ${tkserver_dir} ${node_app}
      }

      tkserver_stop()
      {
      ${node_forever} stop --killSignal SIGTERM `cat ${pidfile}`
      }

      run_rc_command "$1"
      1
      root@freebsd:/home/html/blog # chmod 755 /usr/local/etc/rc.d/tkserver
    • 编辑/etc/rc.conf,并添加
      1
      tkserver_enable="YES"
    • 启动tkserver服务
      1
      2
      3
      4
      5
      6
      7
      8
      9
      root@freebsd:/home/html/blog # service tkserver start
      tkserver starting
      warn: --minUptime not set. Defaulting to: 1000ms
      warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
      info: Forever processing file: /usr/local/lib/node_modules/tkserver/server.js
      (node:48269) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
      (Use `node --trace-warnings ...` to show where the warning was created)
      (node:48269) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
      (node:48269) [DEP0060] DeprecationWarning: The `util._extend` API is deprecated. Please use Object.assign() instead.
  • nginx https反代http

    由于twikoo本身不支持https, 而我的blog已全面启用https,这就需要我们通过nginx来实现https反向代理http

    • 修改/usr/local/etc/nginx/nginx.conf
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      upstream twi {
      server 192.168.1.4:8080; # 192.168.1.4系统服务器所在内网地址
      }

      server {
      listen 443 ssl;
      listen [::]:64443 ssl;
      http2 on;
      server_name twi.mould.club;

      ssl_certificate /home/certs/mould.club/mould.club.chained.crt;
      ssl_certificate_key /home/certs/mould.club/mould.club.key;

      ssl_session_cache shared:SSL:1m;
      ssl_session_timeout 5m;

      ssl_protocols TLSv1.2 TLSv1.3;
      ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
      ssl_prefer_server_ciphers on;

      location / {
      proxy_pass http://twi;
      }
      }