{"id":157,"date":"2015-04-27T12:23:28","date_gmt":"2015-04-27T12:23:28","guid":{"rendered":"http:\/\/betakb.veeble.org\/?p=157"},"modified":"2025-04-22T12:05:55","modified_gmt":"2025-04-22T06:35:55","slug":"geoip","status":"publish","type":"post","link":"https:\/\/www.veeble.com\/kb\/geoip\/","title":{"rendered":"Install and Configure GeoIP on Apache\/cPanel Servers"},"content":{"rendered":"<h2 class=\"wp-block-heading\" id=\"what-is-geo-ip\"><span id=\"What_is_Geo_IP.3F\" class=\"mw-headline\">What is Geo IP?<\/span><\/h2>\n\n\n<p>GeoIP is a service that converts IP addresses to their respective location on the Earth. This is done by looking up the IP address in a database maintained by various Internet Service providers. Advertisers often take advantage by creating localized ads based on your location. When a person visits your website, GeoIP can determine which country, region, city, postal code, and area code the visitor is coming from. Furthermore, GeoIP can provide information such as longitude\/latitude, connection speed, ISP, company name, domain name, and whether the IP address is an anonymous proxy or satellite provider.<\/p>\n\n\n\n<p>GeoIP searches a GeoIP database from maxmindfor a given host or IP address and returns information about the country, city and\/or&nbsp;ISP&nbsp;for that IP address, depending on the database version.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"getting-geoip-enabled-on-a-server\"><span id=\"Getting_GeoIP_enabled_on_a_server.\" class=\"mw-headline\">Getting GeoIP enabled on a server.<\/span><\/h2>\n\n\n<p><b>Install GeoIP<\/b><\/p>\n\n\n\n<p>Login to the server via back end and follow the below steps.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cd \/usr\/local\/src\/\nmkdir geoip\ncd geoip\nwget <a class=\"external free\" href=\"http:\/\/www.maxmind.com\/download\/geoip\/api\/c\/GeoIP.tar.gz\" rel=\"nofollow noopener\" target=\"_blank\">http:\/\/www.maxmind.com\/download\/geoip\/api\/c\/GeoIP.tar.gz<\/a>\ntar zxvf GeoIP.tar.gz\ncd GeoIP-1.4.6\n.\/configure\nmake\nmake install\n<\/pre>\n\n\n\n<p>As soon as it is installed, the database file would be installed here:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/usr\/local\/share\/GeoIP\/GeoIP.dat\n<\/pre>\n\n\n\n<p>We would require this path to load it with our mod_geoip.<\/p>\n\n\n\n<p>Now download and install the mod_geoip module with the apxs tool of Apache. Download the latest version from&nbsp;:<\/p>\n\n\n\n<p><a class=\"external free\" href=\"http:\/\/geolite.maxmind.com\/download\/geoip\/api\/mod_geoip2\/\" rel=\"nofollow noopener\" target=\"_blank\">http:\/\/geolite.maxmind.com\/download\/geoip\/api\/mod_geoip2\/<\/a><\/p>\n\n\n\n<p>and install it as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cd \/usr\/local\/src\/geoip\nwget <a class=\"external free\" href=\"http:\/\/geolite.maxmind.com\/download\/geoip\/api\/mod_geoip2\/mod_geoip2_1.2.5.tar.gz\" rel=\"nofollow noopener\" target=\"_blank\">http:\/\/geolite.maxmind.com\/download\/geoip\/api\/mod_geoip2\/mod_geoip2_1.2.5.tar.gz<\/a>\ntar -zxvf mod_geoip2_1.2.5\ncd mod_geoip2_1.2.5\n\/usr\/sbin\/apxs -i -a -L\/usr\/local\/lib -I\/usr\/local\/include -lGeoIP -c mod_geoip.c\n<\/pre>\n\n\n\n<p>This should load the mod_geoip module in the httpd.conf automatically. If not, you may require to add the following line in the httpd.conf file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">LoadModule geoip_module       modules\/mod_geoip.so\n<\/pre>\n\n\n\n<p>Now restart the Apache, so that it starts using the mod_geoip module. To use mod_geoip, you would need to use the GeoIP database. Add the following two lines in your .htaccess (for account level) or httpd.conf (for server-wide) to load the GeoIP database:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">GeoIPEnable On\nGeoIPDBFile \/usr\/local\/share\/GeoIP\/GeoIP.dat\n<\/pre>\n\n\n\n<p>There is one more thing. If you are using cpanel, cpanel will discard this load automatically if you do not save the configuration on the next Apache rebuild. To save the configuration, run the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/usr\/local\/cpanel\/bin\/apache_conf_distiller \u2013update\n<\/pre>\n\n\n\n<p><b>On cpanel servers<\/b>, mod_geoip can also be installed by running the easyapache script.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/scripts\/easyapache\n<\/pre>\n\n\n\n<p><b>On cpanel servers via WHM\/Cpanel,<\/b> you can simply install GeoIP in cpanel servers by these steps<\/p>\n\n\n\n<p>1)Navigating to WHM<\/p>\n\n\n\n<p>2)click easyapache (More details <a class=\"external text\" href=\"http:\/\/docs.cpanel.net\/twiki\/bin\/view\/EasyApache3\/WebHome\" rel=\"nofollow noopener\" target=\"_blank\">here<\/a>)<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"using-geoip\"><span id=\"Using_GeoIP\" class=\"mw-headline\">Using GeoIP<\/span><\/h2>\n\n\n<p><b>1. Redirect using mod_rewrite<\/b><\/p>\n\n\n\n<p>mod_geoip supports two environmental variables: GEOIP_COUNTRY_CODE and GEOIP_COUNTRY_NAME . Code means the two-letter names of a country and the Name means the official name of the country. CODE is easier to use. Here is an example of how to redirect all Chinese traffic to example.com&nbsp;:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RewriteEngine on<br>RewriteCond&nbsp;%{ENV:GEOIP_COUNTRY_CODE} ^CN$<br>RewriteRule ^(.*)$ http:\/\/www.example.com [L]<br><\/pre>\n\n\n\n<p>This is actually a simple mod_rewrite code using one of the environmental variables supplied by mod_geoip. You can also use any mod_rewrite technique to block\/redirect multiple countries at once. For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RewriteEngine on\nRewriteCond&nbsp;%{ENV:GEOIP_COUNTRY_CODE} ^(CN|NE|NG)$\nRewriteRule ^(.*)$ <a class=\"external free\" href=\"http:\/\/www.another.com%241\/\" rel=\"nofollow\" target=\"_blank\">http:\/\/www.another.com$1<\/a> [L]\n<\/pre>\n\n\n\n<p>This will redirect all the traffic from either China or Niger or Nigeria to another.com or let&#8217;s say block the traffic.<\/p>\n\n\n\n<p><b>2. Using SetEnvIf<\/b><\/p>\n\n\n\n<p>You can use the SetEnvIf directive of .htaccess to block traffic and show a 403 Apache error page. Just add a type of the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry\nSetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry\nDeny from env=BlockCountry\n<\/pre>\n\n\n\n<p>This will block all traffic from China and Russia. You can use the NE code for Niger or NG for Nigeria or a similar code for others. If you would like to add more countries to the block list just add a duplicate line with SetEnvIf and include the country name the same as CN or RU.<\/p>\n\n\n\n<p>If the server admin wants to block traffic from a specific country server-wide, you would need to add one of the rule above in the httpd.conf instead of .htaccess. Just add the code before any virtual host entry and it should work fine.<\/p>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-703d2caf wp-block-button\"><div class=\"uagb-cta__wrap\"><h3 class=\"uagb-cta__title\">Affordable cPanel Plans<\/h3><p class=\"uagb-cta__desc\">Get premium cPanel features at a budget-friendly price. Our plans offer excellent value, with all the tools you need to succeed online. Start building your online presence today.<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/www.veeble.com\/in\/cpanel-hosting\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_blank\" rel=\"noopener noreferrer\">Explore cPanel Plans<svg xmlns=\"https:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path d=\"M504.3 273.6l-112.1 104c-6.992 6.484-17.18 8.218-25.94 4.406c-8.758-3.812-14.42-12.45-14.42-21.1L351.9 288H32C14.33 288 .0002 273.7 .0002 255.1S14.33 224 32 224h319.9l0-72c0-9.547 5.66-18.19 14.42-22c8.754-3.809 18.95-2.075 25.94 4.41l112.1 104C514.6 247.9 514.6 264.1 504.3 273.6z\"><\/path><\/svg><\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>What is Geo IP? GeoIP is a service that converts IP addresses to their respective location on the Earth. This is done by looking [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":8656,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-157","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-installation"],"uagb_featured_image_src":{"full":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Install-and-Configure-GeoIP-on-ApachecPanel-Servers.jpg",1536,1024,false],"thumbnail":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Install-and-Configure-GeoIP-on-ApachecPanel-Servers-150x150.jpg",150,150,true],"medium":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Install-and-Configure-GeoIP-on-ApachecPanel-Servers-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Install-and-Configure-GeoIP-on-ApachecPanel-Servers-768x512.jpg",768,512,true],"large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Install-and-Configure-GeoIP-on-ApachecPanel-Servers-1024x683.jpg",1024,683,true],"1536x1536":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Install-and-Configure-GeoIP-on-ApachecPanel-Servers.jpg",1536,1024,false],"2048x2048":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2015\/04\/Install-and-Configure-GeoIP-on-ApachecPanel-Servers.jpg",1536,1024,false]},"uagb_author_info":{"display_name":"Vipin Raj","author_link":"https:\/\/www.veeble.com\/kb\/author\/vipin\/"},"uagb_comment_info":0,"uagb_excerpt":"What is Geo IP? GeoIP is a service that converts IP addresses to their respective location on the Earth. This is done by looking [&hellip;]","_links":{"self":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/comments?post=157"}],"version-history":[{"count":6,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":8657,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/157\/revisions\/8657"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media\/8656"}],"wp:attachment":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media?parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/categories?post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/tags?post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}