wiki.js部署指南

前言

在日常的学习和工作中,会遇到各种各样的知识点和经验,为了能更好的记录整理这些知识点和经验,搭建一个属于自己的wiki知识库还是很有必要的。

一款好的 Wiki 工具不仅要能够稳定地保存我们的知识经验,同时也要能够尽量有条理和美观地展示这些知识及它们之间的联系,还需要方便查阅和更新。基于以上几点要求,在众多的开源wiki程序中我个人比较喜欢的是Wiki.js

简介

20231207-01.png

Wiki.js,一款基于 NodeJS 构建的现代、轻量级且功能强大的 wiki 应用程序。专注于简单性、性能和可扩展性。通过 Wiki.js,团队可以轻松地创建、组织和共享文档,促进协作和知识管理。致力于通过 Wiki.js 提供的美观而直观的界面,让文档编写成为一种愉悦的体验!

官网:https://js.wiki/

Github:https://github.com/Requarks/wiki

Wiki.js支持多种方式安装,下面将介绍Linux及Docker安装方法。

主要特性

  • 1、直观的用户界面
    • Wiki.js 提供直观、现代且易于使用的用户界面,使用户能够快速创建和编辑文档。无论是新手还是经验丰富的用户,都能够轻松上手。
  • 2、强大的编辑器
    • 内置的 Markdown 编辑器使得编辑文档变得简单而灵活。同时,支持实时预览,让用户能够立即看到他们的更改。
  • 3、多语言支持
    • Wiki.js 提供多语言支持,可以轻松切换和管理多语言文档。这使得 Wiki.js 成为跨国企业和全球团队的理想选择。
  • 4、强大的搜索功能
    • Wiki.js 配备了高效的搜索引擎,帮助用户迅速找到所需的文档和信息。搜索结果按相关性排序,提供更好的用户体验。
  • 5、高度可定制性
    • Wiki.js 具有高度可定制的主题和布局,使您可以根据团队或组织的需求进行个性化定制。从颜色到品牌标识,都可以轻松调整。

1、Linux安装

1.1、环境准备

1.1.1 安装Node.js

Node.js支持12.0,14.0,16.0,18.0及20.0版本,本文采用16版本。

Node.js官网:https://nodejs.org/en

Node.js下载地址:https://nodejs.org/en/download/current

1
2
3
4
5
6
7
8
$ tar -xvf node-v16.15.0-linux-x64.tar.gz
$ mv node-v16.15.0-linux-x64/ nodejs_16.15
$ cd nodejs_16.15/

# 设置环境变量
$ vim /etc/profile
export NODE_HOME=/opt/nodejs_16.15
export PATH=$NODE_HOME/bin:$PATH:$NODE_HOME/data/node_global/bin

1.1.2 安装PostgreSQL

数据库推荐使用PostgreSQL 9.5及更新的版本,本文采用16版本作为示例;

PostgreSQL官网提供各种系统的安装方法,地址:https://www.postgresql.org/

打开 PostgreSQL 官网,点击菜单栏上的 Download ,可以看到这里包含了很多平台的安装包,包括 Linux、Windows、Mac OS等 。

首先我们先选择对应的操作系统,如下图

20231207-02.png

选择完成后会自动跳转下一页,如下图

20231207-03.png

第一个选择的是数据库版本;

第二个选择的是系统版本;

第三个选择的是架构,

第四个就是安装步骤,需要我们复制到Linux命令行执行,执行完成后即安装完成。

1.1.3 创建数据库及用户

1
2
3
4
5
6
7
8
9
10
$ sudo -u postgres psql

# 创建用户
$ CREATE USER wiki WITH PASSWORD 'your password';

# 创建数据库
$ CREATE DATABASE wiki OWNER wiki;

# 授权
$ GRANT ALL PRIVILEGES ON DATABASE wiki TO wiki;

1.2、安装Wiki.js

1.2.1 下载wiki.js包

1
wget https://github.com/Requarks/wiki/releases/latest/download/wiki-js.tar.gz

1.2.2 解压

1
2
3
mkdir wiki
tar xzf wiki-js.tar.gz -C ./wiki
cd ./wiki

1.2.3 修改配置文件

这里主要修改数据库部分

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
mv config.sample.yml config.yml
vim config.yml

#######################################################################
# Wiki.js - CONFIGURATION #
#######################################################################
# Full documentation + examples:
# https://docs.requarks.io/install

# ---------------------------------------------------------------------
# Port the server should listen to
# ---------------------------------------------------------------------

port: 3001

# ---------------------------------------------------------------------
# Database
# ---------------------------------------------------------------------
# Supported Database Engines:
# - postgres = PostgreSQL 9.5 or later
# - mysql = MySQL 8.0 or later (5.7.8 partially supported, refer to docs)
# - mariadb = MariaDB 10.2.7 or later
# - mssql = MS SQL Server 2012 or later
# - sqlite = SQLite 3.9 or later

