本页内容

  • 前提条件
  • 安装 Node.JS
  • 安装 MongoDB 数据库
  • 安装 Let's Chat
  • 为 Let's Chat 创建 Systemd 服务文件
  • 为 Let's Chat 配置 Nginx
  • 访问 Let's Chat Web 界面
  • 结论

Let's Chat 是一款免费、开源且持久化的消息应用程序,运行于 Node.js 和 MongoDB 之上。它专为小型、紧密协作的团队设计,提供丰富的功能,例如 LDAP/Kerberos 身份验证、REST-like API 以及 XMPP 支持。使用 Let's Chat,你可以发送各类消息,还能找到兴趣群组聊天、语音贴纸和酷炫的表情符号。

在本文中,我们将向你展示如何在 Ubuntu 22.04 上以 Nginx 作为反向代理来安装 Let's Chat。

前提条件

  • 一台运行 Ubuntu 22.04 的服务器。
  • 服务器上已配置 root 密码。

安装 Node.JS

开始之前,你需要在服务器上安装 Node.js。要安装最新版本的 Node.js,你需要将 Node Source 仓库添加到 APT。

首先,使用以下命令安装所有必需的依赖项:

apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common -y

所有依赖项安装完成后,将 Node.js 仓库添加到 APT

curl -sL https://deb.nodesource.com/setup_18.x | bash -

仓库添加完成后,可以使用以下命令安装 Node.js:

apt-get install nodejs -y

安装成功后,使用以下命令验证 Node.js 版本:

node -v

你将看到以下输出:

v18.7.0

安装 MongoDB 数据库

你还需要在服务器上安装 MongoDB 数据库。

首先,使用以下命令下载并安装 libssl 软件包:

wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb

接下来,使用以下命令添加 MongoDB GPG 密钥和仓库:

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.4.list

接下来,使用以下命令更新仓库缓存:

apt update

仓库更新完成后,使用以下命令安装 MongoDB:

apt install -y mongodb-org

MongoDB 软件包安装完成后,使用以下命令启动并启用 MongoDB 服务:

systemctl start mongod
systemctl enable mongod

你可以使用以下命令检查 MongoDB 服务的状态:

systemctl status mongod

你应该会看到以下输出:

? mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-07-31 04:00:08 UTC; 6s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 42274 (mongod)
     Memory: 61.1M
        CPU: 1.014s
     CGroup: /system.slice/mongod.service
             ??42274 /usr/bin/mongod --config /etc/mongod.conf

Jul 31 04:00:08 ubuntu2204 systemd[1]: Started MongoDB Database Server.

安装 Let's Chat

首先,你需要从 Git Hub 仓库下载最新版本的 Let's Chat。你可以使用以下命令下载:

git clone https://github.com/sdelements/lets-chat.git

下载完成后,切换到下载的目录,并使用以下命令安装所有必需的依赖项:

cd lets-chat
npm install

接下来,复制示例配置文件,并使用以下命令启动 Let's Chat 服务器:

cp settings.yml.sample settings.yml
npm start

你应该会看到以下输出:

> 
[email protected]
 start
> node app.js

(node:42373) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-client
(Use `node --trace-deprecation ...` to show where the warning was created)

???     ?????????????????????????     ??????????  ??? ?????? ?????????
???     ?????????????????????????    ???????????  ????????????????????
???     ??????     ???   ????????    ???     ????????????????   ???   
???     ??????     ???   ????????    ???     ????????????????   ???   
????????????????   ???   ????????    ???????????  ??????  ???   ???   
????????????????   ???   ????????     ??????????  ??????  ???   ???

Release 0.4.8

按 CTRL + C 停止 Let's Chat 服务。

为 Let's Chat 创建 Systemd 服务文件

接下来,你需要为 Let's Chat 创建一个 systemd 服务文件。你可以使用以下命令创建:

nano /etc/systemd/system/letschat.service

添加以下行:

[Unit]
Description=Let's Chat Server
Wants=mongodb.service
After=network.target mongod.service

[Service]
Type=simple
WorkingDirectory=/root/lets-chat
ExecStart=/usr/bin/npm start
User=root
Group=root
Restart=always
RestartSec=9

[Install]
WantedBy=multi-user.target

保存并关闭文件,然后使用以下命令重新加载 systemd 守护进程:

systemctl daemon-reload

接下来,使用以下命令启动并启用 Let's Chat 服务:

systemctl start letschat
systemctl enable letschat

接下来,使用以下命令检查 Let's Chat 的状态:

systemctl status letschat

你应该会看到以下输出:

? letschat.service - Let's Chat Server
     Loaded: loaded (/etc/systemd/system/letschat.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-07-31 04:02:34 UTC; 4s ago
   Main PID: 42429 (npm start)
      Tasks: 23 (limit: 2242)
     Memory: 157.9M
        CPU: 4.457s
     CGroup: /system.slice/letschat.service
             ??42429 "npm start" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             ??42440 sh /tmp/start-b5e830e4.sh
             ??42441 letschat "" "" ""

Jul 31 04:02:34 ubuntu2204 npm[42429]: > node app.js
Jul 31 04:02:38 ubuntu2204 npm[42441]: (node:42441) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead>
Jul 31 04:02:38 ubuntu2204 npm[42441]: (Use `node --trace-deprecation ...` to show where the warning was created)
Jul 31 04:02:38 ubuntu2204 npm[42441]: ???     ?????????????????????????     ??????????  ??? ?????? ?????????
Jul 31 04:02:38 ubuntu2204 npm[42441]: ???     ?????????????????????????    ???????????  ????????????????????
Jul 31 04:02:38 ubuntu2204 npm[42441]: ???     ??????     ???   ????????    ???     ????????????????   ???
Jul 31 04:02:38 ubuntu2204 npm[42441]: ???     ??????     ???   ????????    ???     ????????????????   ???
Jul 31 04:02:38 ubuntu2204 npm[42441]: ????????????????   ???   ????????    ???????????  ??????  ???   ???
Jul 31 04:02:38 ubuntu2204 npm[42441]: ????????????????   ???   ????????     ??????????  ??????  ???   ???
Jul 31 04:02:38 ubuntu2204 npm[42441]: Release 0.4.8

此时,Let's Chat 服务已启动并监听端口 5000。你可以使用以下命令检查:

ss -antpl | grep 5000

你应该会看到以下输出:

LISTEN 0      511        127.0.0.1:5000       0.0.0.0:*    users:(("letschat",pid=42441,fd=20))     

为 Let's Chat 配置 Nginx

接下来,你需要安装并配置 Nginx 作为 Let's Chat 的反向代理,这样你就可以通过端口 80 访问 Let's Chat。

首先,使用以下命令安装 Nginx 服务器软件包:

apt install nginx -y

接下来,创建一个 Nginx 虚拟主机配置文件:

nano /etc/nginx/conf.d/letschat.conf

添加以下配置:

server {
server_name letschat.example.com;
listen 80;

access_log /var/log/nginx/lets_chat-access.log;
error_log /var/log/nginx/lets_chat-error.log;

location / {
proxy_set_header   X-Real-IP $remote_addr;
proxy_set_header   Host      $host;
proxy_http_version 1.1;
proxy_set_header   Upgrade $http_upgrade;
proxy_set_header   Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
proxy_pass         http://127.0.0.1:5000;
}

}

保存并关闭文件,然后检查 Nginx 是否存在语法错误:

nginx -t

你应该会看到以下输出:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

最后,重启 Nginx 服务以应用配置更改:

systemctl restart nginx

你也可以使用以下命令检查 Nginx 的状态:

systemctl status nginx

你应该会在以下输出中看到 Nginx 的状态:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-07-31 04:04:23 UTC; 5s ago
       Docs: man:nginx(8)
    Process: 43193 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 43194 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 43195 (nginx)
      Tasks: 2 (limit: 2242)
     Memory: 2.5M
        CPU: 35ms
     CGroup: /system.slice/nginx.service
             ??43195 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ??43196 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Jul 31 04:04:23 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jul 31 04:04:23 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.

访问 Let's Chat Web 界面

现在,打开你的 Web 浏览器,使用 URL http://letschat.example.com 访问 Let's Chat Web 界面。

在 Ubuntu 22.04 系统上完整安装 Let's Chat 团队聊天服务教程

你需要点击“ I need an account ”按钮来创建一个账户。你应该会看到以下页面:

在 Ubuntu 22.04 系统上完整安装 Let's Chat 团队聊天服务教程

填写你的用户名、密码、电子邮件地址,然后点击 Register 按钮。注册成功后,你应该会看到以下界面:

在 Ubuntu 22.04 系统上完整安装 Let's Chat 团队聊天服务教程

点击 OK 按钮。你将被重定向到 Let's Chat 登录界面:

在 Ubuntu 22.04 系统上完整安装 Let's Chat 团队聊天服务教程

填写你的 Let's Chat 用户名和密码,然后点击 Sign In 按钮。你应该会在以下界面中看到 Let's Chat 仪表板:

在 Ubuntu 22.04 系统上完整安装 Let's Chat 团队聊天服务教程

结论

恭喜!你已在 Ubuntu 22.04 上成功安装了以 Nginx 作为反向代理的 Let's Chat 服务器。现在你可以在工作环境中部署 Let's Chat,并开始与其他团队成员交流。如有任何问题,请随时向我提问。