网络安全 频道

FreeBSD网站平台建设全过程(上)

前言 
写本文的初衷主要是记录下我一段时间学习使用FreeBSD建设网站平台的一些经验和体会。因为本人是菜鸟,所以很多地方不够完善。本文权当给初学者提供一个具体可操作的实例。所有操作步骤都是我边做边写。避免出现遗漏或一些不必要的小错误,给初学者带来麻烦。 
本网站平台建设全过程包括FreeBSD系统的安装,web、ftp、mail、proxy服务器、视频点播服务器等。所有过程都在FreeBSD4.7环境下测试通过。 
由于写本文时参考了网上太多前辈和大侠的资料,无法一一列举。还请作者原谅。 
本连载文章前后关联很紧密,建议初学者一步一步来做。 
试验环境如下: 
硬件环境:普通pc机,双网卡。配制不需要高,主要是稳定,并能适应长时间开机。听前辈介绍AMD k6的cpu运行FreeBSD好像有点问题。不过我没有试验。建议用intel的。 
软件环境:操作系统:FreeBSD4.7(4.8) 
web服务器:Apache 1.3.27+modssl +mod_php4+mod_gzip+mod_fastcgi +mod_perl +mysql3.23 
ftp服务器:Proftpd1.2.7+proftpd-mod-quotatab-1.2.4+mysql 
mail服务器: iceblood前辈制作的qmail安装包(经过修改)+vqregister-2.5 
proxy服务器:使用FreeBSD内置的NAT和PPP拨号+squid 
视频点播服务器:Helix Universal Servevr (realserver9.01) 
网络环境:我用的是adsl动态拨号方式,因为动态ip所存在的一些问题,我把所有的服务都装在一台机器上,且这台机器要直接与外网相连。静态ip更方便一些。(本文两种方式都会讨论) 

第一步:安装系统 
关于安装过程网上有很多文章,这里不作详述。仅提出几点建议供大家参考: 
1、 采用最小化安装。 
2、 安装时启用inetd,并在编辑inetd.conf文件时,把带有“ftp”的行前面的“#”号去掉。这样作是为了以后安装软件时可以用ftp上传文件。 
3、 添加一个wheel组的用户,这样在系统工作正常后可以在windows系统上通过终端登录软件(如SecureCRT)进行所有操作。FreeBSD4.7在最小安装时支持SSH终端登录,所以我们需要使用支持SSH协议的终端软件,如SecureCRT。例如我添加的用户名是ylf,隶属wheel系统组。系统产生一个用户目录/home/ylf。我可以通过SecureCRT以ylf身份登录到系统,然后通过su命令切换到root用户。同时我也可以在IE浏览器内键入ftp://192.168.0.1 ,并输入ylf用户名和密码登录ftp将需要的文件上传到自己的用户目录里。(192.168.0.1是我的服务器连接内网的网卡ip地址)。以下除系统安装部分,我都是采用终端方式操作。 
4、 分区时将/home、/usr和/var划的大一些,因为/usr为程序目录,/var要存放日志,/home是用户主目录,我的用户文件如网页、ftp上传文件等都放在这里。还有最好保留一部分空间留作以后需要的时候用。我的机器上有2块硬盘,一快18.2G SCSI硬盘,一块40G IDE硬盘。我是这样划分的。 
128M / 
20G /home 
2G /ftp 
256M /tmp 
6G /usr 
5G /var 
其中/ftp是为匿名ftp用户访问专用。剩余空间留作备用。当然如果硬盘空间少的话,做我们的试验也是够用的。 
关于安装方面的文章,大家也可以参考delphij写的《FreeBSD服务器的安装与优化》。http://community.freebsdchina.org/catalog.php?forum=34 一文。 
安装完系统后,要重新编译内核。目的是要系统支持Firrwall和提高运行效率。 
首先确认系统是否安装了FreeBSD的内核支持。如果是最小安装,则需要运行/stand/sysinstall命令安装内核支持。方法如下: 
# /stand/sysinstall 
选Configure—Distributions—src—sys,内核文件将被安装在/usr/src/sys目录中。 
注意:这之后的过程在终端上操作要方便许多。SecureCRT支持在终端界面上直接拷贝和粘贴文本内容。 
转到内核文件目录 
# cd /usr/src/sys/i386/conf 
编辑内核文件 
# vi kernel_wwwx # kernel_wwwx为我的内核文件名 
我的内核文件如下: 

# GENERIC -- Generic kernel configuration file for FreeBSD/i386 

# For more information on this file, please read the handbook section on 
# Kernel Configuration Files: 

#http://www.FreeBSD.org/doc/en_US.IS...fig-config.html 

# The handbook is also available locally in /usr/share/doc/handbook 
# if you"ve installed the doc distribution, otherwise always see the 
# FreeBSD World Wide Web server (http://www.FreeBSD.org/) for the 
# latest information. 

# An exhaustive list of options and more detailed explanations of the 
# device lines is also present in the ./LINT configuration file. If you are 
# in doubt as to the purpose or necessity of a line, check first in LINT. 

# $FreeBSD: src/sys/i386/conf/GENERIC,v 1.246.2.48 2002/08/31 20:28:26 obrien Exp $ 

machine i386 
cpu I586_CPU 
cpu I686_CPU 
ident kernel_wwwx #内核文件名,这个要和你的内核文件名一致 
maxusers 0 

options INET #InterNETworking 
options FFS #Berkeley Fast Filesystem 
options FFS_ROOT #FFS usable as root device [keep this!] 
options SOFTUPDATES #Enable FFS soft updates support 
options UFS_DIRHASH #Improve performance on big directories 
options PROCFS #Process filesystem 
options COMPAT_43 #Compatible with BSD 4.3 [KEEP THIS!] 
options SCSI_DELAY=15000 #Delay (in ms) before probing SCSI 
options SYSVSHM #SYSV-style shared memory 
options SYSVMSG #SYSV-style message queues 
options SYSVSEM #SYSV-style semaphores 
options P1003_1B #Posix P1003_1B real-time extensions 
options _KPOSIX_PRIORITY_SCHEDULING 
options ICMP_BANDLIM #Rate limit bad replies 
options AHC_REG_PRETTY_PRINT # Print register bitfields in debug 
# output. Adds ~128k to driver. 
options AHD_REG_PRETTY_PRINT # Print register bitfields in debug 
# output. Adds ~215k to driver. 

device tun 1 
options IPFIREWALL #防火墙 
options IPFIREWALL_FORWARD #允许透明代理 
options IPFIREWALL_VERBOSE #允许防火墙日志 
options IPFIREWALL_VERBOSE_LIMIT=100 #限制日志 
options IPFIREWALL_DEFAULT_TO_ACCEPT #默认允许所有IP包 
options IPDIVERT #启用由ipfw divert使用的转向IP套接字 

# To make an SMP kernel, the next two are needed 
#options SMP # Symmetric MultiProcessor Kernel 
#options APIC_IO # Symmetric (APIC) I/O 

device isa 
device eisa 
device pci 


# ATA and ATAPI devices 
device ata 
device atadisk # ATA disk drives 


# SCSI Controllers #没有SCSI设备不需要这段 
device ahb # EISA AHA1742 family 
device ahc # AHA2940 and onboard AIC7xxx devices 
device ahd # AHA39320/29320 and onboard AIC79xx devices 
device amd # AMD 53C974 (Tekram DC-390(T)) 
device isp # Qlogic family 
device mpt # LSI-Logic MPT/Fusion 
device ncr # NCR/Symbios Logic 
device sym # NCR/Symbios Logic (newer chipsets) 
options SYM_SETUP_LP_PROBE_MAP=0x40 
# Allow ncr to attach legacy NCR devices when 
# both sym and ncr are configured 

device adv0 at isa? 
device adw 
device bt0 at isa? 
device aha0 at isa? 
device aic0 at isa? 

