CentOS 7 搭建Squid代理服务器

Squid安装

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

[[email protected] ~]# tar xf squid-4.2-20180806-r6d8f397.tar.gz
[[email protected] ~]# mv squid-4.2-20180806-r6d8f397 /opt/squid
[[email protected] ~]# cd /opt/squid
[[email protected] 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

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

[[email protected] squid]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/
[[email protected] squid]# useradd -M -s /sbin/nologin squid
[[email protected] squid]# chown -R squid.squid /usr/local/squid/var/

编辑squid配置文件

[[email protected] squid]# vim /etc/squid.conf   
# 添加 cache_effective_user squid     指定squid的程序用户,用来设置初始化、运行时缓存的账户  
# 添加 cache_effective_group squid  指定账号的基本组     
coredump_dir /usr/local/squid/var/cache/squid

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

[[email protected] squid]# squid -k parse

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

 

[[email protected] squid]# squid -z
[[email protected] squid]# 2018/08/21 17:37:25| Created PID file (/usr/local/squid/var/run/squid.pid)
2018/08/21 17:37:25 kid1| Set Current Directory to /usr/local/squid/var/cache/squid
2018/08/21 17:37:25 kid1| Creating missing swap directories
2018/08/21 17:37:25 kid1| No cache_dir stores are configured.
2018/08/21 17:37:25| Removing PID file (/usr/local/squid/var/run/squid.pid)
[[email protected] squid]# squid
[[email protected] squid]# netstat -antup|grep squid
tcp6       0      0 :::3128                 :::*                    LISTEN      48923/(squid-1)     

 

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

#!/bin/bash
#chkconfig: 111 90 25                                                                        
PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"
case "$1" in
start)
netstat -natp | grep squid &> /dev/null
if [ $? -eq 0  ]
then
echo "squid is running"
else
echo "正在启动 squid..."
$CMD
fi
;;
stop)
$CMD -k kill &> /dev/null
rm -rf $PID &> /dev/null
;;
status)
[ -f $PID  ] &> /dev/null
if [ $? -eq 0  ]
then
netstat -natp | grep squid
else
echo "squid is not running"
fi
;;
restart)
$0 stop &> /dev/null
echo "正在关闭 squid..."
$0 start &> /dev/null
echo "正在启动 squid..."
;;
reload)
$CMD -k reconfigure
;;
check)
$CMD -k parse
;;
*)
echo "用法:$0{start|stop|status|reload|check|restart}"
;;
esac

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

[[email protected] squid]# chmod +x /etc/init.d/squid
[[email protected] squid]# chkconfig --add squid
[[email protected] squid]# chkconfig --level 35 squid on

相关文章

linux下开机自启: 在/etc/init.d目录下新建文件elasticsear...
1、因为在centos7中/etc/rc.d/rc.local的权限被降低了,所以...
最简单的查看方法可以使用ls -ll、ls-lh命令进行查看,当使用...
ASP.NET Core应用程序发布linux在shell中运行是正常的。可一...
设置时区(CentOS 7) 先执行命令timedatectl status|grep &...
vim /etc/sysconfig/network-scripts/ifcfg-eth0 B...