电脑技术学习

FreeBSD网站平台建设全过程 第二步

dn001

第二步:接入Internet并配制代理服务

  使用ADSL接入Internet有两种情况,通过拨号获取动态ip或服务商直接给定静态ip。后者配制起来较容易。本文先讨论动态ip如何设置。

  由于第一步重新编译内核时已经加进了对Firewall的支持。
  这里就可以通过直接编辑/etc/ppp/ppp.conf文件和/etc/rc.conf文件就可以上网并支持NAT方式透明代理了。

# vi /etc/ppp/ppp.conf

  我的ppp.conf文件内容如下:(注意set前要留空格)

default:
set log Phase tun command
set ifaddr 10.0.0.1/0 10.0.0.2/0
adsl:           # 配置代号
set device PPPoE:vr0  # vr0 改成你连接ADSL modem的网卡名
set mru 1492
set mtu 1492
set authname username # username是拨号用户名
set authkey password  # password是拨号密码
set dial
set login
add default HISADDR

# vi /etc/rc.conf

  我的rc.conf文件内容如下:(动态ip)

# -- sysinstall generated deltas -- # Tue Jul 15 21:20:28 1997
# Created: Tue Jul 15 21:20:28 1997
# Enable network daemons for user convenience.
# Please make all changes to this file, not to /etc/defaults/rc.conf.
# This file now contains just the overrides from /etc/defaults/rc.conf.
hostname="wwwx.3322.org"         # 你的主机域名
ifconfig_fxp0="inet 192.168.0.1 netmask 255.255.255.0"  #内网网卡ip地址,fxp0是网卡名
inetd_enable="YES"            # 开机加载inetd
kern_securelevel_enable="NO"
linux_enable="YES"
nfs_reserved_port_only="NO"
sendmail_enable="NO"
sshd_enable="YES"
usbd_enable="NO"
gateway_enable="YES"
firewall_enable="YES"          #启用防火墙
firewall_script="/etc/rc.firewall"
firewall_type="open"
firewall_quiet="YES"
firewall_logging_enable="YES"
ppp_enable="YES"             # 开机自动拨号
ppp_mode="ddial"
ppp_nat="YES"              # 启用透明代理
ppp_profile="adsl"            # 配置代号
# -- sysinstall generated deltas --  # Wed Jul 16 06:52:13 1997

  这样重新启动后就可以拨号上网并实现透明代理了。客户端需要设置dns服务器为服务商提供的dns,网关设成代理服务器的内网卡ip地址,这里是192.168.0.1。并把IE中“internet选项”关于连接设置的所有复选框清除。

  如果解析不了域名,检查一下/etc/resolv.conf文件是否加入了正确的dns服务器地址。


  如果是静态ip方式,则只需要编辑/etc/rc.conf文件。

  我的/etc/rc.conf文件如下:(静态ip)

# -- sysinstall generated deltas -- # Tue Jul 15 21:20:28 1997
# Created: Tue Jul 15 21:20:28 1997
# Enable network daemons for user convenience.
# Please make all changes to this file, not to /etc/defaults/rc.conf.
# This file now contains just the overrides from /etc/defaults/rc.conf.
hostname="wwwx.3322.org"                   #主机域名
defaultrouter="218.10.104.1"                 #服务商提供的路由器地址
ifconfig_vr0="inet 218.10.104.188 netmask 255.255.255.0"  #服务商提供的静态ip
ifconfig_fxp0="inet 192.168.0.1 netmask 255.255.255.0"   #内部网卡ip
inetd_enable="YES"                       #开机加载inetd
kern_securelevel_enable="NO"
linux_enable="YES"
nfs_reserved_port_only="NO"
sshd_enable="YES"
sendmail_enable="NO"
usbd_enable="NO"
gateway_enable="YES"
firewall_enable="YES"
firewall_script="/etc/rc.firewall"
firewall_type="open"
firewall_quiet="YES"
firewall_logging_enable="YES"
natd_enable="YES"               # 启用透明代理
natd_interface="vr0"              # natd接口,vr0为连接外网modem的网卡
# -- sysinstall generated deltas -- # Wed Jul 16 06:52:13 1997

  重起后网络连接及透明代理生效。客户端同样要按上面说的方法配制。


  使用Squid:

  Squid是一个非常不错的代理缓存软件。我曾经一直在使用,后来因为我经常要改变web服务器里的网页,而Squid总是把我以前的页面缓存,致使不能马上反映页面的更新情况,再加上公司上网的负担不是很重。所以就不用了。

  安装方法:

  在FreeBSD下安装软件最方便的方法是使用ports。本文为了让大家对通用的软件安装方法做一定的了解,我们采用通用的方法来安装Squid,也就是说,下面的方法同样适用于Linux或其他Unix版本。

  在ylf的用户目录下创建目录app用来存放程序安装临时文件:

# mkdir /home/ylf/app

  将用户ylf设为/home/ylf/app目录及其子目录的所有者

# chown –R ylf /home/ylf/app

  到 http://www.squid-cache.org/Versions/v2/2.5/ 下载Squid 的最新稳定版本,现在是squid-2.5.STABLE3

  打开IE浏览器,在地址栏输入ftp://192.168.0.1 ,出现ftp登陆对话框,输入用户名ylf及密码,登录成功后。将下载的squid-2.5.STABLE3复制到app目录中。

  执行如下命令:

# cd /home/ylf/app
# tar zxvf squid-2.5.STABLE3.tar.gz    #解压缩安装包
# cd squid-2.5.STABLE3           #进入解开的目录
# ./configure --prefix=/usr/local/squid  #配制、将squid安装在/usr/local/squid目录
# make all                 #编译
# make install               #安装

  下面编辑Squid的配置文件:

# cd /usr/local/squid/etc

  将原来的配置文件改名:

# mv squid.conf squid.conf.bak

  编辑新的配置文件:

# vi squid.conf

  我的squid.conf内容如下:

#取消对代理阵列的支持
icp_port 0

#对日志文件和pid文件位置进行设置
cache_store_log none
cache_access_log /usr/local/squid/var/logs/access.log
cache_log /usr/local/squid/var/logs/cache.log
emulate_httpd_log on
pid_filename /usr/local/squid/var/logs/squid.pid

#设置运行时的用户和组权限
cache_effective_user squid
cache_effective_group squid

#设置管理信息
visible_hostname wwwx.3322.org.
cache_mgr yourname@yourdomain.com

#设置监听地址和端口
http_port 3128
udp_incoming_address 0.0.0.0

#设置squid用户hot object的物理内存的大小以及设置cache目录
cache_mem 32 MB
cache_dir ufs /usr/local/squid/cache 1024 16 256

#访问控制设置
acl mynet src 192.168.0.0/255.255.255.0
acl all src 0.0.0.0/0.0.0.0
http_access allow mynet
http_access deny all

#透明代理设置
httpd_accel_host virtual
httpd_accel_port 80
httpd_accel_with_proxy on
httpd_accel_uses_host_header on

#swap 性能微调
half_closed_clients off
cache_swap_high 100%
cache_swap_low 80%
maximum_object_size 1024 KB

#控制对象的超时时间
refresh_pattern -i .html 1440 90% 129600 reload-into-ims
refresh_pattern -i .shtml 1440 90% 129600 reload-into-ims
refresh_pattern -i .hml 1440 90% 129600 reload-into-ims
refresh_pattern -i .gif 1440 90% 129600 reload-into-ims
refresh_pattern -i .swf 1440 90% 129600 reload-into-ims
refresh_pattern -i .jpg 1440 90% 129600 reload-into-ims
refresh_pattern -i .png 1440 90% 129600 reload-into-ims
refresh_pattern -i .bmp 1440 90% 129600 reload-into-ims
refresh_pattern -i .js 1440 90% 129600 reload-into-ims

  需要改的地方是访问控制设置中的子网改成你自己的子网。其他的地方可根据需要调整。不改也可。

  如果不使用日志,将日志设置部分改成如下句子:

cache_store_log none
cache_access_log /dev/null
cache_log /dev/null

  添加squid系统用户和组:

# pw groupadd squid
# pw useradd squid -g squid -s /sbin/nologin

  建立cache目录:

# mkdir /usr/local/squid/cache

  改变cache目录和logs目录的所有者为squid用户和组:

# chown –R squid /usr/local/squid/cache
# chgrp –R squid /usr/local/squid/cache
# chown –R squid /usr/local/squid/var/logs
# chgrp –R squid /usr/local/squid/var/logs

  运行squid –z建立cache目录结构:

# /usr/local/squid/sbin/squid –z

  测试squid运行情况:

# /usr/local/squid/sbin/squid –NCd1

  出现下面显示证明squid安装成功:

2003/06/21 18:01:09| Starting Squid Cache version 2.5.STABLE3 for i386-unknown-freebsd4.7...
2003/06/21 18:01:09| Process ID 160
2003/06/21 18:01:09| With 957 file descriptors available
2003/06/21 18:01:09| Performing DNS Tests...
2003/06/21 18:01:09| Successful DNS name lookup tests...
2003/06/21 18:01:09| DNS Socket created at 0.0.0.0, port 1029, FD 4
2003/06/21 18:01:09| Adding nameserver 202.97.224.68 from /etc/resolv.conf
2003/06/21 18:01:09| Unlinkd pipe opened on FD 9
2003/06/21 18:01:09| Swap maxSize 1048576 KB, estimated 80659 objects
2003/06/21 18:01:09| Target number of buckets: 4032
2003/06/21 18:01:09| Using 8192 Store buckets
2003/06/21 18:01:09| Max Mem size: 32768 KB
2003/06/21 18:01:09| Max Swap size: 1048576 KB
2003/06/21 18:01:09| Store logging disabled
2003/06/21 18:01:09| Rebuilding storage in /usr/local/squid/cache (DIRTY)
2003/06/21 18:01:09| Using Least Load store dir selection
2003/06/21 18:01:09| Current Directory is /usr/local/squid/etc
2003/06/21 18:01:09| Loaded Icons.
2003/06/21 18:01:09| Accepting HTTP connections at 0.0.0.0, port 3128, FD 8.
2003/06/21 18:01:09| WCCP Disabled.
2003/06/21 18:01:09| Ready to serve requests.
2003/06/21 18:01:16| Done scanning /usr/local/squid/cache swaplog (0 entries)
2003/06/21 18:01:16| Finished rebuilding storage from disk.
2003/06/21 18:01:16| 0 Entries scanned
2003/06/21 18:01:16| 0 Invalid entries.
2003/06/21 18:01:16| 0 With invalid flags.
2003/06/21 18:01:16| 0 Objects loaded.
2003/06/21 18:01:16| 0 Objects expired.
2003/06/21 18:01:16| 0 Objects cancelled.
2003/06/21 18:01:16| 0 Duplicate URLs purged.
2003/06/21 18:01:16| 0 Swapfile clashes avoided.
2003/06/21 18:01:16| Took 7.3 seconds ( 0.0 objects/sec).
2003/06/21 18:01:16| Beginning Validation Procedure
2003/06/21 18:01:16| Completed Validation Procedure
2003/06/21 18:01:16| Validated 0 Entries
2003/06/21 18:01:16| store_swap_size = 0k
2003/06/21 18:01:17| storeLateRelease: released 0 object

  否则根据提示检查配制文件。

  为了使squid的透明代理起作用,需要设置端口转发。方法如下:

  编辑/etc/rc.firewall文件,添加下面一句:

ipfw add 00500 fwd 127.0.0.1,3128 tcp from 192.168.0.0/24 to any 80

  下面建立squid的启动脚本squid.sh:

  首先建立/usr/local/etc/rc.d目录:

# mkdir /usr/local/etc
# mkdir /usr/local/etc/rc.d
# cd /usr/local/etc/rc.d
# vi squid.sh

  文件内容如下:

#!/bin/sh

# if ! PREFIX=$(expr $0 : "(/.*)/etc/rc.d/$(basename $0)$"); then
#   echo "$0: Cannot determine the PREFIX" >&2
#   exit 1
# fi

case "$1" in
start)
  if [ -x /usr/local/squid/sbin/squid -a -f /usr/local/squid/etc/squid.conf ]; then
  (cd /usr/local/squid/var/logs; /usr/local/squid/sbin/squid >/dev/null 2>&1 &) ; echo -n ' squid'
  fi
  ;;
stop)
    /usr/local/squid/sbin/squid -k shutdown 2>&1
    # Uncomment this if you'd like the system to (attempt to
    # wait for) squid to shut down cleanly
    #echo "Sleeping for 45 seconds to allow squid to shutdown.."
    #sleep 45
  ;;
*)
  echo "Usage: `basename $0` {start|stop}" >&2
  ;;
esac

exit 0

  这样每次启动后,squid就会自动运行。

  运行/usr/local/etc/rc.d/squid.sh start 启动squid

  运行/usr/local/etc/rc.d/squid.sh stop 停止squid

标签: