日期:2014-05-16  浏览次数:20719 次

如何在linux上用squid搭建代理服务器_足够详细


在linux上用squid搭建代理服务器,这个东东全世界都是,但是有一些细节问题在网上说的不明不白的,折腾了半天搞好了,如鲠在喉不吐不快。


一台电脑linux系统,上不了网,于是在另外一台可以上网的linux上面搭建一个代理服务器,步骤如下:


下载:squid-3.2.9.tar.bz2
网上随便都能搜到,为了让我赚一分钱,也可以点击大笑
http://download.csdn.net/detail/testingba/6389351




上传到服务器后解压:
解压: tar -vxjf squid-3.2.9.tar.bz2
解压后生成目录:squid-3.2.9




进入目录翻看文档INSTALL:
xxxx> more INSTALL 
To build and install the Squid Cache, type:


        % ./configure --prefix=/usr/local/squid
        % make all
        % make install


To run a Cache, you will need to:


        1. customize the squid.conf configuration file:
              % vi /usr/local/squid/etc/squid.conf


        2. Initalise the cache:
              % /usr/local/squid/sbin/squid -z
              
        3. start the cache:
              % /usr/local/squid/sbin/squid


If you want to use the WWW interface to the Cache Manager, copy
the cachemgr.cgi program into your httpd server's cgi-bin
directory.




安装步骤:
./configure --prefix=/usr/local/squid
make all
sudo make install(因为要拷贝到系统目录,需要root权限,所以sudo了,你也可以root登录执行,我是ubuntu的系统,所以用sudo,有root权限就行)




检查配置文件:
sudo vi /usr/local/squid/etc/squid.conf
配置项1:
# Squid normally listens to port 3128
http_port 3128


配置项2:
acl localnet src 192.168.0.0/16
http_access allow localnet


配置项3:
# Uncomment and adjust the following to add a disk cache directory.
cache_dir ufs /usr/local/squid/var/cache/squid 100 16 128
cache_mem 32 MB (这一条必须配置)
否则你就会遭遇报错: 2013/10/12 16:16:55 kid1| WARNING cache_mem is larger than total disk cache space!



安装好了以后,系统中新建了一个用户squid,在组中一查,发现属于nobody组的:
cat /etc/passwd|grep squid
cat /etc/group|grep 65534



安装squid的所在目录是:/usr/local/squid
我闲得没事干,直接改了所属用户为squid:nobody
sudo chown -Rf squid:nobody /usr/local/squid




建立cache的时候,对下面目录需要nobody用户权限,这个是网上没有说的很清楚的地方,折腾了我半天:
sudo chown -Rf nobody /usr/local/squid/var/cache/
sudo chown -Rf nobody /usr/local/squid/var/logs/


否则你会遭遇:
WARNING: Cannot write log file: /usr/local/squid/var/logs/cache.log
FATAL: Failed to make swap directory /usr/local/squid/var/cache/squid/00: (13) Permission denied




初始化squid.conf里配置的cache目录,就是建立了一堆的目录:
sudo /usr/local/squid/sbin/squid -z


在前台启动squid,并输出启动过程 
sudo /usr/local/squid/sbin/squid -N -d1
显示ready to server reques,则启动成功。可以键入ctrl+c,停止squid,并以后台运行的方式启动。 
我没有在配置文件中配置DNS,而是在 /etc/resolv.conf 中配置:
domain site
nameserver x.x.x.x


所以打印出来的日志中就这样的:
2013/10/12 16:42:13| Adding nameserver x.x.x.x from /etc/resolv.conf


squid从这个配置文件中读取了dns配置来用。




启动squid后台运行 
sudo /usr/local/squid/sbin/squid -s
检查一下进程是否存在:ps -ef|grep squid


通过squid客户端查看squid运行状态 
/usr/local/squid/bin/squidclient -h 127.0.0.1 -p 3128 mgr:info




那台不能上网的机器配置如下:
export http_proxy=http://192.168.199.235:3128/
可以把这句写到你的启动文件中,比如什么.profile或者.bashrc,或者/etc/profile等等。


取消:unset http_proxy




测试一下能不能上网了:
wget www.163.com


能down下来文件就大功告成了!