How to Install Centrifugo Messaging Server on Ubuntu 20.04
Centrifugo is a free, open-source, and real-time messaging server. It supports WebSocket and SockJS to make a connection from the application clients through the web browser. It is fast and able to handle millions of simultaneous connections. It can easily integrate your existing application. It is language-agnostic and its API can be used from any programming language.
In this guide, we will show you how to install Centrifugo on Ubuntu 20.04 server.
Prerequisites
- A server running Ubuntu 20.04.
- A root password is configured on the server.
Getting Started
Before starting, you will need to update your system packages to the latest version. You can update them using the following command:
apt-get update -y
Once your server is updated, you can proceed to the next step.
Install Centrifugo
By default, the Centrifugo package is not included in the Ubuntu 20.04 default repository. So you will need to download its binary from the Git repository.
You can download the latest version of Centrifugo with the following command:
wget https://github.com/centrifugal/centrifugo/releases/download/v2.8.4/centrifugo_2.8.4_linux_amd64.tar.gz
Once the download is completed, extract the downloaded file with the following command:
tar -xvzf centrifugo_2.8.4_linux_amd64.tar.gz
Next, move the Centrifugo binary to the /usr/bin directory:
mv centrifugo /usr/bin
Now, verify the Centrifugo version using the following command:
centrifugo version
You should see the following output:
Centrifugo v2.8.4 (Go version: go1.16.3)
Configure Centrifugo
Next, you will need to generate the Centrifugo configuration file. You can generate it with the following command:
centrifugo genconfig
This command will generate a config.json file in your current working directory. You can check it with the following command:
cat config.json
You should get the following output:
{ "v3_use_offset": true, "token_hmac_secret_key": "03599f21-9737-40bc-821e-afe34c1849ec", "admin_password": "51d88406-da91-4aab-979a-c2114b51a14c", "admin_secret": "fa12779d-ba9a-48b9-9cff-14bf96ee79b7", "api_key": "e667e485-1b46-4a5e-991e-a69729b57d07", "allowed_origins": [] }
Note: Note down the admin_password from the above output. You will need this to log in to the web interface.
Next, create a directory for Centrifugo with the following command:
mkdir /etc/centrifugo
Next, copy the config.json file to the /etc/centrifugo directory:
mv config.json /etc/centrifugo/config.json
Once you are finished, you can proceed to the next step.
Create a Systemd Service File for Centrifugo
Next, you will need to create a systemd service file to manage the Centrifugo service. You can create it with the following command:
nano /etc/systemd/system/centrifugo.service
Add the following lines:
[Unit] Description=Centrifugo Websocket Server After=network.target syslog.target [Service] LimitNOFILE=30000 ExecStartPre=/usr/bin/centrifugo checkconfig --config /etc/centrifugo/config.json ExecStart=/usr/bin/centrifugo --config /etc/centrifugo/config.json --admin ExecReload=/bin/kill -HUP $MAINPID ExecStop=/bin/kill -SIGTERM $MAINPID TimeoutStopSec=5 KillMode=control-group RestartSec=2 Restart=always SyslogIdentifier=centrifugo [Install] WantedBy=multi-user.target Alias=centrifugo.service
Save and close the file then reload the systemd daemon to apply the changes:
systemctl daemon-reload
Next, start the Centrifugo service and enable it to start at system reboot:
systemctl start centrifugo
systemctl enable centrifugo
You can check the status of the Centrifugo service with the following command:
systemctl status centrifugo
You should see the following output:
? centrifugo.service - Centrifugo Websocket Server Loaded: loaded (/etc/systemd/system/centrifugo.service; disabled; vendor preset: enabled) Active: active (running) since Sat 2021-05-22 10:01:52 UTC; 4s ago Process: 3024 ExecStartPre=/usr/bin/centrifugo checkconfig --config /etc/centrifugo/config.json (code=exited, status=0/SUCCESS) Main PID: 3040 (centrifugo) Tasks: 6 (limit: 2353) Memory: 7.8M CGroup: /system.slice/centrifugo.service ??3040 /usr/bin/centrifugo --config /etc/centrifugo/config.json --admin May 22 10:01:51 ubuntu2004 systemd[1]: Starting Centrifugo Websocket Server... May 22 10:01:52 ubuntu2004 systemd[1]: Started Centrifugo Websocket Server. May 22 10:01:52 ubuntu2004 centrifugo[3040]: {"level":"info","version":"2.8.4","runtime":"go1.16.3","pid":3040,"engine":"Memory","gomaxprocs":> May 22 10:01:52 ubuntu2004 centrifugo[3040]: {"level":"info","path":"/etc/centrifugo/config.json","time":"2021-05-22T10:01:52Z","message":"usi> May 22 10:01:52 ubuntu2004 centrifugo[3040]: {"level":"info","time":"2021-05-22T10:01:52Z","message":"serving websocket, SockJS, API, admin en>
At this point, Centrifugo service is started and listening on port 8000. You can check it with the following command:
ss -tunelp | grep 8000
You should get the following output:
tcp LISTEN 0 4096 *:8000 *:* users:(("centrifugo",pid=3040,fd=3)) ino:26878 sk:c v6only:0 <->
Access Centrifugo Web UI
Now, open your web browser and access the Centrifugo web interface using the URL http://your-server-ip:8000. You will be redirected to the Centrifugo login page:
Provide your admin password and click on the LOG IN button. You should see the Centrifugo dashboard on the following page:
Conclusion
Congratulations! you have successfully installed the Centrifugo server on Ubuntu 20.04 server. For more information, you should read the Centrifugo documentation.