device ncv # NCR 53C500 
device nsp # Workbit Ninja SCSI-3 
device stg # TMC 18C30/18C50 

# SCSI peripherals #没有SCSI设备不需要这段 
device scbus # SCSI bus (required) 
device da # Direct Access (disks) 
device sa # Sequential Access (tape etc) 
device cd # CD 
device pass # Passthrough device (direct SCSI access) 

# atkbdc0 controls both the keyboard and the PS/2 mouse 
device atkbdc0 at isa? port IO_KBD 
device atkbd0 at atkbdc? irq 1 flags 0x1 

device vga0 at isa? 


# syscons is the default console driver, resembling an SCO console 
device sc0 at isa? flags 0x100 


# Floating point support - do not disable. 
device npx0 at nexus? port IO_NPX irq 13 


# Serial (COM) ports 
device sio0 at isa? port IO_COM1 flags 0x10 irq 4 

# 我用的是8139和Dlink DFE-530TX网卡,大家可根据自己的网卡型号保留或删除 
# 使用公共的MII总线控制器代码的PCI以太网适配器 
# 注意:一定要保留"device miibus"以确保可用 
# PCI Ethernet NICs that use the common MII bus controller code. 
# NOTE: Be sure to keep the "device miibus" line in order to use these NICs! 
device miibus # MII bus support 
device fxp # Intel EtherExpress PRO/100B (82557, 82558) 
device rl # RealTek 8129/8139 
device vr # VIA Rhine, Rhine II 
device xl # 3Com 3c90x (``Boomerang"", ``Cyclone"") 

# Pseudo devices - the number indicates how many units to allocate. 
pseudo-device loop # Network loopback 
pseudo-device ether # Ethernet support 
pseudo-device sl 1 # Kernel SLIP 
pseudo-device ppp 1 # Kernel PPP 
pseudo-device tun # Packet tunnel. 
pseudo-device pty # Pseudo-ttys (telnet etc) 
pseudo-device md # Memory "disks" 
pseudo-device gif # IPv6 and IPv4 tunneling 
pseudo-device faith 1 # IPv6-to-IPv4 relaying (translation) 

# The `bpf" pseudo-device enables the Berkeley Packet Filter. 
# Be aware of the administrative consequences of enabling this! 
pseudo-device bpf #Berkeley packet filter 
(完) 

我用的是8139网卡和Dlink DFE-530TX网卡,如果你用其他型号的网卡,需要察看当前目录的GENERIC 内核文件,找到描述自己的网卡型号的段并将其添加到新的内核文件里。其他的不改直接拷贝过来就可以用了。 
接下来编译安装新内核: 
# /usr/sbin/config kernel_wwwx #kernel_wwwx为你的内核文件名 
# cd ../../compile/kernel_wwwx 
# make depend 
# make 
# make install 
重新启动(reboot) 

如果系统升级过源代码树,按下面方法编译内核: 
# cd /usr/src 
# make kernel KERNCONF=kernel_wwwx #kernel_wwwx为你的内核文件名 
重新启动

第二步:接入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 

关于域名的问题 
如果需要对外提供www服务,域名必不可少。域名分静态和动态域名两种,网上提供二级免费域名的站点有很多,本文例子的域名wwwx.3322.org就是在希网申请的( http://www.3322.org )。希网同时提供支持FreeBSD客户端的动态域名服务。如果是使用拨号上网的情况,则需要使用动态域名服务。由于拨号方式获得的ip地址是变化的,因此动态域名需要每次拨号上网后,客户端运行域名更新程序与服务端联系,使得申请的域名可以随时指向变化的ip地址,以完成动态域名解析服务。希网的网站上提供了详细的在FreeBSD上安装动态域名客户程序的方法,详情参见http://www.3322.org/help/help_service.html#service_3 。大家可以到那里去下载客户程序并按照说明安装。另外一个比较好的提供免费动态域名服务的网站是科迈网,他们的动态域名可以支持内网机器的域名解析http://netadmin.77169.com/HTML/20031106203400.html

0
相关文章