A starter template for self-hosting n8n β a powerful workflow automation tool β using Docker and Cloudflare Tunnel. This setup enables you to expose your local n8n instance securely to the internet without complex firewall or port forwarding configurations.
- π° Cost-effective: No monthly hosting fees beyond your own hardware and electricity
- π Secure: Cloudflare Tunnel creates encrypted tunnels, hiding your server behind Cloudflare's network
- π Accessible: Access your workflows remotely from anywhere with a custom domain
- π§ Flexible: Run on any always-on device like a Raspberry Pi, old laptop, or home server
- π€ AI-Ready: Perfect foundation for building AI agent workflows with local AI tools
- An always-on device (e.g., Raspberry Pi, old PC, home server)
- Docker and Docker Compose installed
- A free Cloudflare account
- A domain name managed by Cloudflare (can be purchased cheaply)
- Basic familiarity with command line and Docker
n8n-self-hosted-cloudflare-starter/
βββ docker-compose.yml # Docker configuration for n8n
βββ env.example # Example environment variables
βββ .env # Environment variables (create this)
βββ .gitignore # Git ignore rules
βββ backup.sh # Automated backup script
βββ restore.sh # Restore from backup script
βββ n8n_data/ # n8n data directory (auto-created)
β βββ binaryData/ # Binary data storage
β βββ nodes/ # Custom nodes
βββ backups/ # Backup directory (auto-created)
βββ README.md # This file
# Clone this repository
git clone https://github.com/dalenguyen/n8n-self-hosted-cloudflare-starter.git
cd n8n-self-hosted-cloudflare-starter
# Create environment file
cp env.example .envEdit the .env file with your credentials:
# Domain Configuration
DOMAIN=yourdomain.com
# Authentication
N8N_BASIC_AUTH_USER=admin
N8N_BASIC_AUTH_PASSWORD=your_super_secret_passwordThen follow the Cloudflare Firewall Rules Setup section below to secure your webhook subdomain.
# .env
N8N_BASIC_AUTH_USER=admin
N8N_BASIC_AUTH_PASSWORD=your_super_secret_password
β οΈ Important: Add.envto your.gitignorefile to prevent committing secrets.
π For detailed setup instructions with separate subdomains for UI and webhooks, see CLOUDFLARE_SETUP.md
-
Purchase a domain (if you don't have one) from a registrar that supports Cloudflare DNS
-
Add your domain to Cloudflare:
- Sign up for a free Cloudflare account
- Add your domain to Cloudflare
- Update your domain's nameservers to Cloudflare's nameservers
-
Create a subdomain for n8n:
- In Cloudflare dashboard, go to DNS settings
- Add a CNAME record:
n8n.yourdomain.comβyourdomain.com - Or use a subdomain like
workflows.yourdomain.com
- Update your
.envfile with your actual domain:
# Domain Configuration
DOMAIN=n8n.yourdomain.com
# Authentication (use strong passwords!)
N8N_BASIC_AUTH_USER=admin
N8N_BASIC_AUTH_PASSWORD=your_super_secret_password-
Set up Cloudflare Firewall Rules (recommended):
- In Cloudflare dashboard, go to Security β WAF
- Create a firewall rule to restrict access to your n8n instance
- Consider using Cloudflare Access for additional security
-
Optional: Create separate subdomain for webhooks:
- Create
webhooks.yourdomain.comfor webhook endpoints - This separates UI access from webhook access for better security
- Create
π Advanced Security Setup: For comprehensive security configuration with separate UI and webhook subdomains, see the Security Configuration section in CLOUDFLARE_SETUP.md.
docker-compose up -dn8n will be available locally at http://localhost:5678
π For comprehensive setup with automated scripts and separate subdomains, see CLOUDFLARE_SETUP.md
# macOS
brew install cloudflared
# Windows
winget install --id Cloudflare.cloudflared
# Linux (Debian/Ubuntu)
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -o cloudflared.deb
sudo dpkg -i cloudflared.debcloudflared loginThis opens a browser to authorize your Cloudflare account.
# Create tunnel
cloudflared tunnel create n8n-tunnel
# Configure DNS
cloudflared tunnel route dns n8n-tunnel your-subdomain.your-domain.com
# Run tunnel
cloudflared tunnel run n8n-tunnel --url http://localhost:5678Visit https://your-subdomain.your-domain.com to access your n8n workflow editor.
π For detailed security setup with firewall rules and subdomain separation, see CLOUDFLARE_SETUP.md
- Keep basic auth enabled in n8n for an extra security layer
- Use strong passwords for your n8n admin account
- Enable Cloudflare Zero Trust Access policies to restrict who can access your n8n UI
- Separate webhook URLs and UI access with different hostnames for better security
- Regularly update your Docker images and cloudflared
This repository includes a comprehensive backup script (backup.sh) that automatically:
- Creates timestamped compressed backups
- Stores backups in a
./backups/directory - Automatically cleans up old backups (keeps last 7 by default)
- Provides detailed backup information
# Run backup script
./backup.shUse the included restore script (restore.sh) to safely restore from backups:
# Run restore script
./restore.shThe restore script will:
- List available backups
- Safely stop the n8n container
- Backup current data before restoring
- Extract the selected backup
- Restart the n8n container
If you prefer manual backup, you can also use:
# Create manual backup
TIMESTAMP=$(date +"%Y%m%d")
tar -czf n8n_backup_$TIMESTAMP.tar.gz ./n8n_data
# Manual restore
docker-compose down
mv ./n8n_data ./n8n_data_old
tar -xzf n8n_backup_YYYYMMDD.tar.gz
docker-compose up -dTo automate your backups, schedule the script to run periodically using cron:
# Edit crontab
crontab -e
# Add daily backup at 2:00 AM
0 2 * * * /path/to/your/n8n-self-hosted-cloudflare-starter/backup.shThis template is perfect for building AI agent workflows. You can extend it with:
- Self-hosted AI Starter Kit by n8n (bundles with Ollama and Qdrant)
- Local AI tools like Ollama for privacy-conscious AI processing
- Vector databases for document processing and retrieval
- Custom AI workflows for scheduling, summarization, and chatbots
π§ For comprehensive troubleshooting guide including security issues, credentials problems, and domain configuration, see CLOUDFLARE_SETUP.md
- Port already in use: Change the port in
docker-compose.yml - Permission denied: Ensure Docker has proper permissions
- Tunnel connection failed: Check cloudflared authentication and DNS configuration
- Data persistence issues: Verify volume mounting in
docker-compose.yml
# View logs
docker-compose logs -f n8n
# Restart services
docker-compose restart
# Update n8n image
docker-compose pull && docker-compose up -d
# Check tunnel status
cloudflared tunnel listFeel free to submit issues and enhancement requests!
This project is open source and available under the MIT License.
Happy workflow automation! π