centos

CentOS消息推送服务如何搭建

小樊
51
2025-03-09 15:52:13
栏目: 智能运维

在CentOS上搭建消息推送服务有多种选择,以下提供两种常见的消息推送服务搭建方法:使用 Gotify 以及使用 Rocket.Chat。

使用 Gotify 搭建消息推送服务

  1. 下载并解压 Gotify

    在开源项目的版本里下载最新的安装文件。这里选取的是Version 2.4.0版本。

    wget https://github.com/gotify/server/releases/download/v2.4.0/gotify-linux-amd64.zip
    unzip gotify-linux-amd64.zip
    chmod +x gotify-linux-amd64
    
  2. 运行 Gotify 服务

    运行 Gotify 服务,默认会占用 80 端口。

    ./gotify-linux-amd64
    
  3. 配置 Gotify

    下载 config.yml 文件。

    wget -O config.yml https://raw.githubusercontent.com/gotify/server/master/config.example.yml
    

    配置文件默认可以配置 http 端口,也可以配置 https 端口,一般前面都是有 nginx 服务,所以这里只需要配置 http 端口即可。

    port: 9080
    database:
      dialect: sqlite3
      connection: data/gotify.db
    
  4. 配置 Nginx 代理服务

    编辑 Nginx 配置文件,添加以下内容:

    upstream gotify {
        set the port to the one you are using in gotifyserver 127.0.0.1:9080;
    }
    
    server {
        listen 18080;
        # Here goes your domain / subdomain
        server_name push.example.com;
    
        location / {
            # We set up the reverse proxy
            proxy_pass http://gotify;
            proxy_http_version 1.1;
            # Ensuring it can use websockets
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            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 http;
            proxy_redirect http:// $scheme://;
            # The proxy must preserve the host because gotify verifies the host with the origin
            proxy_set_header Host $http_host;
            # These sets the timeout so that the websocket can stay alive
            proxy_connect_timeout 1m;
            proxy_send_timeout 1m;
            proxy_read_timeout 1m;
        }
    }
    
  5. 重启 Nginx 服务

    sudo systemctl restart nginx
    
  6. 访问网页版进行配置

    配置的服务地址端口是 80,那么访问 https://xx.com:18080 地址进入登录界面,默认的用户名密码是 admin/admin。需要点 APPS——CREATE APPLICATION 新建应用,获得令牌 Token。创建后拿到该应用的 token。

使用 Rocket.Chat 搭建消息推送服务

  1. 准备工作

    • 系统要求:CentOS 7 或更高版本。至少 1GB 的 RAM(建议 2GB 以上)。一个指向服务器 IP 地址的域名(例如 linuxidc.com)。Nginx 已安装并配置 SSL 证书(推荐使用 Let’s Encrypt 免费证书)。
    • 开放端口:确保服务器防火墙开放 3000 端口,以便外部访问 Rocket.Chat 服务器。
  2. 安装必要软件包

    更新系统并安装依赖包:

    sudo yum update
    sudo yum install curl gcc make epel-release GraphicsMagick
    

    安装 Node.js 和 npm:

    curl -sL https://rpm.nodesource.com/setup_12.x | sudo bash
    sudo yum install nodejs npm
    

    安装 MongoDB:

    curl -sL https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/getPackage/mongodb-org-4.0.20.rpm | sudo yum install -y
    sudo systemctl start mongod
    sudo systemctl enable mongod
    
  3. 下载并安装 Rocket.Chat

    下载 Rocket.Chat 最新版本:

    curl -L https://releases.rocket.chat/latest/download | tar xzf - -C /tmp
    cd /tmp/bundle/programs/server && npm install unsafeperm
    sudo mv /tmp/bundle /opt/Rocket.Chat
    
  4. 配置 Rocket.Chat 服务

    创建系统用户和目录权限:

    sudo useradd -M rocketchat
    sudo usermod -aG rocketchat rocketchat
    sudo chown -R rocketchat:rocketchat /opt/Rocket.Chat
    

    创建 systemd 单元文件:

    sudo tee /lib/systemd/system/rocketchat.service <<EOF
    [Unit]
    Description=Rocket.Chat server
    After=network.target remotefs.target nss-lookup.target nginx.service mongod.service
    
    [Service]
    StandardOutput=syslog
    StandardError=syslog
    SyslogIdentifier=rocketchat
    User=rocketchat
    Environment=MONGO_URL=mongodb://localhost:27017/rocketchat?replicaSet=rs0
    MONGO_OPLOG_URL=mongodb://localhost:27017/local?replicaSet=rs0
    ROOT_URL=http://your_domain.com:3000/
    PORT=3000
    ExecStart=/usr/local/bin/node /opt/Rocket.Chat/main.js
    
    [Install]
    WantedBy=multi-user.target
    EOF
    

    启动并设置开机自启动:

    sudo systemctl enable rocketchat
    sudo systemctl start rocketchat
    
  5. 配置 Nginx 反向代理(可选)

    编辑 Nginx 配置文件:

    sudo vi /etc/nginx/conf.d/rocketchat.conf
    

    添加以下内容:

    upstream chat {
        server localhost:3000;
        keepalive 8;
    }
    
    server {
        listen 80;
        server_name your_domain.com;
    
        location / {
            proxy_set_header XRealIP $remote_addr;
            proxy_set_header XForwardedFor $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginXProxy true;
            proxy_pass http://chat;
            proxy_redirect off;
        }
    }
    

    重启 Nginx:

    sudo systemctl restart nginx
    
  6. 访问和配置 Rocket.Chat

    访问服务器:打开浏览器,访问 http://your_domain.com:3000,按照页面提示完成初始配置,包括管理员账户信息等。

以上是在 CentOS 上搭建消息推送服务的两种常见方法,你可以根据自己的需求进行选择。

0
看了该问题的人还看了