Setup ntfy for selfhosted notifications

In today’s world, staying informed about events in our homelabs is essential. ntfy is a straightforward HTTP-based notification service that allows you to send notifications to your phone or desktop from any computer using scripts or a REST API. The best part? You can host it entirely within your own homelab.

In this guide, we’ll show you how to make ntfy publicly accessible through Cloudflare Tunnels, ensuring you receive notifications wherever you are.

Setting up Cloudflare tunnels is out of scope for this article see here if you want to set it up.

Setup ntfy with Docker

To get started with ntfy, you’ll need to create the necessary directories and files for the Docker container.

BASH
1mkdir ntfy
Click to expand and view more

Create a docker-compose.yml file: In the ntfy folder, create a docker-compose.yml file:

BASH
1nano ntfy/docker-compose.yml
Click to expand and view more

Add the following content to the file:

docker-compose.yml
 1services:
 2  ntfy:
 3    image: binwiederhier/ntfy:v2.11.0
 4    container_name: ntfy
 5    restart: unless-stopped
 6    environment:
 7      - TZ=Europe/Amsterdam  # Change this to your timezone
 8      - NTFY_BASE_URL=http://ntfy.yourdomain.com
 9      - NTFY_CACHE_FILE=/var/lib/ntfy/cache.db
10      - NTFY_BEHIND_PROXY=true
11      - NTFY_ATTACHMENT_CACHE_DIR=/var/lib/ntfy/attachments
12      - NTFY_UPSTREAM_BASE_URL=https://ntfy.sh # Optional if you need push notifications on iOS
13    volumes:
14      - ntfy:/var/lib/ntfy
15    command:
16      - serve
17    networks:
18      - cloudflared
19volumes:
20  ntfy:
21    name: ntfy
22networks:
23  cloudflared:
24    name: cloudflared
Click to expand and view more

The cloudflared network should correspond to the Docker network where your Cloudflare tunnel is active. Adjust this part according to your setup.

Run the following command to start the container:

BASH
1docker compose -f ntfy/docker-compose.yml up -d
Click to expand and view more

Cloudflare Tunnel

Here’s a refined version of the instructions for adding ntfy to the Cloudflare Tunnel:

Adding ntfy to Your Cloudflare Tunnel

To make ntfy accessible via the Cloudflare Tunnel, follow these steps:

  1. Navigate to the Tunnels Page: Go to the Networks -> Tunnels section in your Cloudflare dashboard.
  2. Select Your Tunnel: Click on the tunnel to which you want to add ntfy.
  3. Edit Tunnel Settings: Click on Edit to modify the tunnel settings.
  4. Configure Public Hostname:
  1. Fill in the Required Fields:
  1. Access Your ntfy WebGui:

After saving the configuration, you can access the ntfy WebGui at https://ntfy.yourdomain.com.

Test Notifications

  1. Open the ntfy WebGui: Navigate to https://ntfy.yourdomain.com.
  2. Subscribe to a Topic:
  1. Send a Test Notification: Open the command line on your server and run the following command:

Open the command line on your server and run the following command:

BASH
1curl -d "Hi" https://ntfy.yourdomain.com/yourtopic
Click to expand and view more

Replace yourtopic with the name of the topic you just created in the WebGui.

After sending the command, you should see the message “Hi” appear in the WebGui under the subscribed topic.

Secure it

To enhance the security of your ntfy setup by requiring authentication, you can update your docker-compose.yml file as follows:

Open the docker-compose.yml file:

BASH
1nano ntfy/docker-compose.yml
Click to expand and view more

Add the following environment variables

docker-compose.yml
1environment:
2  - NTFY_AUTH_FILE=/var/lib/ntfy/auth.db
3  - NTFY_AUTH_DEFAULT_ACCESS=deny-all
4  - NTFY_ENABLE_LOGIN=false
Click to expand and view more

Run the following command to restart the container and apply the changes:

BASH
1docker compose -f ntfy/docker-compose.yml up -d --force-recreate
Click to expand and view more

Users

To manage users and their permissions in your ntfy setup, follow these steps to create users and assign them specific access rights:

Login to the ntfy Docker container

BASH
1docker exec -it ntfy sh
Click to expand and view more

Create admin user

To create an admin user who can read and write to all topics, use the following command:

BASH
1ntfy user add --role=admin <username>
Click to expand and view more

Congrats, you have now your know service for sending push notifications for all your applications.

Copyright Notice

Author: Sven van Ginkel

Link: https://svenvg.com/posts/setup-ntfy-for-selfhosted-notifications/

License: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut