How to Install Centrifugo on Ubuntu Server

Centrifugo is a real-time messaging server that allows you to broadcast messages to multiple clients at once. In this tutorial, we will guide you through the installation of Centrifugo on Ubuntu Server.

Prerequisites

Before you start the installation process, confirm that the following prerequisites are in place:

Step 1: Install the Dependencies

The first step is to install the dependencies required by Centrifugo:

sudo apt-get update
sudo apt-get install wget build-essential

Step 2: Download and Install Centrifugo

To download the latest version of Centrifugo using the following command:

wget https://github.com/centrifugal/centrifugo/releases/download/v3.1.0/centrifugo-3.1.0-linux-amd64.zip

Once the download is complete, unzip the package to the /usr/local/bin/ directory:

sudo unzip centrifugo-*.zip -d /usr/local/bin/

Next, make the centrifugo binary executable by running the following command:

sudo chmod +x /usr/local/bin/centrifugo

Step 3: Configuration

Create a configuration file for centrifugo using the command:

sudo nano /etc/centrifugo/config.json

Enter the below configuration in the json file:

{
    "secret": "Shhh it's a Secret",
    "api_key": "yourapikey",
    "admin_password": "youradminpassword",
    "log_level": "debug",
    "websocket": {
        "enabled": true,
        "sockjs": false,
        "tls": false,
        "port": 8000,
        "address": "0.0.0.0",
        "api_key": "",
        "admin_password": "",
        "api_insecure": true,
        "compression": true,
        "allow_anonymous": true,
        "allow_pubsub": true
    },
    "engine": {
        "type": "memory"
    },
    "control": {
        "enabled": true,
        "port": 8001,
        "api_key": "",
        "admin_password": "",
        "api_insecure": true
    }
}

Once done, save and close the file.

Step 4: Systemd Unit File

Execute the below command to create the systemd unit file for Centrifugo:

sudo nano /etc/systemd/system/centrifugo.service

Enter the below configuration in the file:

[Unit]
Description=Centrifugo Service
Requires=network.target

[Service]
User=root
Group=root
WorkingDirectory=/etc/centrifugo
ExecStart=/usr/local/bin/centrifugo -c /etc/centrifugo/config.json
Restart=on-abnormal

[Install]
WantedBy=multi-user.target

Save and close the file.

Step 5: Start and Enable Centrifugo Service

Once the unit file is created, reload the systemd daemon:

sudo systemctl daemon-reload

Start and enable the Centrifugo service using the following command:

sudo systemctl start centrifugo
sudo systemctl enable centrifugo

Step 6: Configure Nginx Reverse Proxy

Open the nginx configuration file using the following command:

sudo nano /etc/nginx/sites-available/default

Enter the following configuration in the server section:

location /connection/websocket {
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection "upgrade";
     proxy_pass http://127.0.0.1:8000/connection/websocket;
}

Save and close the file.

Then, restart the Nginx web server:

sudo systemctl restart nginx

Step 7: Accessing Centrifugo Dashboard

You can access the Centrifugo web dashboard by visiting your server's IP address on your web browser:

http://your_server_ip:8000/

You can log in with the admin panel by providing the URL with /admin endpoint:

http://your_server_ip:8000/admin

Enter the admin password that you configured earlier.

Congratulations! You have successfully installed and configured the Centrifugo real-time messaging server on Ubuntu Server.

If you want to self-host in an easy, hands free way, need an external IP address, or simply want your data in your own hands, give IPv6.rs a try!