GeoLite2 is a free database provided by MaxMind that contains IP geolocation information. It’s used to determine the approximate location of an IP address, including details such as city, country, and latitude/longitude coordinates.

If you’re a developer, you can take advantage of GeoLite2 in your PHP applications in various ways, such as:

  • Customizing Content: It can be used to tailor content to users based on their geographic location.
  • Analytics: You can analyze traffic and understand the geographical distribution of users.
  • Security: It is useful for implementing location-based access controls or to detect and prevent fraud.
  • Localization: You can use it to automatically select the appropriate language or currency.

For PHP applications, integrating GeoLite2 can provide a valuable layer of user context without incurring extra costs. Aditionally, GeoLite2 is compatible with MaxMind’s paid GeoIP2 services, using the same API, which makes it easy to switch if more precise data or higher query volumes are needed.

Installing GeoLite2 on RunCloud

Prerequisites

Before we get started, you will need to create an account on the Maxmind website and configure SSH access into your server.

Step 1: Create License Keys: Once you have created your Maxmind account, log in to your dashboard and create a new License Key. After creating the key, download the resulting configuration file to your computer.

Step 2: Upload the File: After creating the file, you need to log in to your server via SSH to upload your credentials. To create your configuration file on the server, you can use a terminal based text editor such as nano – and execute the following command:

sudo nano /etc/GeoIP.conf

The above command will open a blank file on your screen, into which you need to paste the credentials from the file downloaded in the first step. After adding the credentials, save the file by pressing Ctrl + o and then press Enter. After saving the file, you can press Ctrl + x to exit the text editor.

Step 3: Install the GeoIP Update tool: After setting up the credentials, you need to download the database. Although you can do it manually, it’s recommended to use the updater tool which will periodically update the database.

To install the binary, go to the GitHub releases page and copy the link for the latest release file that ends with amd64.deb. In your SSH terminal, execute the following command to download the latest binary onto your server:

wget <url that you copied> 

After executing the above command, you can see the downloaded file on your server using the ls command. Once your download is complete, you can change the permissions of this file and install it using the following commands:

chmod +x geoipupdate_7.0.1_linux_amd64.deb
dpkg -i geoipupdate_7.0.1_linux_amd64.deb

Note: In the above command, you will need to replace geoipupdate_7.0.1_linux_amd64 with the name of the binary file that you downloaded.

Step 4: Install the Database: After installing the binary, you can execute it using the geoipupdate command. It will automatically read the credentials from the default location and install the database in the /usr/share/GeoIP/ folder.

Step 5: Set Up a Cron Job: To keep the database updated, set up a cron job from the RunCloud dashboard. You can give it any name you like, then choose a custom vendor binary, and paste the following code snippet: /usr/bin/geoipupdate.

You can update it once or twice every week, depending on your Maxmind subscription.

Using GeoLite2 in PHP

After installing the database, you can start using it on your server in different ways. In this section, we will explain how you can access the GeoLite2 City database via a PHP application.

Step 1: You need to either create a web application on your server, or use any existing PHP application. In your SSH terminal, navigate to the root directory of this web application.

Step 2: In this directory, you need to install the necessary PHP requirements via composer using the following commands:

curl -sS https://getcomposer.org/installer | php
php composer.phar require geoip2/geoip2:~2.0

The above commands will install the PHP dependencies in the current directory. You can start using the GeoLite2 database by importing the vendor/autoload.php file in your code.

Step 3: Go to your RunCloud dashboard and navigate to the PHP settings section. In this section update the open_basedir variable by adding :/usr/share/GeoIP to the end of the existing value. This will allow your PHP application to access the GeoLite2 database files.

Step 4: Create a new PHP file in your web application using the RunCloud file explorer, and add the following code to it:

<?php
require_once 'vendor/autoload.php';
use GeoIp2\Database\Reader;

// This creates the Reader object, which should be reused across
// lookups.
$cityDbReader = new Reader('/usr/share/GeoIP/GeoLite2-City.mmdb');

// Replace "city" with the appropriate method for your database, e.g.,
// "country".
$record = $cityDbReader->city('1.2.3.4');

print($record->country->isoCode . "\n"); 
print($record->country->name . "\n"); 

print($record->mostSpecificSubdivision->name . "\n"); 
print($record->mostSpecificSubdivision->isoCode . "\n"); 

print($record->city->name . "\n"); 
print($record->postal->code . "\n"); 
print($record->location->latitude . "\n"); 
print($record->location->longitude . "\n"); 
print($record->traits->network . "\n"); 

This code will print out various details about the IP address 1.2.3.4, such as the country, city, postal code, and geographical coordinates. Remember to replace 1.2.3.4 with the IP address you want to look up.

After creating the file, save it and visit it in your browser. You should see the location information about the IP address that you entered in your code.