如何在Debian 9上安装NodeBB论坛
杨和超
・15 分钟阅读
使用不同的系统?
nodeBB是基于Node.js的论坛,它利用网络套接字进行即时交互和实时通知,NodeBB源代码托管在github上,本指南将引导你,在全新Debian 9实例上,使用node js MongoDB作为数据库,nginx作为反向代理,以及acme sh作为SSL证书,完成NodeBB的安装过程。
要求
NodeBB需要安装以下软件:
- Git
- Node.js版本6.9.0或更高版本
- MongoDB 2.6或更高版本
- Nginx
- 最低1024MB RAM
- 设置了
A
/AAAA
记录的域名
开始之前
查看Debian版本。
lsb_release -ds
# Debian GNU/Linux 9.4 (stretch)
确保你的系统是最新的。
apt update && apt upgrade -y
安装必要的软件包。
apt install -y sudo dirmngr git build-essential apt-transport-https
使用sudo
访问创建一个新的non-root
用户帐户,并且切换到它。
adduser johndoe --gecos"John Doe"
usermod -aG sudo johndoe
su - johndoe
注:将johndoe
替换为你的用户名。
设置时区。
sudo dpkg-reconfigure tzdata
安装Node.js
NodeBB由node js驱动,需要安装,建议安装当前版本的Node.js 。
从NodeSource存储库安装Node.js 。
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt install -y nodejs
验证Node.js和npm的安装。
node -v && npm -v
# v10.15.0
# 5.6.0
安装和配置MongoDB
MongoDB是NodeBB的默认数据库。
从官方的MongoDB库安装MongoDB 。
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo"deb https://repo.mongodb.org/apt/debian stretch/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
sudo apt update
sudo apt install -y mongodb-org
检查版本。
mongo --version | head -n 1 && mongod --version | head -n 1
# MongoDB shell version v4.00
# db version v4.0.0
启动并启用MongoDB 。
sudo systemctl start mongod.service
sudo systemctl enable mongod.service
为NodeBB创建一个MongoDB数据库和用户。
连接到MongoDB 。
mongo
切换到the admin
数据库。
> use admin
创建管理用户。
> db.createUser( { user:"admin", pwd:"<Enter a secure password>", roles: [ { role:"readWriteAnyDatabase", db:"admin" }, { role:"userAdminAnyDatabase", db:"admin" } ] } )
注:替换占位符 <Enter a secure password>
使用你自己选择的密码。
添加一个名为nodebb
的新数据库。
> use nodebb
数据库将被创建,上下文切换到nodebb
,接下来,使用适当的权限创建nodebb
用户。
> db.createUser( { user:"nodebb", pwd:"<Enter a secure password>", roles: [ { role:"readWrite", db:"nodebb" }, { role:"clusterMonitor", db:"admin" } ] } )
注:再次,替换占位符 <Enter a secure password>
使用你自己选择的密码。
退出Mongo shell 。
> quit()
重新启动MongoDB,并且验证以前创建的管理用户可以连接。
sudo systemctl restart mongod.service
mongo -u admin -p your_password --authenticationDatabase=admin
安装和配置Nginx
从官方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/debian/ $(lsb_release -sc) nginxndeb-src https://nginx.org/packages/mainline/debian/ $(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
默认情况下,NodeBB在端口4567
上运行,为了避免输入http://example.com:4567
,我们将Nginx配置为NodeBB应用程序的反向代理,端口80
或443
(如果使用SSL )上的每个请求都将被转发到端口4567
。
运行 sudo vim /etc/nginx/conf.d/nodebb.conf
并用下面显示的基本反向代理配置填充它。
server {
listen [::]:80;
listen 80;
server_name forum.example.com;
root /usr/share/nginx/html;
client_max_body_size 50M;
location /.well-known/acme-challenge/ {
allow all;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_hide_header X-Powered-By;
proxy_set_header X-Nginx-Proxy true;
proxy_pass http://127.0.0.1:4567;
proxy_redirect off;
# Socket.IO Support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection"upgrade";
}
}
在上面的配置中,用你的域/主机名更新server_name
指令。
检查配置。
sudo nginx -t
重新加载Nginx 。
sudo systemctl reload nginx.service
安装Acme.sh客户端,并且获取let's Encrypt证书(可选)
用HTTPS保护论坛是不必要的,但是,它会保护你的网站流量,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 ~
source ~/.bashrc
检查版本。
acme.sh --version
# v2.7.9
获取forum.example.com
的RSA和ECDSA证书。
# RSA 2048
sudo /etc/letsencrypt/acme.sh --issue --home /etc/letsencrypt -d forum.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 forum.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/forum.example.com
- ecc/ecdsa :
/etc/letsencrypt/forum.example.com_ecc
从let's Encrypt获得证书之后,我们需要配置Nginx以便使用它们。
运行 sudo vim /etc/nginx/conf.d/nodebb.conf
再次将Nginx配置为HTTPS反向代理。
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
listen [::]:80;
listen 80;
server_name forum.example.com;
root /usr/share/nginx/html;
client_max_body_size 50M;
location /.well-known/acme-challenge/ {
allow all;
}
# RSA
ssl_certificate /etc/letsencrypt/forum.example.com/fullchain.cer;
ssl_certificate_key /etc/letsencrypt/forum.example.com/forum.example.com.key;
# ECDSA
ssl_certificate /etc/letsencrypt/forum.example.com_ecc/fullchain.cer;
ssl_certificate_key /etc/letsencrypt/forum.example.com_ecc/forum.example.com.key;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:4567;
proxy_redirect off;
# Socket.IO Support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection"upgrade";
}
}
检查配置。
sudo nginx -t
重新加载Nginx 。
sudo systemctl reload nginx.service
安装NodeBB
创建文档root目录。
sudo mkdir -p /var/www/nodebb
将/var/www/nodebb
目录的所有权更改为johndoe
。
sudo chown -R johndoe:johndoe /var/www/nodebb
导航到文档根目录。
cd/var/www/nodebb
将最新的NodeBB克隆到文档根文件夹中。
git clone -b v1.10.x https://github.com/NodeBB/NodeBB.git .
运行NodeBB setup命令,并且在出现提示时回答每个问题。
./nodebb setup
完成NodeBB设置后,运行./nodebb start
手动启动NodeBB服务器。
./nodebb start
在这个命令之后,你就可以访问你的论坛了。
将NodeBB作为系统服务运行
当通过./nodebb start
启动时,NodeBB将不会在系统重新启动时自动重新启动,为了避免这种情况,我们需要将NodeBB设置为系统服务。
如果正在运行,请停止NodeBB 。
./nodebb stop
创建一个新的nodebb
用户:
sudo adduser nodebb
将/var/www/nodebb
目录的所有权更改为nodebb
。
sudo chown -R nodebb:nodebb /var/www/nodebb
创建nodebb.service
systemd单元配置文件,这个单元文件将处理NodeBB deamon的启动,运行 sudo vim /etc/systemd/system/nodebb.service
并使用以下内容填充该文件:
[Unit]
Description=NodeBB
Documentation=https://docs.nodebb.org
After=system.slice multi-user.target mongod.service
[Service]
Type=forking
User=nodebb
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodebb
Environment=NODE_ENV=production
WorkingDirectory=/var/www/nodebb
PIDFile=/var/www/nodebb/pidfile
ExecStart=/usr/bin/env node loader.js
Restart=always
[Install]
WantedBy=multi-user.target
注:根据你选择的名称设置用户名和目录路径。
启动时启用nodebb.service
,并立即启动nodebb.service
。
sudo systemctl enable nodebb.service
sudo systemctl start nodebb.service
检查nodebb.service
状态。
sudo systemctl status nodebb.service
sudo systemctl is-enabled nodebb.service
就这样,你的NodeBB实例现在已启动,并且正在运行。