如何在Ubuntu 18.04上安装Wiki.js
Tianye
・12 分钟阅读
使用不同的系统?
Wiki.js是一个免费开源的基于Node.js,Git和Markdown的现代wiki应用程序,Wiki.js源代码是公共托管在GitHub ,本指南将向你展示,如何使用Nodejs MongoDB, PM2, nginx, git和Acme.sh在新的Ubuntu 18.04实例上安装Wiki.js。
要求
运行Wiki.js和完成本指南的要求如下:
- Node.js版本6.9.0或更高版本
- 版本3.2或更高版本
- Nginx
- Git版本2.7.4或更高版本
- 最小768MB内存
- 设置了
A
/AAAA
记录的域名
检查Ubuntu版本。
lsb_release -ds
# Ubuntu 18.04.4 LTS
使用sudo
访问创建一个新的non-root
用户帐户,并且切换到它。
adduser johndoe --gecos\\\"John Doe\\\"
usermod -aG sudo johndoe
su - johndoe
注:会johndoe
替换为你的用户名。
设置时区。
sudo dpkg-reconfigure tzdata
确保你的系统是最新的。
sudo apt update && sudo apt upgrade -y
安装必要的软件包。
sudo apt install -y build-essential apt-transport-https
安装GIT
Git 2.7.4预装在Ubuntu服务器版本上,因此我们不需要安装它。如果要安装较新版本,可以使用第三方PPA或从源代码中编译最新版本的Git 。
你可以通过运行以下命令来验证当前安装的Git版本:
git --version
# git version 2.7.4
如果要安装新版本的Git软件,可以使用以下命令。
# Remove existing git package
sudo apt remove -y git
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:git-core/ppa
sudo apt update && sudo apt upgrade -y
sudo apt install -y git
验证Git版本。
git --version
# git version 2.17.0
安装Node.js
通过安装NodeSource来安装node.js 。
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt install -y nodejs
检查Node.js和npm版本。
node -v && npm -v
# v8.11.2
# 5.6.0
安装mongodb
我们会使用官方MongoDB存储库,它与最近主要的和次要的MongoDB版本保持同步。
安装MongoDB社区版。
sudo apt install -y mongodb
检查版本。
mongo --version | head -n 1 && mongod --version | head -n 1
# MongoDB shell version v3.6.3
# db version v3.6.3
安装和配置Nginx
安装Nginx 。
wget https://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key
rm nginx_signing.key
sudo -s
printf\\\"deb https://nginx.org/packages/mainline/ubuntu/ $(lsb_release -sc) nginxndeb-src https://nginx.org/packages/mainline/ubuntu/ $(lsb_release -sc) nginxn\\\" >> /etc/apt/sources.list.d/nginx_mainline.list
exit
sudo apt update
sudo apt install -y nginx
检查版本。
sudo nginx -v
# nginx version: nginx/1.15.0
启用和启动Nginx 。
sudo systemctl enable nginx.service
sudo systemctl start nginx.service
将Nginx配置为Wiki.js应用程序的反向代理。
运行sudo vim /etc/nginx/conf.d/wiki.js.conf
然后用下面的基本反向代理配置填充它。
server {
listen [::]:80;
listen 80;
server_name wiki.example.com;
root /usr/share/nginx/html;
charset utf-8;
client_max_body_size 50M;
location /.well-known/acme-challenge/ {
allow all;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection\\\"upgrade\\\";
proxy_next_upstream error timeout http_502 http_503 http_504;
}
}
在上述配置中你需要更改的惟一东西是server_name
指令,如果你决定配置除3000
之外的其他端口,则可能是proxy_pass
指令,Wiki.js默认使用端口3000
。
检查配置。
sudo nginx -t
重新加载Nginx 。
sudo systemctl reload nginx.service
安装Acme.sh客户端,并且获取let\\\'s Encrypt 证书(可选),
使用HTTPS保护你的wiki不是必需的,但是,它可以保护你网站的流量。Acme.sh是一个纯unix shell软件,用于从let\\\'s Encrypt获取SSL证书。
下载并安装Acme.sh 。
sudo mkdir /etc/letsencrypt
git clone https://github.com/Neilpang/acme.sh.git
cd acme.sh
sudo ./acme.sh --install --home /etc/letsencrypt --accountemail your_email@example.com
cd ~
检查版本。
/etc/letsencrypt/acme.sh --version
获取wiki.example.com
域/主机名的RSA和ECDSA证书。
# RSA 2048
sudo /etc/letsencrypt/acme.sh --issue --home /etc/letsencrypt -d wiki.example.com --webroot /usr/share/nginx/html --reloadcmd\\\"sudo systemctl reload nginx.service\\\" --accountemail your_email@example.com --ocsp-must-staple --keylength 2048
# ECDSA/ECC P-256
sudo /etc/letsencrypt/acme.sh --issue --home /etc/letsencrypt -d wiki.example.com --webroot /usr/share/nginx/html --reloadcmd\\\"sudo systemctl reload nginx.service\\\" --accountemail your_email@example.com --ocsp-must-staple --keylength ec-256
运行上述命令后,你的证书和密钥将位于:
- 对于RSA :
/etc/letsencrypt/wiki.example.com
目录, - 对于ecc/ecdsa :
/etc/letsencrypt/wiki.example.com_ecc
目录,
从let\\\'s Encrypt获取证书后,我们需要配置Nginx才能使用它们。
运行sudo vim /etc/nginx/conf.d/wiki.js.conf
再次将Nginx配置为HTTPS
反向代理。
server {
listen [::]$1 ssl http2;
listen 443 ssl http2;
listen [::]$2;
listen 80;
server_name wiki.example.com;
root /usr/share/nginx/html;
charset utf-8;
client_max_body_size 50M;
location /.well-known/acme-challenge/ {
allow all;
}
# RSA
ssl_certificate /etc/letsencrypt/wiki.example.com/fullchain.cer;
ssl_certificate_key /etc/letsencrypt/wiki.example.com/wiki.example.com.key;
# ECDSA
ssl_certificate /etc/letsencrypt/wiki.example.com_ecc/fullchain.cer;
ssl_certificate_key /etc/letsencrypt/wiki.example.com_ecc/wiki.example.com.key;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection\\\"upgrade\\\";
proxy_next_upstream error timeout http_502 http_503 http_504;
}
}
检查配置。
sudo nginx -t
重新加载Nginx 。
sudo systemctl reload nginx.service
安装Wiki.js
创建Wiki.js应该安装的空文档根目录。
sudo mkdir -p /var/www/wiki.example.com
导航到文档根目录。
cd /var/www/wiki.example.com
将/var/www/wiki.example.com文件夹的所有权更改为用户johndoe 。
sudo chown -R johndoe:johndoe /var/www/wiki.example.com
从/var/www/wiki.example.com
文件夹中运行以下命令下载和安装Wiki.js 。
curl -sSo- https://wiki.js.org/install.sh | bash
要查看当前安装的Wiki.js版本,可以运行以下命令。
node wiki --version
# 1.0.78
安装完成后,系统会提示你运行配置向导。
启动配置向导。
node wiki configure
这会通知你浏览http://localhost:3000
以配置Wiki.js ,如果你在Wiki.js面前有Nginx,你可以打开你的域名(例如,http://wiki.example.com
),而不是去localhost
。
使用浏览器,导航到http://wiki.example.com
,并且按照屏幕指示操作,在配置向导中输入的所有设置都保存在config.yml
文件中,配置向导将自动为你启动Wiki.js 。
安装PM2
默认情况下,在系统重新启动后,wiki不会自动启动,为了使它在引导时启动,我们需要设置PM2,PM2作为本地NPM模块捆绑在Wiki.js中,因此我们不需要在全局上安装PM2.
告诉PM2将自身配置为启动服务。
/var/www/wiki.example.com/node_modules/pm2/bin/pm2 startup
最后,保存当前的PM2配置。
/var/www/wiki.example.com/node_modules/pm2/bin/pm2 save
Wiki.js作为后台进程运行,使用PM2作为进程管理器。