CentOS7 uses RocketChat to build its own instant chat server

Introduction

What is RocketChat?

RocketChat is an open source chat tool (similar to WeChat and QQ), but the difference is that the servers of WeChat and QQ are owned by Tencent, and the data is stored on Tencent's servers. We use RocketChat, which is a server built by ourselves. Chat history is stored on its own server.

RocketChat supports Mac, Windows, Linux, iPhone, Android, web version, that is, fully supports all platforms.

RocketChat client interface:
-w999

Why build your own chat server?

Nowadays, people pay more and more attention to privacy. Sometimes some internal communications within the company, as well as some well-known sensitive topics, are not suitable for discussion using WeChat, QQ and other tools that do not have any privacy at all. RocketChat's server is We built it ourselves, and the data is in our own hands, so it is more secure.

How to build RocketChat server?

RocketChat is written using nodejs and uses mongodb as the database, so we need to build the RocketChat server and install four parts:

  • 1. nodejs (and some nodejs tool libraries)
  • 2. mongodb (database)
  • 3. GraphicsMagick (a picture processing library, RocketChat needs to use it to process pictures)
  • 4、RocketChat

Next, we will install the four tools mentioned above step by step, of course, if you want to read the official documentation directly: Official installation documentation .

install mongodb

Add yum source for mongodb

Since mongodb has not been added to the yum source, if we want to use yum to install mongodb, we need to add its yum source first. In fact, it is /etc/yum.repos.d/to add a file called mongodb-org-4.0.repoin the directory, and fill in the following content to save it:

[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc

If you think the above method is troublesome, you can directly execute the following command in the terminal, and /etc/yum.repos.d/this file will be automatically generated in the directory:

cat << EOF | sudo tee -a /etc/yum.repos.d/mongodb-org-4.0.repo
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
EOF

install mongodb

After adding the yum source, just use the familiar yum installation (but its package name mongodb-orgis not mongodb):

yum -y install mongodb-org

Modify the mongodb configuration file

You need to set the engine to wiredTiger, and set replicationit to replSetName: rs01, and enclose bindIpthe ip in quotation marks.

The entire configuration that has been set is shown below. If you feel troublesome, you can just overwrite my configuration directly:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
  engine: wiredTiger
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: "127.0.0.1"  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


#security:

#operationProfiling:

replication:
  replSetName: rs01

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

start mongodb

Start first:

systemctl start mongod

Then check if the startup is successful:

systemctl status mongod

If it is as shown in the figure below, if there are active (running)words, it means that it has been successfully started:
-w821

To set the boot to start automatically:

systemctl enable mongod

If you want to stop or restart:

# 停止mongodb
systemctl stop mongod

# 重启mongodb
systemctl restart mongod

Initialize mongodb

mongoLog in to mongodb with the command:

mongo 127.0.0.1

Create a single-server replica set (you can also have multiple servers as a cluster):

rs.initiate({_id:'rs01',members:[{_id:1,host:'127.0.0.1:27017'}]})

The execution result is shown in the following figure:
-w596

After setting, exitexit mongodb with the command.

install nodejs

Execute the following command to install nodejs:

yum -y install nodejs

The version of nodejs installed above is relatively low (you can use the node -vcommand to view the version, I am here at present v6.17.1), but RocketChat needs to use a relatively high version of nodejs, so we need to use the version management tool of nodejs nto switch to the new version (yes, this tool name is just one letter n).

First use npm to install inheritsand n(after installing nodejs earlier, the npmcommand is automatically available):

npm install -g inherits n

The above installation may have some WARNING, you can ignore it (of course it may not).

Then use nthe tool to switch the version of nodejs to the version required by RocketChat 8.11.4:

n 8.11.4

As shown in the figure below, after the switch is successful, you can use node -vthe view version again, which may still be the original version. The reason is that the terminal records the original version. You need to log out, log in to the server again, and then view the version again. It should have been replaced:
-w718

So far, nodejs has been installed.

Install GraphicsMagick

As mentioned earlier, GraphicsMagick is a picture processing tool, RocketChat needs to use it to process pictures in chat, so we have to install it.

This installation is relatively simple, just use yum to install it directly:

yum install -y GraphicsMagick

Install RocketChat

Introduction

All preparations have been completed, and finally we can start to install our protagonist "RocketChat". As mentioned earlier, RocketChat is written in nodejs, so in fact, the so-called installation is to download the source code, unzip it, and put it in a suitable place , to do a systemd self-start at boot, its startup is also very simple, in fact, it is just the entry file to run RocketChat with node main.js, for example:

/usr/local/bin/node /path/to/RocketChat/main.js

Start installing RocketChat

Download the latest version of RocketChat into the /tmp/directory:

curl -L https://releases.rocket.chat/latest/download -o /tmp/rocket.chat.tgz

Entering the /tmp/directory, we can see a rocket.chat.tgzfile called, which is just downloaded:

cd /tmp/

Unzip rocket.chat.tgz, unzip is a bundlefolder:

tar -zxvf rocket.chat.tgz

After the decompression is complete, we enter the serverfolder inside it:

cd bundle/programs/server

After entering the serverfolder, use npm to install the dependencies:

npm install

OK, now RocketChat is actually available, but now it is still in the /tmp/directory, this is the temporary folder of Linux, we can't put it there (of course you can run it if you have to put it there, but I'm afraid in the future Clearing the cache will accidentally delete it, and no one will put the program there), so we're going to move to a place we see fit.

