|
| 1 | +# Using `caddy add-package` to Install Caddy WAF |
| 2 | + |
| 3 | +This guide demonstrates how to install the Caddy WAF module using Caddy's built-in `caddy add-package` command. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Caddy v2.7 or higher already installed on your system |
| 8 | +- Internet connection (to download the custom binary) |
| 9 | +- Appropriate permissions to replace the Caddy binary |
| 10 | + |
| 11 | +## Quick Installation |
| 12 | + |
| 13 | +To add the Caddy WAF module to your existing Caddy installation: |
| 14 | + |
| 15 | +```bash |
| 16 | +caddy add-package github.com/fabriziosalmi/caddy-waf |
| 17 | +``` |
| 18 | + |
| 19 | +That's it! The command will: |
| 20 | +1. Detect your current Caddy installation and modules |
| 21 | +2. Send a build request to Caddy's remote build service |
| 22 | +3. Download a new binary with the WAF module included |
| 23 | +4. Backup your current Caddy binary |
| 24 | +5. Replace the binary with the new one |
| 25 | + |
| 26 | +## Verification |
| 27 | + |
| 28 | +After installation, verify the module is loaded: |
| 29 | + |
| 30 | +```bash |
| 31 | +caddy list-modules | grep waf |
| 32 | +``` |
| 33 | + |
| 34 | +Expected output: |
| 35 | +``` |
| 36 | +http.handlers.waf |
| 37 | +``` |
| 38 | + |
| 39 | +You can also check the full list of modules: |
| 40 | +```bash |
| 41 | +caddy list-modules --packages |
| 42 | +``` |
| 43 | + |
| 44 | +## Usage Example |
| 45 | + |
| 46 | +Once installed, you can use the WAF module in your Caddyfile: |
| 47 | + |
| 48 | +```caddyfile |
| 49 | +{ |
| 50 | + auto_https off |
| 51 | + admin localhost:2019 |
| 52 | +} |
| 53 | +
|
| 54 | +:8080 { |
| 55 | + log { |
| 56 | + output stdout |
| 57 | + format console |
| 58 | + level INFO |
| 59 | + } |
| 60 | +
|
| 61 | + route { |
| 62 | + waf { |
| 63 | + metrics_endpoint /waf_metrics |
| 64 | + rule_file rules.json |
| 65 | + ip_blacklist_file ip_blacklist.txt |
| 66 | + dns_blacklist_file dns_blacklist.txt |
| 67 | + anomaly_threshold 10 |
| 68 | + |
| 69 | + rate_limit { |
| 70 | + requests 100 |
| 71 | + window 60s |
| 72 | + cleanup_interval 300s |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | + respond "Hello, World!" 200 |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Advanced Options |
| 82 | + |
| 83 | +### Keep Backup |
| 84 | + |
| 85 | +By default, the command deletes the backup after successful replacement. To keep the backup: |
| 86 | + |
| 87 | +```bash |
| 88 | +caddy add-package --keep-backup github.com/fabriziosalmi/caddy-waf |
| 89 | +``` |
| 90 | + |
| 91 | +The backup will be saved as `caddy.backup` in the same directory. |
| 92 | + |
| 93 | +### Version Pinning |
| 94 | + |
| 95 | +To install a specific version of the module: |
| 96 | + |
| 97 | +```bash |
| 98 | +caddy add-package github.com/fabriziosalmi/caddy-waf@vX.Y.Z |
| 99 | +``` |
| 100 | + |
| 101 | +Replace `vX.Y.Z` with the desired version tag (e.g., `v0.1.3`). You can find available versions on the [GitHub releases page](https://github.com/fabriziosalmi/caddy-waf/releases). |
| 102 | + |
| 103 | +## Troubleshooting |
| 104 | + |
| 105 | +### Command Not Found |
| 106 | + |
| 107 | +If the `caddy add-package` command is not available, you may be using an older version of Caddy: |
| 108 | + |
| 109 | +```bash |
| 110 | +caddy version |
| 111 | +``` |
| 112 | + |
| 113 | +You need Caddy v2.7 or higher. To update Caddy, visit [caddyserver.com/download](https://caddyserver.com/download) and follow the instructions for your operating system and architecture. |
| 114 | + |
| 115 | +### Permission Denied |
| 116 | + |
| 117 | +If you get a permission denied error, run the command with sudo: |
| 118 | + |
| 119 | +```bash |
| 120 | +sudo caddy add-package github.com/fabriziosalmi/caddy-waf |
| 121 | +``` |
| 122 | + |
| 123 | +### Build Service Unavailable |
| 124 | + |
| 125 | +If Caddy's remote build service is unavailable, you can build from source instead: |
| 126 | + |
| 127 | +```bash |
| 128 | +# Install xcaddy |
| 129 | +go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest |
| 130 | + |
| 131 | +# Build with the module |
| 132 | +xcaddy build --with github.com/fabriziosalmi/caddy-waf |
| 133 | +``` |
| 134 | + |
| 135 | +### Module Already Exists |
| 136 | + |
| 137 | +If you get an error that the package already exists, the module is already installed. To update: |
| 138 | + |
| 139 | +```bash |
| 140 | +# Remove the package first |
| 141 | +caddy remove-package github.com/fabriziosalmi/caddy-waf |
| 142 | + |
| 143 | +# Then add it again |
| 144 | +caddy add-package github.com/fabriziosalmi/caddy-waf |
| 145 | +``` |
| 146 | + |
| 147 | +## Removing the Module |
| 148 | + |
| 149 | +To remove the Caddy WAF module: |
| 150 | + |
| 151 | +```bash |
| 152 | +caddy remove-package github.com/fabriziosalmi/caddy-waf |
| 153 | +``` |
| 154 | + |
| 155 | +## Comparison with Other Installation Methods |
| 156 | + |
| 157 | +| Method | Pros | Cons | |
| 158 | +|--------|------|------| |
| 159 | +| `caddy add-package` | Simple, no build tools needed, keeps existing modules | Requires internet, depends on remote build service | |
| 160 | +| `xcaddy build` | Full control, works offline (after dependencies cached), good for development | Requires Go and build tools, more complex | |
| 161 | +| Quick script | Automated setup with sample configs | Downloads and builds from source, requires build tools | |
| 162 | + |
| 163 | +## Next Steps |
| 164 | + |
| 165 | +- Read the [Configuration Guide](configuration.md) to customize your WAF rules |
| 166 | +- Learn about [Rate Limiting](ratelimit.md) configuration |
| 167 | +- Explore [GeoIP Blocking](geoblocking.md) features |
| 168 | +- Check out the [Metrics](metrics.md) endpoint for monitoring |
| 169 | + |
| 170 | +## References |
| 171 | + |
| 172 | +- [Caddy Command Line Documentation](https://caddyserver.com/docs/command-line) |
| 173 | +- [Caddy Module System](https://caddyserver.com/docs/extending-caddy) |
| 174 | +- [xcaddy Build Tool](https://github.com/caddyserver/xcaddy) |
0 commit comments