db:
type: postgres

# PostgreSQL / MySQL / MariaDB / MS SQL Server only:
host: localhost
port: 5432
user: wiki # 1.1.3创建的用户
pass: wikijs # 1.1.3创建用户时指定的密码
db: wiki # 1.1.3创建的数据库
ssl: false

# Optional - PostgreSQL / MySQL / MariaDB only:
# -> Uncomment lines you need below and set `auto` to false
# -> Full list of accepted options: https://nodejs.org/api/tls.html#tls_tls_createsecurecontext_options
sslOptions:
auto: true
# rejectUnauthorized: false
# ca: path/to/ca.crt
# cert: path/to/cert.crt
# key: path/to/key.pem
# pfx: path/to/cert.pfx
# passphrase: xyz123

# Optional - PostgreSQL only:
schema: public

# SQLite only:
# storage: path/to/database.sqlite

#######################################################################
# ADVANCED OPTIONS #
#######################################################################
# Do not change unless you know what you are doing!

# ---------------------------------------------------------------------
# SSL/TLS Settings
# ---------------------------------------------------------------------
# Consider using a reverse proxy (e.g. nginx) if you require more
# advanced options than those provided below.

ssl:
enabled: false
port: 3443

# Provider to use, possible values: custom, letsencrypt
provider: custom

# ++++++ For custom only ++++++
# Certificate format, either 'pem' or 'pfx':
format: pem
# Using PEM format:
key: /opt/certs/wiki.zhoumx.net/wiki.zhoumx.net.key
cert: /opt/certs/wiki.zhoumx.net/fullchain.cer
# Using PFX format:
pfx: path/to/cert.pfx
# Passphrase when using encrypted PEM / PFX keys (default: null):
passphrase: null
# Diffie Hellman parameters, with key length being greater or equal
# to 1024 bits (default: null):
dhparam: null

# ++++++ For letsencrypt only ++++++
domain: wiki.yourdomain.com
subscriberEmail: admin@example.com

# ---------------------------------------------------------------------
# Database Pool Options
# ---------------------------------------------------------------------
# Refer to https://github.com/vincit/tarn.js for all possible options

pool:
# min: 2
# max: 10

# ---------------------------------------------------------------------
# IP address the server should listen to
# ---------------------------------------------------------------------
# Leave 0.0.0.0 for all interfaces

bindIP: 0.0.0.0

# ---------------------------------------------------------------------
# Log Level
# ---------------------------------------------------------------------
# Possible values: error, warn, info (default), verbose, debug, silly

logLevel: info

# ---------------------------------------------------------------------
# Log Format
# ---------------------------------------------------------------------
# Output format for logging, possible values: default, json

logFormat: default

# ---------------------------------------------------------------------
# Offline Mode
# ---------------------------------------------------------------------
# If your server cannot access the internet. Set to true and manually
# download the offline files for sideloading.

offline: false

# ---------------------------------------------------------------------
# High-Availability
# ---------------------------------------------------------------------
# Set to true if you have multiple concurrent instances running off the
# same DB (e.g. Kubernetes pods / load balanced instances). Leave false
# otherwise. You MUST be using PostgreSQL to use this feature.

ha: false

# ---------------------------------------------------------------------
# Data Path
# ---------------------------------------------------------------------
# Writeable data path used for cache and temporary user uploads.
dataPath: /opt/wiki

# ---------------------------------------------------------------------
# Body Parser Limit
# ---------------------------------------------------------------------
# Maximum size of API requests body that can be parsed. Does not affect
# file uploads.

bodyParserLimit: 5mb

1.2.4 运行wiki.js

1
node server

访问测试:使用浏览器访问http://ip:3001

1.2.5 作为服务运行,加入systemd

我们使用node命令运行是在前台运行,当我们退出当前shell后就会中断,所以我们需要将它作为服务运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vim /etc/systemd/system/wiki.service

[Unit]
Description=Wiki.js
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/node server
Restart=always
# Consider creating a dedicated user for Wiki.js here:
User=nobody
Environment=NODE_ENV=production
WorkingDirectory=/home/wwwroot/wiki #修改为你存放wiki的目录

[Install]
WantedBy=multi-user.target

运行服务

1
systemctl start wiki

在系统启动时启用该服务

1
systemctl enable wiki

1.2.6 查看日志

1
journalctl -u wiki

2、Docker安装

下文演示操作系统为ubuntu,支持Ubuntu 18.04 / 20.04 / 22.04 LTS

2.1 环境准备

1.1.1 更新操作系统

1
2
3
$ sudo apt -qqy update

$ sudo DEBIAN_FRONTEND=noninteractive apt-get -qqy -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold' dist-upgrade

2.1.2 安装docker

1
2
3
4
5
6
7
8
9
10
11
# 安装依赖
$ sudo apt -qqy -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold' install ca-certificates curl gnupg lsb-release

# 注册 Docker 包仓库
$ sudo mkdir -p /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 刷新软件包并安装 Docker
$ sudo apt -qqy update
$ sudo apt -qqy -o Dpkg::Options::='--force-confdef' -o Dpkg::Options::='--force-confold' install docker-ce docker-ce-cli containerd.io docker-compose-plugin

2.2 部署容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 创建 Wiki.js 的安装目录
$ mkdir -p /etc/wiki

# 生成数据库密钥
$ openssl rand -base64 32 > /etc/wiki/.db-secret

# 创建内部 Docker 网络
$ docker network create wikinet

# 为 PostgreSQL 创建数据卷
$ docker volume create pgdata

# 创建容器
$ docker create --name=db -e POSTGRES_DB=wiki -e POSTGRES_USER=wiki -e POSTGRES_PASSWORD_FILE=/etc/wiki/.db-secret -v /etc/wiki/.db-secret:/etc/wiki/.db-secret:ro -v pgdata:/var/lib/postgresql/data --restart=unless-stopped -h db --network=wikinet postgres:15

$ docker create --name=wiki -e DB_TYPE=postgres -e DB_HOST=db -e DB_PORT=5432 -e DB_PASS_FILE=/etc/wiki/.db-secret -v /etc/wiki/.db-secret:/etc/wiki/.db-secret:ro -e DB_USER=wiki -e DB_NAME=wiki -e UPGRADE_COMPANION=1 --restart=unless-stopped -h wiki --network=wikinet -p 80:3000 -p 443:3443 ghcr.io/requarks/wiki:2

$ docker create --name=wiki-update-companion -v /var/run/docker.sock:/var/run/docker.sock:ro --restart=unless-stopped -h wiki-update-companion --network=wikinet ghcr.io/requarks/wiki-update-companion:latest

2.3 启动容器

1
2
3
$ docker start db # 数据库容器
$ docker start wiki # wiki程序
$ docker start wiki-update-companion # wiki更新助手

3、升级

3.1 Linux安装环境

1、停止wiki程序

1
$ systemctl stop wiki

2、备份配置文件

1
$ cp wiki/config.yml ~/config.yml.bak

3、删除应用目录

1
$ rm -rf /etc/wiki/*

4、下载最新版wiki程序

1
$ wget https://github.com/Requarks/wiki/releases/latest/download/wiki-js.tar.gz

5、解压

1
2
$ tar xzf wiki-js.tar.gz -C ./wiki
$ cd wiki

6、还原配置文件

1
$ cp ~/config.yml.bak ./config.yml

7、启动wiki程序

1
$ systemctl start wiki

3.2 Docker安装环境

在第二章安装期间,您安装了 Wiki.js 更新助手。当有新版本可用时,此功能会自动执行升级过程。

当有新版本可用时,您将在“管理区域”中收到通知。要执行升级,请按照以下简单说明操作:

  • 1、转到“系统信息”部分,然后单击“执行升级”按钮。
  • 2、等待该过程完成。
  • 3、您现在应该使用的是最新版本。就是这么简单!

4、管理

4.1 配置nginx反向代理

当你拥有域名和公网ip时,可以使用nginx反代进行域名访问。

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
server
{
listen 80;
listen [::]:80;
server_name 域名;
return 301 https://域名$request_uri;
access_log /home/wwwlogs/wiki.log;
}

server
{
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name 域名;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/wiki;

ssl_certificate 证书;
ssl_certificate_key 私钥;
ssl_session_timeout 5m;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
ssl_session_cache builtin:1000 shared:SSL:10m;
# openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

include rewrite/other.conf;
#error_page 404 /404.html;

# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

include enable-php.conf;

location / {
proxy_pass http://127.0.0.1:3001;
}

access_log /home/wwwlogs/wiki.log;
}

5、wiki数据库迁移

5.1 Linux安装环境

备份数据库

1
pg_dump wiki -U wiki -F c > wikibackup.dump

还原数据库

1
cat ~/wikibackup.dump | pg_restore -U wiki -d wiki

5.2 Docker安装环境

备份数据库

1
docker exec db pg_dump wiki -U wiki -F c > wikibackup.dump

拷贝备份文件

1
rsync -P wikibackup.dump root@YOUR-NEW-SERVER-IP:~/wikibackup.dump

还原数据库

1
2
3
$ docker exec -it db dropdb -U wiki wiki
$ docker exec -it db createdb -U wiki wiki
$ cat ~/wikibackup.dump | docker exec -i db pg_restore -U wiki -d wiki

wiki.js部署指南
https://www.zhoumx.net/wiki.js部署指南.html
作者
阿星
发布于
2022年10月16日
许可协议