CentOS 编译安装 Squid 3.1

Squid代理服务器是一个缓存Internet数据的软件,可以代理HTTP、FTP、GOPHER、SSL和WAIS等协议,提高用户下载页面的速度,并设置过滤。使用Squid可以通过访问控制特性来灵活的控制用户访问时间、站点等限制。这些可以通过Squid ACL和访问列表来轻松实现。

安装约定

squid源码路径:/usr/local/src
squid安装路径:/usr/local/squid
squid配置文件路径:/usr/local/squid/etc/squid.conf

创建squid用户和组

# groupadd -g 23 squid
# useradd -u 23 -g squid -c "Squid Cache" -d /var/spool/squid -s /sbin/nologin squid

下载源代码

http://www.squid-cache.org/Versions/

# cd /usr/local/src/
# wget http://www.squid-cache.org/Versions/v3/3.1/squid-3.1.23.tar.gz

安装gcc、make等

# yum -y install gcc gcc-c++ make autoconf automake

安装编译squid所需的库

# yum -y install cppunit cppunit-devel libcap-devel openssl-devel

安装squid

# tar zxvf squid-3.1.23.tar.gz
# cd squid-3.1.23

下面进行编译

# ./configure --prefix=/usr/local/squid \
--disable-strict-error-checking \
--disable-dependency-tracking \
--disable-ident-lookups \
--disable-wccp \
--disable-wccpv2 \
--enable-arp-acl \
--enable-follow-x-forwarded-for \
--enable-cache-digests \
--enable-cachemgr-hostname=localhost \
--enable-delay-pools \
--enable-epoll \
--enable-linux-netfilter \
--enable-referer-log \
--enable-removal-policies=heap,lru \
--enable-snmp \
--enable-ssl \
--enable-storeio=aufs,diskd,ufs \
--enable-useragent-log \
--enable-esi \
--enable-icmp \
--with-aio \
--with-default-user=squid \
--with-filedescriptors=65536 \
--with-dl \
--with-openssl \
--with-pthreads \
--with-large-files
# make
# make install

配置squid启动脚本

将如下代码复制粘贴到/etc/init.d/squid

#!/bin/bash
# chkconfig: - 90 25
# pidfile: /usr/local/squid/var/run/squid.pid
# config: /usr/local/squid/etc/squid.conf
#
### BEGIN INIT INFO
# Provides: squid
# Short-Description: starting and stopping Squid Internet Object Cache
# Description: Squid - Internet Object Cache. Internet object caching is \
#       a way to store requested Internet objects (i.e., data available \
#       via the HTTP, FTP, and gopher protocols) on a system closer to the \
#       requesting site than to the source. Web browsers can then use the \
#       local Squid cache as a proxy HTTP server, reducing access time as \
#       well as bandwidth consumption.
### END INIT INFO

ulimit -HSn 65536

PATH=/usr/bin:/sbin:/bin:/usr/sbin
export PATH

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

if [ -f /etc/sysconfig/squid ]; then
	. /etc/sysconfig/squid
fi

# don't raise an error if the config file is incomplete
# set defaults instead:
SQUID_OPTS=${SQUID_OPTS:-""}
SQUID_PIDFILE_TIMEOUT=${SQUID_PIDFILE_TIMEOUT:-20}
SQUID_SHUTDOWN_TIMEOUT=${SQUID_SHUTDOWN_TIMEOUT:-100}
SQUID_CONF=${SQUID_CONF:-"/usr/local/squid/etc/squid.conf"}

# determine the name of the squid binary
[ -f /usr/local/squid/sbin/squid ] && SQUID=/usr/local/squid/sbin/squid && SQUID_id=squid

prog="$SQUID"

# determine which one is the cache_swap directory
CACHE_SWAP=`sed -e 's/#.*//g' $SQUID_CONF | \
	grep cache_dir | awk '{ print $3 }'`

RETVAL=0

probe() {
	# Check that networking is up.
	[ ${NETWORKING} = "no" ] && exit 1

	[ `id -u` -ne 0 ] && exit 4

	# check if the squid conf file is present
	[ -f $SQUID_CONF ] || exit 6
}

start() {
	probe

	parse=`$SQUID -k parse -f $SQUID_CONF 2>&1`
	RETVAL=$?
	if [ $RETVAL -ne 0 ]; then
		echo -n $"Starting $prog: "
		echo_failure
		echo
		echo "$parse"
		return 1
	fi
	for adir in $CACHE_SWAP; do
		if [ ! -d $adir/00 ]; then
			echo -n "init_cache_dir $adir... "
			$SQUID -z -F -f $SQUID_CONF >> /usr/local/squid/var/logs/squid.out 2>&1
		fi
	done
	echo -n $"Starting $prog: "
	$SQUID $SQUID_OPTS -f $SQUID_CONF >> /usr/local/squid/var/logs/squid.out 2>&1
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		timeout=0;
		while : ; do
			[ ! -f /usr/local/squid/var/run/squid.pid ] || break
			if [ $timeout -ge $SQUID_PIDFILE_TIMEOUT ]; then
				RETVAL=1
				break
			fi
			sleep 1 && echo -n "."
			timeout=$((timeout+1))
		done
	fi
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SQUID_id
	[ $RETVAL -eq 0 ] && echo_success
	[ $RETVAL -ne 0 ] && echo_failure
	echo
	return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	$SQUID -k check -f $SQUID_CONF >> /usr/local/squid/var/logs/squid.out 2>&1
	RETVAL=$?
	if [ $RETVAL -eq 0 ] ; then
		$SQUID -k shutdown -f $SQUID_CONF &
		rm -f /var/lock/subsys/$SQUID_id
		timeout=0
		while : ; do
			[ -f /usr/local/squid/var/run/squid.pid ] || break
			if [ $timeout -ge $SQUID_SHUTDOWN_TIMEOUT ]; then
				echo
				return 1
			fi
			sleep 2 && echo -n "."
			timeout=$((timeout+2))
		done
		echo_success
		echo
	else
		echo_failure
		if [ ! -e /var/lock/subsys/$SQUID_id ]; then
			RETVAL=0
		fi
		echo
	fi
	return $RETVAL
}

reload() {
	$SQUID $SQUID_OPTS -k reconfigure -f $SQUID_CONF
}

restart() {
	stop
	start
}

condrestart() {
	[ -e /var/lock/subsys/$SQUID_id ] && restart || :
}

rhstatus() {
	status $SQUID_id && $SQUID -k check -f $SQUID_CONF
}


case "$1" in
start)
	start
	;;

stop)
	stop
	;;

reload|force-reload)
	reload
	;;

restart)
	restart
	;;

condrestart|try-restart)
	condrestart
	;;

status)
	rhstatus
	;;

probe)
	probe
	;;

*)
	echo $"Usage: $0 {start|stop|status|reload|force-reload|restart|try-restart|probe}"
	exit 2
esac

exit $?

启动squid

# chmod +x /etc/init.d/squid
# service squid start

设置squid开机启动

# chkconfig squid on

设置squid环境变量

# vim /etc/profile

在其文件末尾添加如下变量

export PATH=$PATH:/usr/local/squid/sbin:/usr/local/squid/bin

或者用以下命令添加

# sed -i '/unset -f pathmunge/a\export PATH=$PATH:/usr/local/squid/sbin:/usr/local/squid/bin' /etc/profile

运行如下命令使环境变量生效

# source /etc/profile