当前位置:Linux教程 - Linux文化 - Squid 安装配置指南

Squid 安装配置指南


Squid 安装配置指南

红色的部分是手工输入的命令,绿色的是文件的配置.

1.安装linux系统,选择自定义安装,选择开发工具包,webSERVER服务,去掉squid,安装的时候把SELINUX选成DISABLE

你也可以不用做以下的步骤,用我提供的脚本安装一下的一些配置!!!把setup.tar.bz2拷贝到 /root目录下,然后用 tar –jxvf setup.tar.bz2 cd ./setup sh ./setup 这三个命令就可以完成下面的配置了.安装完毕后系统会重新启动,启动后进入系统,配置ipmacfile文件(ip与mac地址绑定文件)和初始化你用户和密码(具体请看服务器使用说明)

2.下载源代码并解压

cd /usr/src wget -c http://www.squid-cache.org/Versi ... 2.5.STABLE14.tar.gz tar -zxvf squid-2.5.STABLE14.tar.gz

3.安装Squid Proxy Server

cd /usr/src/squid-2.5.STABLE14

./configure --prefix=/usr/local/squid --sysconfdir=/etc/squid --bindir=/usr/bin --sbindir=/usr/sbin --mandir=/usr/share/man --enable-gnuregex --enable-carp --enable-async-io=80 --enable-removal-policies=heap,lru --enable-icmp --enable-delay-pools --enable-useragent-log --enable-referer-log --enable-kill-parent-hack --enable-snmp --enable-arp-acl --enable-htcp --enable-cache-digests --enable-default-err-language=Simplify_Chinese --enable-err-languages="Simplify_Chinese" --enable-poll --enable-linux-netfilter --disable-ident-lookups --enable-underscores --enable-auth="basic" --enable-basic-auth-helpers="NCSA" --enable-external-acl-helpers="ip_user" --enable-x-accelerator-vary

make && make install

4.设置squid.conf

cd mv /etc/squid/squid.conf /etc/squid/squid.conf.bak vi /etc/squid/squid.conf #在按下i键 ==============================/etc/squid/squid.conf========================= http_port 100.100.100.1:808 cache_mem 64 MB dns_nameservers 202.98.0.68 202.98.5.68 visible_hostname 100.100.100.1 cache_mgr [email protected] cache_dir aufs /usr/local/squid/var/cache 25000 16 256 error_directory /usr/local/squid/share/errors/Simplify_Chinese emulate_httpd_log on cache_store_log none logfile_rotate 1 hierarchy_stoplist cgi-bin ? hierarchy_stoplist -i ^https:\\ ? acl QUERY urlpath_regex -i cgi-bin \? \.asp \.php \.jsp \.cgi acl denyssl urlpath_regex -i ^https:\\ no_cache deny QUERY no_cache deny denyssl auth_param basic program /usr/local/squid/libexec/ncsa_auth /etc/squid/passwd.sq auth_param basic children 10 auth_param basic realm proxy.linuxserver.com auth_param basic credentialsttl 2 hours auth_param basic casesensitive off refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern . 0 20% 4320 acl all src 0.0.0.0/0.0.0.0 acl OverConnLimit maxconn 5 acl auth_user proxy_auth REQUIRED #acl macaddress arp 00:16:36:05:a6:54 acl manager proto cache_object acl localhost src 127.0.0.1/255.255.255.255 acl to_localhost dst 127.0.0.0/8 acl SSL_ports port 443 563 acl Safe_ports port 80 # http acl Safe_ports port 21 # ftp acl Safe_ports port 443 563 # https, snews acl Safe_ports port 70 # gopher acl Safe_ports port 210 # wais acl Safe_ports port 1025-65535 # unregistered ports acl Safe_ports port 280 # http-mgmt acl Safe_ports port 488 # gss-http acl Safe_ports port 591 # filemaker acl Safe_ports port 777 # multiling http acl CONNECT method CONNECT http_access allow auth_user http_access deny OverConnLimit #http_access allow macaddress http_access allow manager localhost http_access deny manager http_access deny !Safe_ports http_access deny CONNECT !SSL_ports http_access deny all http_reply_access allow all icp_access allow all coredump_dir /usr/local/squid/var/cache ===============================脚本结束================================

5.初始化cache目录和启动squid

cd htpasswd -cb /etc/squid/passwd.sq proxy proxy chmod 666 /etc/squid/passwd.sq mkdir -p /usr/local/squid/var/cache cd /usr/local/squid/var chown -R nobody.nobody cache logs squid -z squid start squid -k parse #检查配置是否有错误,没有任何输出,证明配置基本是正确的!!! squid -zX #可以加一个X参数,看看初始化具体的项目. 如果因为DNS无法启动squid,可以用下面的命令启动squid squid -D

6.安装squid密码修改工具

修改/etc/httpd/conf/httpd.conf的内容,把"AddDefaultCharset" 设置成 GB2312,是WEB显示中文. cd /usr/src vi /var/www/html/index.html

