Nodebb 部署流程记录 (Nginx+MongoDB)

以下系统为 Debian

当然啦,可以用任何你喜欢的系统,只是我用 Debian 9,所以只能记录它的过程...

准备

添加 backports 套件库,有些软件可能需要这个
https://backports.debian.org/Instructions/

Debian 8 jessie-updates 库已经被删除,如需继续使用 Debian 8 需要其他替代,一些信息可以查看 Failed to fetch jessie backports repository 。因为我没有用 Debian 8 所以不能测试写在这里...

Debian 9 stretch 打开 /etc/apt/sources.list.d/sources.list 添加 deb http://ftp.debian.org/debian stretch-backports main

更新包 apt-get update

安装 web 常用的软件和套件 apt-get install dirmngr libgd2-xpm-dev build-essential libpcre3 libpcre3-dev zlib1g-dev unzip git

OpenSSL

咱要另外下载一个OpenSSL,系统上的可能太旧了
下载,解压缩,更改文件名。

wget -c  https://github.com/openssl/openssl/archive/OpenSSL_1_1_1c.tar.gz
tar xzf OpenSSL_1_1_1c.tar.gz
mv openssl-OpenSSL_1_1_1c openssl

编译安装 Nginx

下载 ngx_brotli ,Google 开发的 Brotli 压缩格式,非常好的压缩比,相比 gzip 更快。

有些教学是旧的,现在已经不需要编译 libbrotli 。

git clone https://github.com/google/ngx_brotli.git && cd ngx_brotli
git submodule update --init

注意 user=nginx 和 group=nginx 为用户和组,with-openssl=/root/openssl 是刚下载的 Openssl 目录,--add-module=/root/ngx_brotli 是 ngx_brotli 的目录。

wget -c http://nginx.org/download/nginx-1.16.0.tar.gz
tar zxf nginx-1.15.2.tar.gz&&cd nginx-1.16.0
./configure  --prefix=/usr/local/nginx \
--sbin-path=/usr/local/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--lock-path=/run/lock/subsys/nginx \
--pid-path=/run/nginx.pid \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_stub_status_module \
--without-mail_pop3_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-http_image_filter_module \
--with-http_addition_module \
--with-http_realip_module \
--with-mail_ssl_module \
--with-file-aio \
--with-openssl=/root/openssl \
--add-module=/root/ngx_brotli \
--with-openssl-opt='enable-tls1_3 enable-weak-ssl-ciphers'

make
make install

make install 结束后会显示很多重要文件被分配到的目录,如果是新 nginx 用户那么很重要,需要记住配置文件所处的位置。

创建用户和组:

groupadd -r nginx
useradd -r -g nginx nginx

为方便操作修改 vi /etc/profile 添加 PATH=$PATH:/usr/local/sbin/nginx 这样就能方便的控制 nginx 啦
直接 nginx 启动它,nginx -s reload 重启 nginx. 如正常启动没有报错的话,访问 IP 会打开一个欢迎页面~

启动 Nginx /usr/local/sbin/nginx 如正常启动没有报错的话,访问 IP 会打开一个欢迎页面~

准备安装 Nodebb

安装 Nodebb 部分内容建议参考官方文档
(Debian 页面):
https://docs.nodebb.org/installing/os/debian/

安裝 node.js

https://github.com/nodesource/distributions

按网页安装完成后 node -v 看看版本 ヘ(。□°)ヘ

安装 Yarn

安装 node.js 自带 npm,但是它在 nodebb 安装包简直是噩梦(报错魔),为身心健康我使用 Yarn 替代了 npm .

以下是官网提供的 Debian 安装流程,我想能看这篇文章大多都是个人用户,应该会用 root 权限操作,我删除了 sudo .

通过软件包安装,配置存储库。

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" |  tee /etc/apt/sources.list.d/yarn.list

更新软件包完成后安装 Yarn .

apt-get update && apt-get install yarn

安裝mongoDB

https://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat/

咱把页面划到下面就有主流系统的安装文档勒。鉴于 monggoDB 经常更新,建议直接看官方文档。

apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

创建文件 /etc/apt/sources.list.d/mongodb-org-4.0.list 并写入:

Debian 8 “Jessie” :

echo "deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

Debian 9 “Stretch” :

echo "deb http://repo.mongodb.org/apt/debian stretch/mongodb-org/4.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

更新包 apt-get update

完整安装 MongDB apt-get install -y mongodb-org

启动 MongoDB 和查看数据库状态

systemctl start mongod
systemctl status mongod

访问 MongoDB shell mongo

创建管理员密码 ( 用自己的密码替换占位符,同时删除 <> )

db.createUser( { user: "admin", pwd: "<在这里放你的密码>", roles: [ { role: "readWriteAnyDatabase", db: "admin" }, { role: "userAdminAnyDatabase", db: "admin" } ] } )

创建 Nodebb 数据库 use nodebb

db.createUser( { user: "nodebb", pwd: "<还是放你的烂密码>", roles: [ { role: "readWrite", db: "nodebb" }, { role: "clusterMonitor", db: "admin" } ] } )

退出 Mongo Shell quit()

修改 Mongo 配置文件 /etc/mongod.conf 添加以下内容启动数据库安全授权:

security:
  authorization: enabled

重新启动 Mongo systemctl restart mongod 并测试管理员是否能连接 mongo -u admin -p <你的烂密码> --authenticationDatabase=admin (使用时需要删除"你的烂密码"和 <>)

如没问题就退出 Mongo 命令,开始安装 Nodebb.

安装Nodebb

给 Nodebb 放到一个较好的位置,最好选一个合适的文件夹。进入文件夹执行以下命令。

