一、安装 FreeBSD
A、系统分区
许多初次接触 FreeBSD 的用户面对分区操作时往往感到无从下手。实际上,分区方案并没有绝对的标准,完全取决于实际使用需求。以下是一份针对 120GB 硬盘的常见分区建议,供参考。
根分区(/)保留 1GB 左右看似够用,但由于 /root 工作目录也位于此分区,建议适当增大容量。/home/data 用于存放 Email 和 FTP 用户的数据。/tmp 分配 1GB,临时文件较多时不会出现空间不足的情况。/usr 分配 10GB —— 若不安装 GUI 图形界面,这个空间足以容纳各类软件和下载包。/var 分配 3GB,日志、邮件临时目录都会写入该分区;若空间不足,像 ama vis-new 这类杀毒工具将无法正常解包并处理邮件。
%df -h Filesystem Size Used A vail Capacity Mounted on /dev/ad0s1a 1.9G 481M 1.3G 26% / devfs 1.0K 1.0K 0B 100% /dev /dev/ad0s1g 92G 353M 84G 0% /home/data /dev/ad0s1e 989M 224K 910M 0% /tmp /dev/ad0s1f 9.7G 1.8G 7.1G 20% /usr /dev/ad0s1d 2.9G 105M 2.6G 4% /var
B、安装基本系统
安装系统时,建议选择 minimal(最小系统)并附带 ports。不必要的软件包一律不装,既能减小系统体积,也能降低被攻击的风险。作为服务器,图形界面从来都不是必需项。
下载 FreeBSD 镜像时,只需下载 Disc 1 即可;缺少的软件包完全可以通过网络安装完成。
C、配置系统
如果在安装后期没有完成系统配置,启动后可以 root 身份登录系统,运行 sysinstall 或使用 ee 编辑器直接修改 /etc/rc.conf。需要特别留意:大多数服务都必须在 /etc/rc.conf 中添加对应的启动项,才能正常随系统自启。
配置好固定 IP(或使用 DHCP 自动分配)后,再通过 SSH 客户端管理服务器,工作效率会明显提升。
SSH 配置要点
启用 sshd 通常是在安装过程中的标准步骤。检查 /etc/rc.conf 中是否有 sshd_enable="YES",该配置表示系统下次启动时自动加载 OpenSSH 服务。当然也可以手动启动:
/etc/rc.d/sshd start
限制登录用户:在 /etc/ssh/sshd_config 中使用 AllowUsers 选项,可以精确控制哪些用户从哪些 IP 地址登录。例如,只允许 root 用户从 192.168.1.32 登录:
AllowUsers root@192.168.1.32
若要允许 admin 用户从任意位置登录,直接写用户名:AllowUsers admin。多个用户之间用空格分隔。
注意:务必把所有需要登录的用户都列全,否则他们会被拒绝访问。
修改配置文件后,执行 /etc/rc.d/sshd reload 使其立即生效。
安全建议:不建议直接使用 root 远程登录。正确的做法是添加一个新用户,将其加入 wheel 组,然后以该用户身份登录,再通过 su - 切换到 root 执行管理操作。
SecureCRT 下的配置(以个人使用习惯为例):Session Options → Authentication → Primary 选择 Keyboard Interactive。若希望终端显示彩色,在 Emulation → Terminal → Xterm 中勾选 ANSI Color,然后在 /etc/csh.cshrc 中添加:
setenv LSCOLORS ExGxFxdxCxegedabagExEx setenv CLICOLOR yes set autolist
执行以下命令使配置生效:
sed -i.bak -E s/set\ prompt/#set\ prompt/g /root/.cshrc
退出后重新登录,即可看到彩色目录显示效果。
二、更新软件包
FreeBSD 的强大之处之一在于其 ports 系统,能够轻松安装最新版本的软件。如果安装时未选择 ports,请参考手册(Handbook)补装。
更新 ports 前,建议先修改系统默认的下载地址,以提高下载速度。编辑 /etc/make.conf,添加以下镜像站点:
MASTER_SITE_OVERRIDE?= \
https://ports.hshh.org/${DIST_SUBDIR}/ \
ftp://ftp.freebsdchina.org/pub/FreeBSD/ports/distfiles/${DIST_SUBDIR}/ \
ftp://ftp.tw.freebsd.org/pub/ports/distfiles/${DIST_SUBDIR}/ \
ftp://freebsd.csie.nctu.edu.tw/pub/ports/distfiles/${DIST_SUBDIR}/
CVSup 的镜像站点非常多,建议选择距离最近的站点,可以显著加快更新速度。具体站点列表可参考 freebsd.org 或 freebsdchina.org.cn。
安装 cvsup-without-gui:
% cd /usr/ports/net/cvsup-without-gui/ % make install clean
更新 ports:
% /usr/local/bin/cvsup -gL 2 -h cvsup4.freebsdchina.org /usr/share/examples/cvsup/ports-supfile
三、安装 MySQL
MySQL 有多个版本可选,请根据实际需求选择。这里以 mysql41-server 为例进行演示:
% cd /usr/ports/databases/mysql41-server/ % make install clean
在 /etc/rc.conf 中添加:
mysql_enable="YES"
复制配置文件(非必需):
cp /usr/local/share/mysql/my-small.cnf /usr/local/etc/my.cnf
补充说明:
- 如果不添加
mysql_enable="YES",将无法手动启动 MySQL。 - 想知道某个软件在
/etc/rc.conf中应添加什么配置,可以查看/usr/local/etc/rc.d/下对应的启动脚本,里面通常有详细说明。
% /usr/local/etc/rc.d/mysql-server.sh start Starting mysql.
检查服务是否正常启动,可以使用 ps 查看进程,或使用 netstat 查看端口监听情况:
% ps aux|grep mysql mysql 94899 0.2 0.5 1644 1240 p0 S 3:52PM 0:00.07 /bin/sh /usr/local/bin/mysqld_safe -- mysql 94919 0.0 10.8 55564 27428 p0 S 3:52PM 0:01.54 /usr/local/libexec/mysqld --defaults- % netstat -an|grep 3306 tcp4 0 0 *.3306 *.* LISTEN
MySQL 安装后 root 用户默认密码为空,强烈建议立即修改:
% /usr/local/bin/mysqladmin -u-root -p password 你的新密码 Enter password:
如果服务器仅供内部使用,可以在 my.cnf 中添加以下配置,限制仅本地访问,提升安全性:
[mysqld] bind_address=127.0.0.1
四、安装 Apache
1. 安装 Apache Server
Apache 在 Web 服务器领域的地位无可替代。通过 ports 安装:
% cd /usr/ports/www/apache22/ % make install clean
安装过程中会出现选项菜单(例如 Python 的线程支持等),根据自身需求选择即可。典型的选项界面如下(以 Python 2.4.3 为例):
Options for python 2.4.3 │ [X] THREADS Enable thread support │ [ ] HUGE_STACK_SIZE Use a larger thread stack │ [X] UCS4 Use UCS4 for unicode support │ [X] PYMALLOC Use python's internal malloc │ [ ] IPV6 Enable IPv6 support │ [ ] FPECTL Enable floating point exception handling
在 /etc/rc.conf 中添加:
apache22_enable="YES"
启动时若遇到“Could not reliably determine the server's fully qualified domain name”错误,请在 /usr/local/etc/apache22/httpd.conf 约第 144 行位置添加:
ServerName mail.extmail.org
启动 Apache:
% /usr/local/etc/rc.d/apache22.sh start Performing sanity check on apache22 configuration: Syntax OK Starting apache22.
2. 安装 PHP
% cd /usr/ports/www/mod_php4/ % make install clean
同样会出现选项窗口,例如:
Options for mod_php4 4.4.2_1,1 │ [ ] DEBUG Enable debug │ [X] MULTIBYTE Enable zend multibyte support │ [ ] IPV6 Enable ipv6 support │ [X] OPENSSL Build static OpenSSL extension
在 httpd.conf 中添加以下两行,让 Apache 能够解析 PHP 文件:
AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
将 index.php 加入目录索引,约在 httpd.conf 的 212 行:
DirectoryIndex index.html index.php
重启或重载 Apache 使其生效:
% /usr/local/etc/rc.d/apache22.sh reload Performing sanity check on apache22 configuration: Syntax OK Performing a graceful restart
3. 安装 PHP 扩展
% cd /usr/ports/lang/php4-extensions/ % make install clean
这里建议:若没有特殊需求,尽量不安装 GD 库以节省编译时间。根据实际需要勾选对应模块,常见的选项包括:
Options for php4-extensions 1.0 │ [ ] BCMATH bc style precision math functions │ [X] BZ2 bzip2 library support │ [X] CTYPE ctype functions │ [X] CURL CURL support │ [X] FTP FTP support │ [X] GETTEXT gettext library support │ [X] ICONV iconv support │ [X] IMAP IMAP support │ [X] MBSTRING multibyte string support │ [X] MCRYPT Encryption support │ [X] MYSQL MySQL database support │ [X] OPENSSL OpenSSL support │ [X] OVERLOAD user-space object overloading support │ [X] PCRE Perl Compatible Regular Expression support │ [X] POSIX POSIX-like functions │ [X] SESSION session support │ [X] TOKENIZER tokenizer support │ [X] XML XML support │ ... 等等
安装完成后,可以在 /usr/local/www/apache22/data/ 下创建一个测试文件 test.php:
4. 安装 phpMyAdmin 管理数据库
为简化安装过程,这里推荐手工安装方式。先下载软件包,解压后复制到 Web 目录下。
% cd /usr/ports/databases/phpmyadmin/ % make fetch % cd /usr/ports/distfiles % tar jxvf phpMyAdmin-2.7.0-pl2.tar.bz2 % cp -r /usr/ports/distfiles/phpMyAdmin-2.7.0-pl2 /usr/local/www/apache22/data/phpmyadmin
然后修改配置文件 config.default.php:
% cd /usr/local/www/apache22/data/phpmyadmin/ % ee config.default.php
将 $cfg['Servers'][$i]['auth_type'] = 'config'; 改为:
$cfg['Servers'][$i]['auth_type'] = 'http';
这样打开页面时会弹出用户名密码验证窗口。最后设置权限:
#chmod 755 test.php
访问 https://IP/phpmyadmin 即可管理 MySQL 数据库。
注意: 你下载的 phpMyAdmin 版本可能与示例不同,请根据实际版本进行调整。
另一种常见方法——通过 Port 安装 phpMyAdmin-2.8.1:
# whereis phpmyadmin phpmyadmin: /usr/ports/databases/phpmyadmin # cd /usr/ports/databases/phpmyadmin # make install clean
安装后,进入 /usr/local/www/phpMyAdmin/,修改 libraries/config.default.php:
# vi libraries/config.default.php 找到 $cfg['PmaAbsoluteUri'] = ''; 修改成 $cfg['PmaAbsoluteUri'] = 'https://你的IP/phpmyadmin'; 找到 $cfg['Servers'][$i]['auth_type'] = 'config'; $cfg['Servers'][$i]['user'] = 'root'; $cfg['Servers'][$i]['password'] = ''; 写上自己的 MySQL 用户名和密码,保存退出。
由于 phpMyAdmin 默认路径为 /usr/local/www/phpmyadmin,需要在 Apache 中建立别名。编辑 /usr/local/etc/apache22/httpd.conf,在 Alias 组中加入:
Alias /phpmyadmin "/usr/local/www/phpMyAdmin/"Order allow,deny Allow from all
保存后重启 Apache:
# /usr/local/etc/rc.d/apache22.sh reload
然后访问 https://your IP/phpmyadmin。若想使用 HTTP 验证方式,同样修改 config.default.php 中的 auth_type 为 'http'。
5. 使用同一个 IP 的虚拟主机设置
首先取消中心主机,在 httpd.conf 的 ServerName 前面添加 # 注释掉。然后添加虚拟主机配置。假设你为域名 domain.tld 提供服务,同时还想在同一个 IP 上增加一个 otherdomain.tld:
NameVirtualHost *ServerName domain.tld ServerAlias domain.tld *.domain.tld DocumentRoot /www/domain ServerName otherdomain.tld DocumentRoot /www/otherdomain
这种添加别名的方法思路是一致的。
6. 安装 Webmin
# whereis webmin webmin: /usr/ports/sysutils/webmin # cd /usr/ports/sysutils/webmin # make install clean # cd /usr/local/etc/webmin # sh start
然后访问 https://IP:10000 即可通过 Web 界面管理服务器。