============================/var/www/html/index.html=======================请单击这里修改您的用户密码 ===================================配置结束============================

wget -c http://jaist.dl.sourceforge.net/ ... passwd-2.2.3.tar.gz wget -c http://sarg.sourceforge.net/htoi.patch tar -zxvf chpasswd-2.2.3.tar.gz cd /usr/src/chpasswd-2.2.3 patch -p0 < ../htoi.patch ./configure --enable-language=Chinese --enable-cgidir=/var/www/cgi-bin make && make install vi /usr/local/etc/chpasswd/chpasswd.conf

=================/usr/local/etc/chpasswd/chpasswd.conf========================= password_file /etc/squid/passwd.sq =================================脚本结束==============================

7.防火墙设置及IP地址与MAC地址绑定

touch /usr /sbin/firewall chmod u+x /usr/sbin/firewall vi /usr/sbin/firewall

===============================/usr/sbin/firewall=========================== #!/bin/sh

UPLINK="eth0" LANLINK="eth1" INTERFACES="lo eth0 eth1"

case "$@" in start) echo -n "Starting firewall..." iptables -F iptables -X iptables -Z iptables -t nat -F iptables -t nat -X iptables -t nat -Z iptables -t mangle -F iptables -t mangle -X iptables -t mangle -Z iptables -P INPUT DROP

iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo -j ACCEPT

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

#enable public access to certain services iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT iptables -A INPUT -p tcp --dport 80 -i ! ${UPLINK} -m state --state NEW -j ACCEPT iptables -A INPUT -p tcp -i ${UPLINK} -j REJECT --reject-with tcp-reset

while read ipaddr macaddr do iptables -A INPUT -s ${ipaddr} -m mac --mac-source ${macaddr} -j ACCEPT done < /etc/ipmacfile

#enable system-log #iptables -A INPUT -j LOG --log-prefix "bad input:" #iptables -A INPUT -p udp -i ${UPLINK} -j REJECT --reject-with icmp-port-unreachable

#explicitly disable ECN if [ -e /proc/sys/net/ipv4/tcp_ecn ] then echo 0 > /proc/sys/net/ipv4/tcp_ecn fi

#disable spoofing on all interfaces for x in ${INTERFACES} do echo 1 > /proc/sys/net/ipv4/conf/${x}/rp_filter done

echo "OK!" exit 0 ;; stop) echo -n "Stopping firewall..." iptables -F iptables -X iptables -Z iptables -t nat -F iptables -t nat -X iptables -t nat -Z iptables -t mangle -F iptables -t mangle -X iptables -t mangle -Z iptables -P INPUT ACCEPT iptables -P OUTPUT ACCEPT iptables -P FORWARD ACCEPT iptables -t nat -P POSTROUTING ACCEPT iptables -t nat -P PREROUTING ACCEPT iptables -t nat -P OUTPUT ACCEPT echo "OK!" exit 0 ;; restart) $0 stop $0 start

;; show) clear echo ">------------------------------------------------------" iptables -nvL echo ">------------------------------------------------------" iptables -t nat -nvL exit 0 ;; *) echo "Usage: $0 {start|stop|restart|show}" exit 1 esac =================================脚本结束============================== 注: 建立IP地址与MAC地址对应关系表 touch /etc/ipmacfile vi /etc/ipmacfile ipmacfile文件格式 100.100.100.100 00:16:36:05:a6:54 100.100.100.101 00:16:36:05:a6:53

8.配置系统日志查看 cd mkdir /var/www/html/logs touch /usr/sbin/sqbak chmod u+x /usr/sbin/sqbak vi /etc/squid/index.html

===============================/etc/squid/index.html======================== 客户端网络访问日志 ======================================================================

cp /etc/squid/index.html /var/www/html/logs/index.html

===============================/usr/sbin/sqbak============================ #!/bin/sh logdir=/usr/local/squid/var/logs/ fl=access.log.0 bakpath=/logbak/ wwwpath=/var/www/html/logs/ sysdate=`date +%F` day=`date +%d` #lanip=`ifconfig eth1 | awk '/inet/{print $2}' | awk -F: '{print $2}' | grep -v ^$` if [ "$day" = "01" ] then rm -f /var/www/html/logs/* cp /etc/squid/index.html ${wwwpath} fi if [ -s "${logdir}""${fl}" ] then cd ${bakpath} mv ${logdir}${fl} ${sysdate}.log cat ${sysdate}.log | awk '{print NR " " substr($4,2) " " $3 " " $1 " " $7 " " $11 "\n"}' | awk '{printf "%-16s %-24s %-16s %-24s %-150s %-s\n",$1,$2,$3,$4,$5,$6}' > ${sysdate}.txt tar -cvf ${sysdate}.tar ${sysdate}.txt bzip2 -f ${sysdate}.tar cp ${sysdate}.txt ${wwwpath} echo "
[$sysdate]" >> ${wwwpath}index.html rm -f ${sysdate}.log ${sysdate}.txt fi ======================================================================