git clone -b v1.10.x https://github.com/NodeBB/NodeBB.git nodebb

cd nodebb

启动安装程序,它会显示需要输入的信息,除管理员信息外其他配置可以之后在 config.json 修改。

配置成功后会提示 NodeBB Setup Completed

注意: 输入网址时确保输入的网址与访问论坛使用的网址相同,例如论坛网址是 http://www.example.org ,输入内容同样也是 http://www.example.org ,不能为 http://example.org 或其他。

./nodebb setup

启动 Nodebb :

./nodebb start

配置 Nginx

这里有很多个人设置和历史设置的残留,可作为参考,使用时需要对应的修改。安全证书使用 Certbot 部署。

吾多年前记录的使用流程 使用 Certbot 申请 Let's Encrypt 免费证书 启用 https ,我最近有重新安装过,略微重新编写,因为它安装步骤和介绍早很早之前就过时了....

新的 Nginx 已经不需要 ssl on; ,有在 listen 的 ssl 就可以。注意 brotli 段落,它启动了压缩。

server {
    listen 443 ssl http2 fastopen=3;

    server_name www.set-fire.com set-fire.com;

    if ($host != 'www.set-fire.com' ) {
    rewrite ^/(.*)$ https://www.set-fire.com/$1 permanent;
    }
    
    location ~ /\.well-known/acme-challenge {
	root /home/nodebb/public;
    } 

    ssl_certificate /etc/letsencrypt/live/set-fire.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/set-fire.com/privkey.pem;
    ssl_dhparam  /home/ssl/dhparams.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    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+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256;
    ssl_prefer_server_ciphers on;
    ssl_session_tickets      on;
    #ssl_stapling             on;
    #ssl_stapling_verify      on;
   
    #limit_conn addr 100;
    #limit_req zone=one burst=50;
  
    error_log /home/nodebb.log warn;
    access_log    /home/nodebb_access.log  access;

    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
                                                         
    add_header Strict-Transport-Security "max-age=15552000; includeSubDomains; preload";
 
  # Max Upload
    client_max_body_size 200m; 

    client_body_buffer_size 256k;

    set_real_ip_from 103.21.244.0/22;
    set_real_ip_from 103.22.200.0/22;
    set_real_ip_from 103.31.4.0/22;
    set_real_ip_from 104.16.0.0/12;
    set_real_ip_from 108.162.192.0/18;
    set_real_ip_from 131.0.72.0/22;
    set_real_ip_from 141.101.64.0/18;
    set_real_ip_from 162.158.0.0/15;
    set_real_ip_from 172.64.0.0/13;
    set_real_ip_from 173.245.48.0/20;
    set_real_ip_from 188.114.96.0/20;
    set_real_ip_from 190.93.240.0/20;
    set_real_ip_from 197.234.240.0/22;
    set_real_ip_from 198.41.128.0/17;
    set_real_ip_from 199.27.128.0/21;
    set_real_ip_from 2400:cb00::/32;
    set_real_ip_from 2606:4700::/32;
    set_real_ip_from 2803:f800::/32;
    set_real_ip_from 2405:b500::/32;
    set_real_ip_from 2405:8100::/32;

    real_ip_header CF-Connecting-IP;
   
  # NodeBB
  
    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_redirect off;

  # Socket.iO Support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    proxy_next_upstream error timeout http_500 http_502 http_504;

    brotli on;
    brotli_comp_level 6;
    brotli_static on;
    brotli_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/vnd.microsoft.icon image/bmp image/svg+xml;

    # gzip            on;
    #gzip_min_length 1000;
    #gzip_proxied    off;
    #gzip_types      text/plain application/xml text/javascript application/javascript application/x-javascript text/css application/json;
    
    location @nodebb {
        proxy_pass http://io_nodes;
    }

    location ~ ^/assets/(.*) {
        root /home/nodebb/;
        try_files /build/public/$1 /public/$1 @nodebb;
    }

    location /plugins/ {
        root /home/nodebb/build/public/;
        try_files $uri @nodebb;
    }

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

server {
    listen       80  default_server;
    server_name  _;
    return       404;
}

upstream io_nodes {
    ip_hash;
    server 127.0.0.1:4567;
}

嗯姆,nodebb 部署基本就是这些,也可以更简单些用 apt 安装 Nginx ,但那样会少些自定义的套件。

nodebb 包安装器 npm 更换到 yarn

编辑 config.json

{
    "package_manager": "yarn"
}

可能需要删除 nodebb 的 ./node_modules 目录让(运行) yarn 重新下载。


更新:
//06 20
删除 nginx 配置文件的 ssl on;,添加 brotli 压缩,添加些细致的东西例如直接输入它名字启动 nginx,nodebb 包安装更换到 NPM .

最后由 BlueBlue_Master 编辑

绝了居然是Debian

我用lnmp来部署但是有些问题

lnmp add vhost
之后会直接写入nginx的配置文件但是不知道什么毛病反代HTTPS会503而且用域名访问就加载不了CSS和JavaScript
还有
我讨厌编译安装

最后由 RNAtlance203 编辑

@rnatlance203 如果直接访问 nodebb 端口打开正常,试试用我那个 nginx 配置(改些什么),还是错误只能对 Nginx 编译重新安装了 ʕ •ᴥ•ʔ

最后由 BlueBlue_Master 编辑

我还是直接用cloudflare帮我生成证书吧

还有一件事,MongoDB似乎会几率性默认ban掉http://localhost,原因未知

最后由 RNAtlance203 编辑

萌新瑟瑟发抖:bubble_funny:

在 nodebb 官方论坛有一篇文,有参考作用,虽然可能很多东西是用不到的,NodeBB - Full Stack Setup,以及它的 Nginx 配置文件