CentOS 7 搭建Squid代理服务器

时间:2021-10-14 10:21:06

Squid安装

官方地址:http://www.squid-cache.org/

[root@DaMoWang ~]# tar xf squid-4.2--r6d8f397.tar.gz
[root@DaMoWang ~]# mv squid-4.2--r6d8f397 /opt/squid
[root@DaMoWang ~]# cd /opt/squid
[roit@DaMoWang squid]# ./configure --prefix=/usr/local/squid \
--sysconfdir=/etc \
--enable-arp-acl \
--enable-linux-netfilter \
--enable-linux-tproxy \
--enable-async-io=100 \
--enable-err-language="Simplify_Chinese" \
--enable-underscore \
--enable-poll \
--enable-gnuregex && make && make install

安装完成后,创建链接文件、创建用户和组

[root@DaMoWang squid]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/
[root@DaMoWang squid]# useradd -M -s /sbin/nologin squid
[root@DaMoWang squid]# chown -R squid.squid /usr/local/squid/var/

编辑squid配置文件

[root@DaMoWang squid]# vim /etc/squid.conf
# 添加 cache_effective_user squid 指定squid的程序用户,用来设置初始化、运行时缓存的账户
# 添加 cache_effective_group squid 指定账号的基本组
# 在这行上添加上面两条内容 coredump_dir /usr/local/squid/var/cache/squid 
# 把 cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256 的注释去掉

检查配置文件的语法是否正确

[root@DaMoWang squid]# squid -k parse

先初始化缓存目录,调用squid程序来启动服务

[root@DaMoWang squid]# squid -z
[root@DaMoWang squid]# squid
[root@DaMoWang squid]# netstat -antup|grep squid
tcp6 ::: :::* LISTEN /(squid-)

编写squid启动脚本,并使用chkconfig和service工具进行管理

[root@DaMoWang squid]# vim /etc/init.d/squid
#!/bin/bash
#chkconfig:
#config: /etc/squid.conf
#pidfile: /usr/local/squid/var/run/squid.pid
#Description: Squid - Internet Object Cache PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid" case "$1" in
start)
netstat -utpln | grep squid &>/dev/null
if [ $? -eq ]
then
echo "Squid is running"
else
$CMD
fi
;;
stop)
$CMD -k kill &>/dev/null
rm -rf $PID &>/dev/null
;;
status)
[ -f $PID ] &>/dev/null
if [ $? -eq ]
then
netstat -utpln | grep squid
else
echo "Squid is not running"
fi
;;
restart)
$ stop &>/dev/null
echo "正在关闭Squid..."
$ start &>/dev/null
echo "正在启动Squid..."
;;
reload)
$CMD -k reconfigure
;;
check)
$CMD -k parse
;;
*)
echo "用法:{ start | stop | restart | reload | check | status}"
;;
esac

赋予权限 , 添加到系统服务

[root@DaMoWang squid]# chmod +x /etc/init.d/squid
[root@DaMoWang squid]# chkconfig --add squid
[root@DaMoWang squid]# chkconfig --level squid on