Show real ip in cpanel Apache log when using cloudflare

Cloudflare’s own Apache mod_cloudflare is now discontinued, we will install Apache’s own mod_remoteip in cPanel.

1. Install mod_remoteip in EA4 (uninstall mod_cloudflare if exists)

2. Add following list of trusted Cloudflare proxy IPs to a file /etc/apache2/conf.modules.d/370_mod_remoteip.conf

RemoteIPHeader X-Real-IP
RemoteIPTrustedProxy 173.245.48.0/20
RemoteIPTrustedProxy 103.21.244.0/22
RemoteIPTrustedProxy 103.22.200.0/22
RemoteIPTrustedProxy 103.31.4.0/22
RemoteIPTrustedProxy 141.101.64.0/18
RemoteIPTrustedProxy 108.162.192.0/18
RemoteIPTrustedProxy 190.93.240.0/20
RemoteIPTrustedProxy 188.114.96.0/20
RemoteIPTrustedProxy 197.234.240.0/22
RemoteIPTrustedProxy 198.41.128.0/17
RemoteIPTrustedProxy 162.158.0.0/15
RemoteIPTrustedProxy 104.16.0.0/12
RemoteIPTrustedProxy 172.64.0.0/13
RemoteIPTrustedProxy 131.0.72.0/22
RemoteIPTrustedProxy 2400:cb00::/32
RemoteIPTrustedProxy 2606:4700::/32
RemoteIPTrustedProxy 2803:f800::/32
RemoteIPTrustedProxy 2405:b500::/32
RemoteIPTrustedProxy 2405:8100::/32
RemoteIPTrustedProxy 2a06:98c0::/29
RemoteIPTrustedProxy 2c0f:f248::/32

3. Now go to WHM and under “Apache Configuration”, click on “Include Editor”, then under “Pre Main Include” edit the file and add:

<IfModule remoteip_module>
RemoteIPHeader X-Forwarded-For
</IfModule>

4. Then, go to WHM and under “Apache Configuration”, click on “Global Configuration”, change the value %h to %a in LogFormat (combined/common) like follows:

%a %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”

%a %l %u %t \”%r\” %>s %b

5. Rebuild and Restart Apache