Topics

memcached

Install Sphinx 2.0.4 on CentOS 6.2

– Make sure to check CentOS, PHP-FPM, Nginx, memcached and MYSQL posts.

– Get fresh RPM from sphinxsearch.com

– Install Sphinx

rpm -Uhv http://sphinxsearch.com/files/sphinx-2.0.4-1.rhel6.x86_64.rpm

– Create sample MySQL database

Watch log files on Linux server with Swatch 3.2.3

– Make sure to check CentOS, PHP-FPM, memcached and MYSQL posts.

– get a fresh package from Swatch project

wget "http://downloads.sourceforge.net/project/swatch/swatch/3.2.3/swatch-3.2.3.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fswatch%2F&ts=1330806322&use_mirror=garr"
tar xfz swatch-3.2.3.tar.gz 
cd swatch-3.2.3
cpan -i Date::Format File::Tail
cpan -i Date::Manip Date::Calc
cpan -i Test::Inter Test::Pod Test::Pod::Coverage Module::Build
cp /root/.cpan/sources/authors/id/S/SB/SBECK/Date-Manip-* .
tar xfz Date-Manip-*.tar.gz
cd Date-Manip-*
perl Build.PL 
./Build install
cd ..
perl Makefile.PL
make install

– configure swatch

install memcached 1.4.13 on CentOS 6.2

– Make sure to check PHP-FPM and MYSQL posts.

– One of the dependencies of memcache is libevent, get latest version from libevent.org

mkdir libevent
cd libevent
wget https://github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz
tar xfz libevent-2.0.17-stable.tar.gz
cd libevent-2.0.17-stable

– get latest version from memcached.org

Installing CentOS 6.2 on VMware

This is a step by step tuturial for installing CentOS 6.2 64bit on VMware 7.0.1 build-227600 running on Windows 7

– downoad the iso DVD from CentOS.org ( need only CentOS-6.2-x86_64-bin-DVD1.iso for this installation )

http://isoredirect.centos.org/centos/6/isos/x86_64/

– Open VMware and follow the slide

Ban IP from logging in for 5 minutes after 10 failed logins


if( login_limit() )
   die( 'Your IP has been banned from logging in for the next 5 minutes' );



/*
 * Counts login times by same IP 
 * returns true if limit reached or false if not 
 */
function login_limit(){
    //get real IP if user behind proxy noted by Sebastian Enger
    $ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
    
    //user still banned from login
    if( _cache( $ip . 'banned' ) )
        return true;

    //number of seconds 
    $sec = 30;
    //find ip info array in cache saved less than $sec ago
    if( ($ip_info=_cache( $ip ))  && $ip_info[0] > time()-$sec ){
        
        //user login 10 times during last $sec 
        if(  $ip_info[1] > 10 ){
            //ban user ip for the next 5 minutes
            _cache( $ip . 'banned', 1, 0, 60*5 );
            _cache( $ip, -1 );
            return true;
        }
        //increase login retries +1 
        _cache( $ip, array(  $ip_info[0],   ++$ip_info[1] ), 0, $sec );
        return false;
        
    }
        
    //add ip info to cache
    _cache( $ip, array( time(), 1 ), 0, $sec );
    return false;
}

function _cache( $name, $val=NULL, $ttl=false ){
        //memcached
	global $mcdb;
	if(empty($mcdb) ) 
		$mcdb = memcache_connect('unix:///etc/sockets/memcached.sock', 0);
	
	if($val === -1){
		return memcache_delete($mcdb,$name);
	}elseif( $val !== NULL ){
		return memcache_set($mcdb,$name,$val, false, $ttl);
	}else{
		$retval = memcache_get($mcdb,$name);
		return  $retval ? $retval : NULL;
	}

}