Where should I put it? In fact, under normal circumstances, the programs installed by yourself in Linux will be placed in /usr/local/the directory, but the official documents put it in the /opt/directory, here we will bundlemove the entire folder just unzipped to the /opt/directory, and rename it to Rocket.Chat(of course you can not rename it, but you may forget what program this is in the future).

Execute the following command to bundlemove the entire folder (that is, the Rocket.Chat source program) into a /opt/directory and rename it to Rocket.Chat:

mv /tmp/bundle /opt/Rocket.Chat

Then, by the way, we delete the compressed package downloaded at the beginning, because we have already decompressed it and used it. This package will only occupy the server hard disk space (138M) here:

rm /tmp/rocket.chat.tgz

Set up to start automatically

As mentioned earlier, to run the "server" Rocket.Chat, you can actually use node to run its entry file main.js, that is:

/usr/local/bin/node /path/to/RocketChat/main.js

However, in order to facilitate start/stop, in order to facilitate self-starting at boot, we need to set up self-starting at boot. There are two types of self-starting in Linux, one is the previous init method (CentOS6 and below), and the other is CentOS7. systemd, since we are using CentOS7, we use the systemd way.

Using systemd to manage the start and stop of the program is actually /usr/lib/systemd/system/to add a .serviceconfiguration file ending in the directory, such as RocketChat, we can name rocketchat.serviceit, and then paste the following content directly into it (but be careful to ROOT_URLreplace the ip with you own server ip):

[Unit]
Description=The Rocket.Chat server
After=network.target remote-fs.target nss-lookup.target nginx.target mongod.target
[Service]
ExecStart=/usr/local/bin/node /opt/Rocket.Chat/main.js
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=rocketchat
User=rocketchat
Environment=MONGO_URL=mongodb://127.0.0.1:27017/rocketchat?replicaSet=rs01
Environment=MONGO_OPLOG_URL=mongodb://127.0.0.1:27017/local?replicaSet=rs01
Environment=ROOT_URL=http://你的服务器ip:3000/
Environment=PORT=3000
[Install]
WantedBy=multi-user.target

Since we're going to use a dedicated user to start RocketChat, we need to create a user first, let's call rocketchatit a username, and use the following command to create it:

useradd -M rocketchat && usermod -L rocketchat

After creating the user, don't forget to change the permissions of Rocket.Chat to this user:

chown -R rocketchat:rocketchat /opt/Rocket.Chat

Then we can systemctlstart the RocketChat server with:

systemctl start rocketchat

Then let's check if the startup is successful:

systemctl status rocketchat

If all goes well, you will see output similar to this:
-w684

If there are red words like the picture below failed, it means that the startup failed. Generally speaking, it is a permission problem or a path problem. Just check more:
-w782

After the startup is successful, we add RocketChat to the startup:

systemctl enable rocketchat

RocketChat has been started here, and I will continue to talk about how to use it below. If you want to stop or restart RocketChat, since we have used the systemd startup method, it is a general method of systemctl:

# 停止
systemctl stop rocketchat

# 重启
systemctl restart rocketchat

If you want to have a further understanding of the system/program startup method of Linux, you can take a look at these articles:
Analysis of the Linux initialization init system Part 1 – Sysvinit
analysis of the Linux initialization init system Part 2 – UpStart
analysis of the Linux initialization init system Part 1 Three Parts – Systemd
Systemd Introductory Tutorial: Commands
Systemd Introductory Tutorial: Practical Combat
Linux – Differences and Connections between rc.local/init.d/chkconfig/service/systemctl/rcN.d

Get started with RocketChat

If RocketChat has been started normally, then if you visit in the browser:

http://你的服务器ip:3000

You can enter the first installation wizard interface:
-w1440

  • Name: You don't need to fill in your real name stupidly, you can fill it in casually, or you can fill in your commonly used screen name, English name, etc.;
  • Username: Do not have spaces, it is composed of English underscore numbers, etc. (It doesn't mean that underscores or numbers are required, but that you can use them);
  • Organization Email: Fill in your email
  • Password: fill in the password, this password and the previous user name are actually the administrator, and of course a user (can chat)

Continue down, these you can look at and fill in yourself:
-w1440

continue:
-w1440

continue:
-w1440

You're all set, now you can click "Go to your workspace"
-w974

The interface is like this (this is the background interface):
-w1440

Then, you send your address, that is, http://你的服务器ip:3000/to friends or colleagues in the company. They access this link in the browser or client, register an account, and then they can chat with each other.

Colleagues/friends visit on the browser (register an account first):
-w1398

On the computer client (the same is true on the mobile phone), the first connection may be slow, please wait a moment:
-w993

Use Nginx for reverse generation

In general, we do not recommend using ip to access directly, so it is strongly recommended to use nginx for reverse generation, so that we can access our RocketChat server through the domain name. If you don't know how to install nginx, just find the information on the Internet, here Not to mention.

The following is the nginx reverse generation configuration I use (supports https):

# rocketchat反代
server {
    listen 80;
    listen 443 ssl;
    server_name rocketchat.xxx.com;

    if ($scheme = http){
        return 302 https://$host$request_uri;
    }

    access_log /var/log/nginx/rocketchat.xxx.com_nginx.access.log combined buffer=1k;
    error_log /var/log/nginx/rocketchat.xxx.com_nginx.error.log error;

    #https start
    ssl_certificate /usr/local/nginx/letsencrypt/xiebruce.top/fullchain.pem;
    ssl_certificate_key /usr/local/nginx/letsencrypt/xiebruce.top/private.pem;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHellA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    #https end

    location / {
        proxy_redirect off;
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        #由于RocketChat是使用websocket来传输数据的,所以这里反代的时候要设置支持websocket协议
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

If you don't know how to configure https, you can take a look at configuring free https support for your website and https certificate auto-renewal (renew) two articles.


Of course, you can also use a configuration that doesn't support https if you don't want that trouble:

# rocketchat反代
server {
    listen 80;

    server_name rocketchat.xxx.com;

    access_log /var/log/nginx/rocketchat.xxx.com_nginx.access.log combined buffer=1k;
    error_log /var/log/nginx/rocketchat.xxx.com_nginx.error.log error;

    location / {
        proxy_redirect off;
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        #由于RocketChat是使用websocket来传输数据的,所以这里反代的时候要设置支持websocket协议
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
  • server_nameChange the value of the domain name to your own domain name, and add an A record to your domain name service provider to resolve the domain name to your server;
  • access_logThe path of sum error_logshould be modified to the real path;
  • proxy_passIt means that the request received by nginx is forwarded to RocketChat, so http://127.0.0.1:3000the address and port of RocketChat are behind it. Since RocketChat is local, the address is the same 127.0.0.1, and the port is specified by us /usr/lib/systemd/system/rocketchat.service. Environment=PORT=3000If you do not want to use this port, You can modify it (remember to restart if you modify it).

How to Update RocketChat Servers

Since updating RocketChat is to update its source code, we can repeat the previous installation steps, that is, download → decompress → enter the server folder → npm install→ move and rename it to the /opt/directory.

Client installation

iPhone client

Just search for "rocket.chat" in the AppStore, as shown below:
-w495

Android client

1. GooglePlay download: search for "rocket.chat", as shown below:
-w394

2. Apk installation : Originally, Android can directly download the apk installation package, but since the official does not provide the apk installation package, only the source code , so we can only find a way to install it on googleplay.

Computer client (Mac/Win/Linux)

Method 1: Just go to the official website to download, of course, this website may be a bit slow if there is no scientific Internet access:
image

Method 2: You can go to Github to download.

Web version

No client is required, just access the address directly in the browser.

0 0 vote
Article Rating

This article is simultaneously shared on the blog "Starting Space" (other).
If there is any infringement, please contact [email protected] to delete it.
This article participates in the " OSC Yuanchuang Project ", you are welcome to join and share with us.

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324137860&siteId=291194637