La combinación ideal: Debian + squid + thundercache + apt-cacher-ng

Imagen de RazaMetaL

Tema: 

En mi red privada tengo varios usuarios con varios sistemas operativos. Las actualizaciones de Ubuntu, windows, antivirus, videos de youtube, y un largo etcetera, cuando son descargados mas de dos veces consumen ancho de banda y es para mi un problema, podemos ahorrar mucho utilizando squid y thundercache para hacer caché de los archivos antes mencionados y adicionalmente apt-cacher-ng para hacer caché de manera transparente de los paquetes deb descargados desde internet.

[b]Descripción:[/b]
- Utilizaremos Debian GNU/Linux como sistema operativo;
- Utilizaremos squid en modo transparente para hacer cache de páginas web;
- Utilizaremos apt-cacher-ng en conjunto con squid para hacer cache de los paquetes .deb que descarguemos con apt-get, aptitude, synaptic, etc;
- Utilizaremos thundercache para hacer caché de videos, actualizaciones antivirus, megaupload, rapidshare, etc;
- Tengo dos interfaces de red, eth0 conectada a Internet y eth1 conectada a mi red interna;

[b]Instalando Squid:[/b]
Nada mas sencillo que escribir como root:

aptitude install squid

Configuramos squid en modo transparente, si tenemos muchos usuarios es recomendable incrementar el parámetro "file descriptors":

sed -i 's/http_port\ 3128/\http_port\ 3128\ transparent/g' /etc/squid/squid.conf
sed -i 's/\#http_access\ allow\ localnet/http_access\ allow\ localnet/g' /etc/squid/squid.conf
sed -i 's/SQUID_MAXFD\=1024/SQUID_MAXFD\=4096/g' /etc/default/squid

Con iptables redirigimos las peticiones al puerto 80 hacia el puerto 3128 en el que escucha squid, estas líneas las podemos agregar en /etc/rc.local para que sean ejecutadas cuando se inicie el servidor:

echo 1 > /proc/sys/net/ipv4/ip_forward
/sbin/iptables -A INPUT -i eth1 -p tcp --dport 3128 -j ACCEPT
/sbin/iptables -t nat -A PREROUTING -i eth1 -p tcp -s 0/0 --dport 80 -j REDIRECT --to-port 3128

[b]Instalando apt-cacher-ng:[/b]

aptitude install apt-cacher-ng

Configuracion de squid para y apt-cacher-ng:
Agregamos en /etc/squid/squid.conf lo siguiente en la sección de acl :

# Configuracion para hacer apt-cacher-ng transparente:
cache_peer localhost parent 3142 7 proxy-only no-query no-netdb-exchange connect-timeout=15
acl aptget browser -i apt-get apt-http apt-cacher apt-proxy
acl deburl urlpath_regex /(Packages|Sources|Release|Translations-.*)\(.(gpg|gz|bz2))?$ /pool/.*/\.deb$ /(Sources|Packages)\.diff/ /dists/[^/]*/[^/]*/(binary-.*|source)/.
cache_peer_access localhost allow aptget
cache_peer_access localhost allow deburl
cache_peer_access localhost deny all
# Fin de configuracion para apt-cacher-ng

[b]Instalando thundercache:[/b]
Instalamos algunas dependencias:

aptitude install apache2 libapache2-mod-php5 php5 php-db php5-gd php5-sqlite sqlite3 libsqlite3-dev libstdc++6 unzip bzip2

Descargamos thundercache con el siguiente comando:

mkdir /usr/src/thundercache
cd /usr/src/thundercache
wget http://www.razametal.org/conf/thundercache/thunder3-32bits.tar.bz2

Lo descomprimimos e instalamos:

tar -jvxf thunder3-32bits.tar.bz2
cp -R sbin/* /usr/local/sbin
chmod a+x /usr/local/sbin/thunder

mkdir /var/log/thunder
mkdir /var/tmp/thunder
mkdir /var/run/thunder
mkdir /var/cache/thunder
ln -s /var/cache/thunder /thunder

chmod a+rwx /var/log/thunder
chmod a+rwx /var/tmp/thunder
chmod a+rwx /var/run/thunder
chmod a+rwx /thunder
chown -R www-data:www-data /thunder/
chmod -R 777 /thunder/
umask 000 /thunder/

cp etc/init.d/* /etc/init.d
chmod a+x /etc/init.d/thunder
cp -R etc/thunder /etc/thunder
touch /etc/thunder/whitelist
touch /etc/thunder/blacklist
ln -s /etc/thunder/thunder.php /var/www/thundercache.php
update-rc.d thunder defaults 98

Modificamos la configuración de squid en el archivo /etc/squid/squid.conf :

sed -i 's/hierarchy\_stoplist\ cgi\-bin/\#hierarchy\_stoplist\ cgi\-bin/g' /etc/squid/squid.conf

Agregamos al final del archivo /etc/squid/squid.conf:

# Thundercache 3
acl thunder_lst url_regex -i "/etc/thunder/thunder.lst"
cache deny thunder_lst
cache_peer 127.0.0.1 parent 8080 0 proxy-only no-digest
dead_peer_timeout 2 seconds
cache_peer_access 127.0.0.1 allow thunder_lst
cache_peer_access 127.0.0.1 deny all
# Fin de modificaciones para Thundercache

Podemos obtener un resúmen de la actividad de thundercache dirigiendo un navegador hacia:

http://ip.del.servidor/thundercache.php

Para eliminar automáticamente los archivos que no han tenido HITS durante las últimas 2 semanas debemos crear este script con el nombre /usr/local/bin/clear_thundercache y luego agregarlo como tarea programada:

#!/bin/sh
data=`date +"%Y-%m-%d" -d "2 week ago"`

dirs=`find /thunder/ -name domain.db`

for d in $dirs
do
lista="init"

dir=`dirname $d`
echo "entered in $dir."

while [ -n "$lista" ]
do
lista=`sqlite3 $dir/domain.db "select file from thunder where last_request < \"$data\" and requested = 0 limit 100;"`
for i in $lista
do
sqlite3 $dir/domain.db "delete from thunder where file=\"$i\""
if rm "$dir/$i" > /dev/null
then
echo "removed $i..."
fi
done
done
done

Lo hacemos ejecutable:

chmod a+x /usr/local/bin/clear_thundercache

Agregamos el script como tarea programada para que se ejecute todos los dias a las 16h00

echo "0 16 * * * root /usr/local/bin/clear_thundercache" >> /etc/crontab

Iniciamos Squid, thundercache, apt-cacher-ng y cron:

/etc/init.d/squid restart
/etc/init.d/thunder restart
/etc/init.d/apt-cacher-ng restart
/etc/init.d/cron restart

Podremos ver la actividad de cada servicio en el log respectivo:

tail -f /var/log/squid/access.log
tail -f /var/log/thunder/access.log
/var/log/apt-cacher-ng/apt-cacher.log

Comentarios

error de libreria sqlite

Buenos días, tengo el siguiente problema con thundercache en centos 6.3 x86_64 con una libreria de sqlite q si lo tengo instalado. El mensaje de error es el siguiente:
[root@mail thunder3-64bits]# /etc/init.d/thunder restart
Shutting down Thunder ...
Starting Thunder ...
/usr/local/sbin/thunder: error while loading shared libraries: libsqlite3.so.0: wrong ELF class: ELFCLASS64

Si alguiente sabe como solucionarlo, que lo publique por favor

Ing. en Electronica y Computacion
Aprendiendo cada dia mas de GNU/Linux

Páginas