如何在CentOS 7上安装Wiki.js
Vera
・14 分钟阅读
使用不同的系统?
Wiki.js是一个免费的开源应用程序,它构建在node.js,MongoDB,git和Markdown之上,Wiki.js源代码公共托管在GitHub ,本指南将向你展示,如何使用Nodejs MongoDB, PM2, nginx, git和Acme.sh在新的CentOS 7实例上安装Wiki.js。
要求
运行Wiki.js的要求如下:
- Node.js版本6.9.0或更高版本
- 版本3.2或更高版本
- Nginx
- Git版本2.7.4或更高版本
- 一个符合git的存储库(公用或私有)(可选)
- 最低768MB RAM
- 设置了
A
/AAAA
记录的域名
检查CentOS版本。
cat /etc/centos-release
# CentOS Linux release 7.5.1804 (Core)
使用sudo
访问创建一个新的non-root
用户帐户,并且切换到它。
useradd -c\\\"John Doe\\\" johndoe && passwd johndoe
usermod -aG wheel johndoe
su - johndoe
注:将johndoe
替换为你的用户名。
设置时区。
timedatectl list-timezones
sudo timedatectl set-timezone \\\'Region/City\\\'
确保你的系统是最新的。
sudo yum update -y
安装必要的软件包。
sudo yum install -y wget curl vim zip unzip bash-completion
禁用SELinux和防火墙。
sudo setenforce 0
sudo systemctl stop firewalld
sudo systemctl disable firewalld
启用EPEL存储库。
sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
安装git
CentOS存储库提供了一个非常过时的Git版本,因此我们需要从源代码构建Git。
安装Git从源代码中编译。
# Remove existing git package if installed:
sudo yum remove -y git
sudo yum groupinstall -y\\\"Development Tools\\\"
sudo yum install -y gettext-devel openssl-devel perl-CPAN perl-devel zlib-devel curl-devel
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.17.1.tar.gz && tar zxvf git-2.17.1.tar.gz
rm git-2.17.1.tar.gz
cd git-2.17.1
make configure
./configure
make prefix=/usr/local all
sudo make prefix=/usr/local install
cd ~
# Confirm this command returns /usr/local/bin/git:
which git
验证版本。
git --version
# git version 2.17.1
安装node.js
Wiki.js需要Node.js 6.9.0或更高版本,因此我们首先需要安装node.js 。
使用node js的NodeSource YUM存储库安装node js 。
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
sudo yum install -y nodejs
检查Node.js和npm版本。
node -v && npm -v
# v8.11.2
# 5.6.0
安装mongodb
Wiki.js使用MongoDB作为数据库引擎,我们将使用官方MongoDB存储库,它包含最新的主要版本和次要版本。
安装MongoDB社区版。
sudo vim /etc/yum.repos.d/mongodb-org-3.6.repo
# Copy/paste this
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
sudo yum install -y mongodb-org
检查版本。
mongo --version | head -n 1 && mongod --version | head -n 1
# MongoDB shell version v3.6.5
# db version v3.6.5
启用和启动MongoDB 。
sudo systemctl enable mongod.service
sudo systemctl start mongod.service
安装和配置nginx
强烈建议在Wiki.js前面放置一个标准的web服务器,这可以确保你可以使用SSL,多网站,缓存等功能。
安装Nginx 。
sudo vim /etc/yum.repos.d/nginx_mainline.repo
# Copy/paste this
[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=1
enabled=1
wget https://nginx.org/keys/nginx_signing.key
sudo rpm --import nginx_signing.key
rm nginx_signing.key
sudo yum install -y nginx
检查版本。
nginx -v
# nginx version: nginx/1.15.0
启用和启动Nginx 。
sudo systemctl enable nginx.service
sudo systemctl start nginx.service
将Nginx配置为Wiki.js的HTTP
或HTTPS
反向代理。
运行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,但是,保护你的网站流量是一种很好的做法。为了从let\\\'s Encrypt获得SSL证书,我们将使用Acme.sh客户端,Acme.sh是一个纯UNIX shell script,用于从let\\\'s Encrypt获得SSL证书,它依赖关系为零,与其他一些需要大量依赖项才能成功运行的Acme协议客户端相比,这使得它非常轻量级。
下载并安装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
# v2.7.9
获取你的域/主机名的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
注意:不要忘记将wiki.example.com
替换为你的域名。
从让我们加密证书之后,我们需要配置Nginx以利用它们。
运行sudo vim /etc/nginx/conf.d/wiki.js.conf
再次将Nginx配置为HTTPS
反向代理。
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
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;
}
# 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将自身配置为启动服务。
/var/www/wiki.example.com/node_modules/pm2/bin/pm2 startup
最后,通过运行命令保存当前的PM2配置。
/var/www/wiki.example.com/node_modules/pm2/bin/pm2 save
使用PM2作为进程管理器,你的Wiki.js实例作为后台进程运行,你可以使用sudo reboot重新启动你的操作系统,并确保Wiki.js在重新引导后启动。