Skip to content

Instantly share code, notes, and snippets.

@centminmod
Created January 16, 2014 16:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save centminmod/8458596 to your computer and use it in GitHub Desktop.
geoip.sh is official Centmin Mod Addon script to install GeoIP database and libraries for Centmin Mod installations on CentOS servers. centminmod.com
#!/bin/bash
######################################################
# written by George Liu (eva2000) vbtechsupport.com
######################################################
# variables
#############
DT=`date +"%d%m%y-%H%M%S"`
CENTMINLOGDIR='/root/centminlogs'
DIR='/svr-setup'
######################################################
# Setup Colours
black='\E[30;40m'
red='\E[31;40m'
green='\E[32;40m'
yellow='\E[33;40m'
blue='\E[34;40m'
magenta='\E[35;40m'
cyan='\E[36;40m'
white='\E[37;40m'
boldblack='\E[1;30;40m'
boldred='\E[1;31;40m'
boldgreen='\E[1;32;40m'
boldyellow='\E[1;33;40m'
boldblue='\E[1;34;40m'
boldmagenta='\E[1;35;40m'
boldcyan='\E[1;36;40m'
boldwhite='\E[1;37;40m'
Reset="tput sgr0" # Reset text attributes to normal
#+ without clearing screen.
cecho () # Coloured-echo.
# Argument $1 = message
# Argument $2 = color
{
message=$1
color=$2
echo -e "$color$message" ; $Reset
return
}
###########################################
# functions
#############
geoipinstall() {
cecho "GeoIP database and library install..." $boldyellow
cd $DIR
# wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
# tar -zxvf GeoIP.tar.gz
cecho "Download GeoIP.tar.gz ..." $boldyellow
if [ -s GeoIP.tar.gz ]; then
cecho "GeoIP.tar.gz found, skipping download..." $boldgreen
else
wget -vnc http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz --tries=3
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
cecho "Error: GeoIP.tar.gz download failed." $boldgreen
exit #$ERROR
else
cecho "Download done." $boldyellow
#echo ""
fi
fi
tar xzf GeoIP.tar.gz
ERROR=$?
if [[ "$ERROR" != '0' ]]; then
cecho "Error: GeoIP.tar.gz extraction failed." $boldgreen
exit #$ERROR
else
cecho "GeoIP.tar.gz valid file." $boldyellow
echo ""
fi
cd GeoIP-1.4.8
./configure
make
make install
echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
ldconfig
ldconfig -v | grep GeoIP
cecho "GeoLiteCity database download ..." $boldyellow
wget -cnv http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz -O /usr/local/share/GeoIP/GeoLiteCity.dat.gz
gzip -d /usr/local/share/GeoIP/GeoLiteCity.dat.gz
#wget -cnv http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz -O /usr/local/share/GeoIP/GeoIP.dat.gz
#gzip -d /usr/local/share/GeoIP/GeoIP.dat.gz
}
######################################################
geoinccheck() {
cecho "geoip.conf include check..." $boldyellow
GEOIPCHECK=$(grep '/usr/local/nginx/conf/geoip.conf' /usr/local/nginx/conf/nginx.conf)
if [[ ! -f '/usr/local/nginx/conf/geoip.conf' ]]; then
cat > "/usr/local/nginx/conf/geoip.conf" <<EOF
# SET the path to the .dat file used for determining the visitors country from the IP-address ###
geoip_country /usr/local/share/GeoIP/GeoIP.dat;
# SET the path to the .dat file used for determining the visitors country from the IP-address ###
geoip_city /usr/local/share/GeoIP/GeoLiteCity.dat;
# Set php-fpm geoip variables
fastcgi_param GEOIP_COUNTRY_CODE \$geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 \$geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME \$geoip_country_name;
fastcgi_param GEOIP_CITY_COUNTRY_CODE \$geoip_city_country_code;
fastcgi_param GEOIP_CITY_COUNTRY_CODE3 \$geoip_city_country_code3;
fastcgi_param GEOIP_CITY_COUNTRY_NAME \$geoip_city_country_name;
fastcgi_param GEOIP_REGION \$geoip_region;
fastcgi_param GEOIP_CITY \$geoip_city;
fastcgi_param GEOIP_POSTAL_CODE \$geoip_postal_code;
fastcgi_param GEOIP_CITY_CONTINENT_CODE \$geoip_city_continent_code;
fastcgi_param GEOIP_LATITUDE \$geoip_latitude;
fastcgi_param GEOIP_LONGITUDE \$geoip_longitude;
EOF
echo
echo "cat /usr/local/nginx/conf/geoip.conf"
cat /usr/local/nginx/conf/geoip.conf
echo
fi
if [[ -z "$GEOIPCHECK" ]]; then
sed -i 's/http {/http { \ninclude \/usr\/local\/nginx\/conf\/geoip.conf;/g' /usr/local/nginx/conf/nginx.conf
fi
}
######################################################
geoipphp() {
cat > "/usr/local/nginx/html/geoip.php" <<END
<html>
<body>
<?php
\$geoip_country_code = getenv(GEOIP_COUNTRY_CODE);
/*
\$geoip_country_code = \$_SERVER['GEOIP_COUNTRY_CODE']; // works as well
*/
\$geoip_country_code3 = getenv(GEOIP_COUNTRY_CODE3);
\$geoip_country_name = getenv(GEOIP_COUNTRY_NAME);
\$geoip_city_country_code = getenv(GEOIP_CITY_COUNTRY_CODE);
\$geoip_city_country_code3 = getenv(GEOIP_CITY_COUNTRY_CODE3);
\$geoip_city_country_name = getenv(GEOIP_CITY_COUNTRY_NAME);
\$geoip_region = getenv(GEOIP_REGION);
\$geoip_city = getenv(GEOIP_CITY);
\$geoip_postal_code = getenv(GEOIP_POSTAL_CODE);
\$geoip_city_continent_code = getenv(GEOIP_CITY_CONTINENT_CODE);
\$geoip_latitude = getenv(GEOIP_LATITUDE);
\$geoip_longitude = getenv(GEOIP_LONGITUDE);
echo 'country_code: '.\$geoip_country_code.'<br>';
echo 'country_code3: '.\$geoip_country_code3.'<br>';
echo 'country_name: '.\$geoip_country_name.'<br>';
echo 'city_country_code: '.\$geoip_city_country_code.'<br>';
echo 'city_country_code3: '.\$geoip_city_country_code3.'<br>';
echo 'city_country_name: '.\$geoip_city_country_name.'<br>';
echo 'region: '.\$geoip_region.'<br>';
echo 'city: '.\$geoip_city.'<br>';
echo 'postal_code: '.\$geoip_postal_code.'<br>';
echo 'city_continent_code: '.\$geoip_city_continent_code.'<br>';
echo 'latitude: '.\$geoip_latitude.'<br>';
echo 'longitude: '.\$geoip_longitude.'<br>';
?>
</body>
</html>
END
cecho "Test geoip.php file located at: " $boldyellow
cecho "/usr/local/nginx/html/geoip.php" $boldyellow
}
##############################################################
starttime=$(date +%s.%N)
{
geoipinstall
geoinccheck
geoipphp
echo
cecho "GeoIP database and library installed..." $boldyellow
} 2>&1 | tee ${CENTMINLOGDIR}/centminmod_geoipdb_install_${DT}.log
endtime=$(date +%s.%N)
INSTALLTIME=$(echo "scale=2;$endtime - $starttime"|bc )
echo "" >> ${CENTMINLOGDIR}/centminmod_geoipdb_install_${DT}.log
echo "Total GeoIP database and libraries Install Time: $INSTALLTIME seconds" >> ${CENTMINLOGDIR}/centminmod_geoipdb_install_${DT}.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment