How to set up Cloudflare for Magento with the OCM Labs Cloudflare module
Magento Cloudflare setup step by step: install the OCM Labs module, create a scoped Cloudflare API token, connect the zone, start the purge consumer.

The install is five commands. What goes wrong is everything around it: a token missing one permission, a zone never detected, a purge consumer nobody started. Here is the whole Magento Cloudflare setup in order, verified at each step.
OCM Labs Cloudflare is free and open source (OSL-3.0). One package covers Magento Open Source and Adobe Commerce 2.4.4 - 2.4.9, PHP 8.1 - 8.4, Luma and Hyva. By the end, the module is installed, connected, purging through a running consumer and locked down with ACL. Edge caching stays off until post 2.
Step 1: Check the requirements
Before touching Composer, confirm four things:
- Magento Open Source or Adobe Commerce 2.4.4 or later
- PHP 8.1 or later
- A Cloudflare zone proxying the storefront, with the orange cloud on for every storefront hostname. A DNS-only record means Cloudflare never sees the request.
- A working Magento cron. The purge queue consumer and the hourly health checks depend on it.
Verify: the storefront records in the Cloudflare DNS tab show the orange proxy icon, and crontab -l as the Magento user lists the bin/magento cron:run entries.
Step 2: Install the module
composer require ocmlabs/module-cloudflare
bin/magento module:enable OCMLabs_Cloudflare
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento cache:cleanIn production mode also run bin/magento setup:static-content:deploy.
The last line matters more than it looks. Run a full bin/magento cache:clean, not just a flush of the full page cache. The module registers plugins and a RequireJS map, and both stay stale until the config and layout caches are cleaned. Skip it and you get dashboard buttons that do not render and an old plugin that stays alive; the troubleshooting page has that case.
Verify:
bin/magento module:status OCMLabs_CloudflareThen log into the admin. System > Cloudflare > Dashboard should be in the menu. Nothing changes on the storefront yet: the module is disabled by default and edge caching is a separate switch.
Step 3: Create the Cloudflare API token
Create a custom token scoped to the zones the store uses. In Cloudflare go to My Profile > API Tokens > Create Token > Create Custom Token and grant these six permissions:
| ZONE:READ | Look up the zone, its plan and development mode |
|---|---|
| ZONE:CACHE PURGE | Purge by tag, URL, prefix or everything |
| ZONE:SETTINGS:READ | Health checks on zone settings (SSL mode, Rocket Loader and so on) |
| ZONE:SETTINGS:EDIT | Turn development mode on and off |
| ZONE:CACHE RULES:EDIT | Apply the recommended cache rules |
| ANALYTICS:READ | The Analytics tab |
Zone:Cache Rules:Edit is the one people miss. It is separate from Zone:Settings:Edit. Without it everything works except applying cache rules from the module; you can paste the rules by hand instead, but it is less work to get the token right now.
Verify: the token summary lists all six permissions and the zones you expect.
Step 4: Connect the store
Everything lives under Stores > Configuration > OCMLabs > Cloudflare. The connection is global, the zone is per website.
- General (default scope). Set Enable Cloudflare Integration to Yes and paste the token into API Token; it is encrypted at rest. Leave Account ID blank, it is detected from the token. Save, then click Test Connection, which verifies the token and lists the zones it can see.
- Zone (website scope). Switch the scope selector to each website and click Detect Zone. It looks the zone up from the website's base URL hostnames, or paste the Zone ID from the Cloudflare overview page. Each website can map to its own zone, and the token can be overridden per website if one lives on a different Cloudflare account.
- Storefront Hosts is optional. Blank means the hosts come from the website's base URLs. If the public host differs, fill it in, one host per line. It feeds the cache rule expressions and the live health probes.
- Storefront Origin is optional too. Only the "Origin sends edge cache headers" health check uses it, to fetch a page straight from your server instead of through Cloudflare. Accepts a host,
host:port, an IP or a URL, for exampleorigin.example.com:8443.
Verify: Test Connection returns the zone list, and the Zone group at website scope shows a populated Zone ID.
If Test Connection fails:
- Authentication error. The token is wrong, expired, or pasted with whitespace.
- Permission error on a specific action. The token is valid but missing a permission; the error names the call that failed.
Zone:Cache Rules:Editis the usual one. - Works at default scope, fails for one website. A website-scoped API Token override is set. Clear it or fix it.
If Detect Zone finds nothing, the token's zone scope does not include the zone serving the base URL, or that host is not in Cloudflare. Paste the Zone ID by hand and set Storefront Hosts if the public host differs.
Step 5: Start the purge queue consumer
Purges are queued by default and sent by the ocmlabsCloudflarePurge consumer on Magento's built-in db queue connection. No RabbitMQ required, but the consumer has to run. If it does not, tags pile up in the queue and Cloudflare keeps serving the old page after every product save.
Option 1: Magento cron. If app/etc/env.php has no cron_consumers_runner key, Magento's cron already starts every registered consumer, including this one. To scope it explicitly:
'cron_consumers_runner' => [
'cron_run' => true,
'max_messages' => 10000,
'consumers' => ['ocmlabsCloudflarePurge'],
],If env.php already has a consumers list, add ocmlabsCloudflarePurge to it. A list that omits it is a silent way to never purge.
Option 2: process supervisor. Run it as a long-lived process under supervisord or similar:
bin/magento queue:consumers:start ocmlabsCloudflarePurge --max-messages=10000Verify: open System > Cloudflare > Dashboard > Health and read the "Purge queue consumer" check. Rows stuck at queued in the purge log mean the same thing.
Step 6: Grant admin access
The module registers five ACL resources under System > Permissions > User Roles:
| OCMLABS_CLOUDFLARE::DASHBOARD | Open the dashboard (Overview, Analytics and Health tabs) |
|---|---|
| OCMLABS_CLOUDFLARE::PURGE | Purge from the dashboard (quick purge, purge everything) |
| OCMLABS_CLOUDFLARE::SETTINGS_WRITE | Change zone settings from the dashboard (development mode, apply cache rules, apply a health check fix) |
| OCMLABS_CLOUDFLARE::PURGE_LOG | View the purge log |
| OCMLABS_CLOUDFLARE::CONFIG | Change module configuration under Stores > Configuration |
Keep settings_write to ops: it can flip development mode and rewrite the zone's cache rules.
Verify: log in on the restricted role and confirm the dashboard matches what you granted.
Step 7: Script it for CLI-driven deployments
If your pipeline sets configuration with bin/magento config:set or env.php overrides, every field sits under ocmlabs_cloudflare/<group>/<field>:
bin/magento config:set ocmlabs_cloudflare/general/enabled 1
bin/magento config:set --encrypt ocmlabs_cloudflare/general/api_token "<token>"
bin/magento config:set --scope=websites --scope-code=base ocmlabs_cloudflare/zone/zone_id "<zone id>"The token is encrypted at rest, so set it with --encrypt. The full path list, including the edge cache, purging and real IP groups you will need later, is on the settings reference.
What is still off
The module is connected and purging, but the storefront behaves as before. Enable Edge Caching of HTML is No by default, and while it is No every storefront response carries Cache-Control: private, no-store, so even a zone with a "Cache Everything" rule cannot cache HTML by accident. curl -sI https://www.example.com/some-category.html | grep -i cf-cache-status returns DYNAMIC, which is correct for now.
The next three posts turn things on in order. First the cache rules, with a diff before anything is written: how to apply Cloudflare cache rules for Magento. Then edge caching: how to cache Magento HTML at the Cloudflare edge. Then the 25 health checks: how to run Cloudflare health checks for Magento.
If you would rather hand off the install, the zone audit and the rollout, that is our performance and infrastructure work.
✦ FAQ
Do I need RabbitMQ for the purge queue?+–
Why does Test Connection pass but applying cache rules fail?+–
Can one Magento install use two Cloudflare zones?+–
Does installing the module change anything on the storefront?+–
Why does the admin dashboard look broken after install?+–
Where do I look when something fails?+–

Shane Blandford
Founder of Orange Collar Media, a Denver ecommerce agency behind 800+ Magento and Shopify builds. In Magento since version 1.x - building, rescuing, and supporting revenue-critical stores since 2008.
✦ KEEP READING
Related from the Journal.
Why put Cloudflare in front of Magento: what a CDN does for a store, what it does not, and what breaks
What Cloudflare does for a Magento store: a CDN for static files, edge-cached HTML, DDoS and bot protection, a WAF, and the five settings that break checkout.

OCM Labs Cloudflare: free edge caching for Magento, on Magento's own terms
A free, open-source Magento module that caches storefront HTML at the Cloudflare edge using Magento's own full page cache rules, and purges by cache tag.

How to use Cloudflare development mode and analytics from the Magento admin
Turn Cloudflare development mode on before a theme deploy, run a quick purge and read Cloudflare analytics from the Magento admin with a free module.
