Centos 7 编译安装Squid

修订:2024-06-30

关闭防火墙、SELINUX、禁止防火墙开机启动、重启系统

systemctl stop firewalld.service && systemctl disable firewalld.service && sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config && shutdown -r now

安装软件及依赖库

yum install -y perl gcc autoconf automake make sudo wget libxml2-devel libcap-devel libtool-ltdl-devel gcc-c++

下载squid后解压

wget http://www.squid-cache.org/Versions/v5/squid-5.9.tar.gz && tar -zxvf squid-5.9.tar.gz && cd squid-5.9

编译安装Squid

./configure --prefix=/usr/local/squid --includedir=/usr/local/squid/include --datadir=/usr/local/share --bindir=/usr/local/squid/sbin --libexecdir=/usr/local/lib --sysconfdir=/usr/local/squid --localstatedir=/usr/local/squid/var --enable-arp-acl --enable-cache-digests --enable-linux-netfilter --with-filedescriptors=65535 --enable-ssl --enable-icmp --enable-linux-tproxy --enable-async-io=100 --enable-storeio=aufs --enable-err-language="Simplify_Chinese" --enable-poll --enable-gnuregex

安装Squid

make && make install

添加软链接

ln -s /usr/local/squid/sbin/* /usr/local/sbin/

添加新用户

useradd -M -s /sbin/nologin squid

配置权限

chown -R squid:squid /usr/local/squid/var

配置squid

vi /usr/local/squid/squid.conf

修改配置文件 http_access deny all 及http_port 3128 后面新增

http_access allow all
cache_effective_user squid
cache_effective_group squid
forwarded_for delete

测试配置

squid -k parse

初始化

squid -z

启动

squid

后台启动

squid -s

检测是否启动成功

netstat -anpt|grep squid

新建Squid脚本

vi /etc/init.d/squid

开机Squid启动脚本

#!/bin/bash
#chkconfig: 345 92 25
# description: squid is a web cache server
# processname: squid
. /etc/rc.d/init.d/functions
case $1 in
"start") /usr/local/squid/sbin/squid -s
    if [ $? == "0" ];then
        echo "squid start ok"
    else
        echo "please check the log"
    fi
;;
"stop") /usr/local/squid/sbin/squid -k shutdown
    if [ $? == "0" ];then
        echo "squid stop ok"
    else
        echo "please check the log"
    fi
;;
"restart")
    /usr/local/squid/sbin/squid -k shutdown
    if [ $? == "0" ];then
        /usr/local/squid/sbin/squid -s
        if [ $? == "0" ];then
            echo "squid restart ok"
        else
            /usr/local/squid/sbin/squid -s
            if [ $? == "0" ];then
                echo "squid restart ok"
            else
                echo "please check the log"
            fi
        fi
    fi
;;
 *)
    echo "Usage only start|stop|restart"
;;
esac

添加执行权限

chmod a+x /etc/init.d/squid

增加到开机启动项并设置开机启动

chkconfig --add squid && chkconfig --level 345 squid on

启动Squid

/etc/init.d/squid start

修改系统内核参数

vi /etc/sysctl.conf

net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0

使内核参数生效

sysctl -p

测试

curl -x 192.168.100.10:3128 www.baidu.com -I

192.168.100.10请以自己机器为主

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注