crontab -u root -e ====================================================================== 01 0 * * * /usr/sbin/squid -k rotate 15 0 * * * /usr/sbin/sqbak ======================================================================

9.设置启动项目

vi /etc/rc.d/rc.local

/usr/sbin/squid start /sbin/firewall start /usr/sbin/httpd start 10.批量生成用户名密码脚本

脚本一.

touch /usr/sbin/uuadd chmod u+x /usr/sbin/uuadd vi /usr/sbin/uuadd

=========================/usr/sbin/uuadd================================== #!/bin/sh #uuadd: batch add squid user. #install step. #chmod u+x uuadd #./uuadd userfile if [ $# -lt 1 ] then echo "Usage: Please set user's name file." echo "Exmple: `basename $0` userfile." echo "######userfile######" echo " username1" echo " username2" echo " username3" echo " ." echo " ." echo " ." echo " usernamen" echo "#####################" exit 1 fi userfile=$1 uuadd=/usr/bin/htpasswd while read line do $uuadd -b /etc/squid/passwd.sq $line $line done < $userfile =============================脚本结束================================== 注: userfile文件内容的格式 username1 username2 . . . usernamen 脚本二.

touch /usr/sbin/upadd chmod u+x /usr/sbin/upadd vi /usr/sbin/upadd

===================================/usr/sbin/upadd======================== #!/bin/sh #upadd: batch add squid user. #install step. #chmod u+x upadd #./upadd userfile if [ $# -lt 2 ] then echo "usage: $0 userfile" exit 1 fi userfile=$1 upadd=/usr/bin/htpasswd while read username passwd do $upadd -b /etc/squid/passwd.sq $username $passwd done < $userfile =================================脚本结束============================== 注: userfile文件内容的格式 username1 passwd1 username2 passwd2

请帮用户名设置为小写字母,批量生成的密码与用户名相同

单独更改用户密码: htpasswd -b /etc/squid/passwd.sq 用户名 密码

Squid proxy server 使用FAQ

1.怎样添加新用户?

htpasswd -b /etc/squid/passwd.sq username password

说明:用htpasswd工具生成用户名为username密码为password的用户,保存在用户数据库/etc/squid/passwd.sq里.

2.怎样批量添加新用户?

1) 生成用户名和密码一样,然后用户自己更改

uuadd userfile

说明:用uuadd命令批量生成密码与用户名一样的初始用户,用此命令时请指定userfile文件,userfile文件格式如下,是多行单列的内容,请注意在初始化时,如果用户名里包含大写字母的,初始化时密码被初始成小写. 注: userfile文件内容如下 username1 username2 . . Usernamen

最好是所有用户都用小写字母的用户名!!! 注意: 当下次在想批量添加用户时请指定你新的用户文件,因为如果你在还用以前的userfile文件的话,那么原来你的用户数据将被重新初始化.所以请每次批量添加玩用户时,把此文件删除!!!

2)生成用户自己设的的用户名和密码 upadd userfile

说明:用upadd命令批量生成密码与用户名,用此命令时请指定userfile文件,userfile文件格式如下,是多行单列的内容,请注意在初始化时,如果用户名里包含大写字母的,初始化时密码被初始成小写. 注: userfile文件内容的格式 username1 passwd1 username2 passwd2 3.用户初始化密码的更改和当用户想更换密码怎么办?

IE浏览器里输入http://服务器ip地址 点击"请单击这里修改您的代理服务器密码",看说明修改你的密码!!!

4.如果有用户忘记自己的密码怎么办?

请用第1个问题里方法给用户设置密码.

5.怎样查看系统日志?

请在IE浏览器里输入http://服务器ip/logs 能够查看一个月内的用户上网日志. 如果想查看全部的用户上网日志请用WINSCP连接上服务器,在目录/logbak目录下是以日期命名的日志.请你下载想看的日志到本地机器.然后用WINRAR解压后,用记事本即可查看日志了!

6.如果客户端上不去网怎么办? 1) 可以重新启动服务器 用命令 reboot或者shutdown -r now

2) 可能是硬盘空间不足,系统无法写入日志而导致代理服务器进程无法启动,请把/logbak目录下的日志文件拷贝到别的机器上,再把此目录里的文件删除,然后用reboot命令重新启动系统.

3) 也可能是突然断电,导致系统文件丢失,这中情况一般是不会发生,应为LINUX有自我修复功能.如果真的启动不起来,那么请用一张别的LINUX启动光盘启动,把系统里面的重要数据备份出来.重新安装系统吧!!! 要备份的文件包括:用户数据库文件/etc/squid/passwd.sq 系统日志文件/logbak目录下的所有文件和/usr/local/squid/var/logs/access.log文件 /etc/ipmacfile ip地址与mac地址绑定文件

4)网络不通,请检查网络状况

5)硬件系统出现故障,请检查内存,网卡等

6)ip和MAC的地址绑定问题