How to Install and Use Webhook on Ubuntu

How to Install and Use Webhook on Ubuntu
Photo by Anatvida Sukchanta / Unsplash

Webhooks are a powerful way to automate tasks by triggering actions on your server. In this blog post, I'll walk you through the process of installing webhook on Ubuntu, setting up a configuration file, and provide an example of how to use the webhook.

Step 1: Installing Webhook

First, let's install the webhook package on your Ubuntu system. Open a terminal and run the following commands:

sudo apt update
sudo apt install webhook

This will update your package list and install the webhook package.

Step 2: Creating the Configuration File

Next, we'll create a configuration file for webhook. This file will define the hooks and their corresponding actions. We'll create a configuration file at /etc/webhook.conf.

sudo nano /etc/webhook.conf

Add the following content to the file:

[
  {
    "id": "example-hook",
    "execute-command": "/usr/local/bin/my-script.sh",
    "command-working-directory": "/usr/local/bin",
    "response-message": "Webhook received and action executed.",
    "trigger-rule": {
      "match": {
        "type": "value",
        "value": "my-api-key",
        "parameter": {
          "source": "query",
          "name": "api_key"
        }
      }
    }
  }
]

If you want a quick and dirty way of making an API key you can just use the uuidgen command.

uuidgen

Breakdown of the Configuration

  • id: A unique identifier for the webhook. This will be used in the URL to call the webhook.
  • execute-command: The command to be executed when the webhook is triggered.
  • command-working-directory: The directory from which the command will be executed.
  • response-message: The message returned in response to the webhook trigger. Usually you will not see this.
  • trigger-rule: Defines a rule to trigger the webhook. In this example, it checks for a specific API key in the URL parameters.

Step 3: Creating the Script

Create the script that will be executed when the webhook is triggered. For this example, we'll create a simple script at /usr/local/bin/my-script.sh.

sudo nano /usr/local/bin/my-script.sh

Add the following content to the script:

#!/bin/bash
echo "Webhook triggered successfully!" >> /var/log/webhook.log

Make the script executable:

sudo chmod +x /usr/local/bin/my-script.sh

Step 4: Configuring the firewall

Figure out your source IP address. This can be a Private IP if you're on a shared LAN with the server or it can be a public address if you're working with a VPS. You will need that for the firewall rule.

ufw allow from any to any port 80 proto tcp
ufw allow from any to any port 443 proto tcp
ufw limit from SOURCE_IP to any port 22 proto tcp

limit allows up to six new connections every 30 seconds. Prevents brute forcing if you do not already have this in place.

Step 5: nginx reverse proxy with TLS

install nginx and certbot tools

sudo apt install certbot python3-certbot-nginx

Edit the default configuration file to look something like this:

server {

	root /var/www/example.com;

	index index.html index.htm;

	server_name example.com;

	location / {
		try_files $uri $uri/ =404;
	}

	location /hooks/ {
		proxy_pass http://127.0.0.1:9000/hooks/;
	}

}

The biggest part is adding the location /hoooks/ block to the configuration file.

Now have certbot run and give you a certificate so you can use HTTPS. This is assuming you already have the domain pointed to this server.

certbot --nginx -d example.com

It's going to ask for your email address so you can be emailed if the renewal is failing.

Step 6: Starting the Webhook Service

Now, enable the service on boot, start the service, and verify it's running correctly.

systemctl enable webhook
systemctl start webhook
systemctl status webhook

Step 7: Testing the Webhook

To test the webhook, open a browser or use curl to send a request with the API key:

curl -X POST https://example.com/hooks/example-hook?api_key=my-api-key

If everything is set up correctly, you should see the response message "Webhook received and action executed." and the script's output in /var/log/webhook.log.

Conclusion

Congratulations! You've successfully installed and configured webhook on Ubuntu. By following this guide, you can now automate tasks on your server using webhooks. Feel free to customize the script and configuration to fit your specific needs. Webhooks are a versatile tool that can greatly enhance your server's capabilities.

Happy automating!