编译安装Apache实现单服务器托管多网站(完整教程)
编译安装的Apache(httpd)无默认系统服务配置和标准化目录结构,需手动配置虚拟主机实现多网站托管,以下涵盖基于域名、基于端口、基于IP三种核心模式,步骤详细可落地。
一、前期准备
1. 确认Apache编译安装路径
默认编译安装路径为 /usr/local/httpd/(若编译时通过 --prefix 指定自定义路径,需全程替换),核心目录说明:
- 主配置文件:
/usr/local/httpd/conf/httpd.conf - 虚拟主机配置文件:
/usr/local/httpd/conf/extra/httpd-vhosts.conf - 网站根目录:
/usr/local/httpd/htdocs/ - 日志目录:
/usr/local/httpd/logs/ - 二进制工具:
/usr/local/httpd/bin/apachectl
2. 启用虚拟主机模块
编辑主配置文件,确保加载虚拟主机模块并引入虚拟主机配置文件:
bash
vi /usr/local/httpd/conf/httpd.conf
找到以下配置,确保未被注释(若已注释,删除行首 #):
plain
# 加载虚拟主机核心模块
LoadModule vhost_alias_module modules/mod_vhost_alias.so
# 引入虚拟主机配置文件(关键:启用httpd-vhosts.conf)
Include conf/extra/httpd-vhosts.conf
保存退出(ESC → :wq)。
3. 检查配置语法(提前规避错误)
bash
/usr/local/httpd/bin/apachectl -t
输出 Syntax OK 表示配置无误,若报错需先修正。
二、模式1:基于域名的虚拟主机(最常用)
同一IP+80端口,通过不同域名区分多个网站,适合对外提供多域名站点服务。
1. 配置域名解析(关键前提)
- 公网服务器:在域名服务商后台,将多个域名(如
site1.com、site2.com)解析到服务器公网IP。 - 内网/测试环境:修改服务器本地
hosts文件(或客户端hosts文件),添加域名与IP映射:
bash
# 编辑hosts文件
vi /etc/hosts
# 添加内容(替换为你的服务器IP)
192.168.1.100 site1.com www.site1.com
192.168.1.100 site2.com www.site2.com
2. 编辑虚拟主机配置文件
bash
vi /usr/local/httpd/conf/extra/httpd-vhosts.conf
删除默认示例配置,添加两个域名对应的虚拟主机配置:
plain
# 站点1:site1.com
<VirtualHost *:80>
# 主域名
ServerName site1.com
# 域名别名(带www的域名)
ServerAlias www.site1.com
# 网站1根目录(需手动创建)
DocumentRoot "/usr/local/httpd/htdocs/site1"
# 网站管理员邮箱(可选)
ServerAdmin admin@site1.com
# 目录权限配置(避免403错误)
<Directory "/usr/local/httpd/htdocs/site1">
Options Indexes FollowSymLinks # 允许目录索引、跟随软链接
AllowOverride All # 允许.htaccess文件生效
Require all granted # 允许所有客户端访问
</Directory>
# 独立日志配置(便于排查单个站点问题)
ErrorLog "logs/site1_error.log" # 错误日志
CustomLog "logs/site1_access.log" combined # 访问日志(combined格式更详细)
</VirtualHost>
# 站点2:site2.com
<VirtualHost *:80>
ServerName site2.com
ServerAlias www.site2.com
DocumentRoot "/usr/local/httpd/htdocs/site2"
ServerAdmin admin@site2.com
<Directory "/usr/local/httpd/htdocs/site2">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog "logs/site2_error.log"
CustomLog "logs/site2_access.log" combined
</VirtualHost>
3. 创建网站根目录并添加测试页面
bash
# 创建两个站点的根目录
mkdir -p /usr/local/httpd/htdocs/site1
mkdir -p /usr/local/httpd/htdocs/site2
# 创建测试页面
echo "这是基于域名的站点1:site1.com" > /usr/local/httpd/htdocs/site1/index.html
echo "这是基于域名的站点2:site2.com" > /usr/local/httpd/htdocs/site2/index.html
# 赋予合适权限(避免访问权限错误)
chmod -R 755 /usr/local/httpd/htdocs/
4. 配置生效与验证
bash
# 1. 检查配置语法
/usr/local/httpd/bin/apachectl -t
# 2. 平滑重载Apache(不中断现有连接,推荐)
/usr/local/httpd/bin/apachectl graceful
# 3. 验证站点
# 本地验证
curl http://site1.com
curl http://site2.com
# 远程客户端验证:浏览器访问 http://site1.com、http://site2.com
三、模式2:基于端口的虚拟主机(无需域名)
同一IP,通过不同端口区分多个网站,适合内网测试、多站点隔离(无需配置域名解析)。
1. 添加Apache监听端口
编辑主配置文件,添加额外端口(如8080、8081):
bash
vi /usr/local/httpd/conf/httpd.conf
找到 Listen 80,在下方添加新端口:
plain
# 保留默认80端口
Listen 80
# 添加自定义端口(按需添加,建议1024以上)
Listen 8080
Listen 8081
保存退出。
2. 编辑虚拟主机配置文件
bash
vi /usr/local/httpd/conf/extra/httpd-vhosts.conf
添加不同端口对应的虚拟主机配置(可与域名模式共存):
plain
# 80端口站点(默认站点)
<VirtualHost *:80>
ServerName web80
DocumentRoot "/usr/local/httpd/htdocs/web80"
<Directory "/usr/local/httpd/htdocs/web80">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog "logs/web80_error.log"
CustomLog "logs/web80_access.log" combined
</VirtualHost>
# 8080端口站点
<VirtualHost *:8080>
ServerName web8080
DocumentRoot "/usr/local/httpd/htdocs/web8080"
<Directory "/usr/local/httpd/htdocs/web8080">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog "logs/web8080_error.log"
CustomLog "logs/web8080_access.log" combined
</VirtualHost>
# 8081端口站点
<VirtualHost *:8081>
ServerName web8081
DocumentRoot "/usr/local/httpd/htdocs/web8081"
<Directory "/usr/local/httpd/htdocs/web8081">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog "logs/web8081_error.log"
CustomLog "logs/web8081_access.log" combined
</VirtualHost>
3. 创建网站根目录与测试页面
bash
# 创建3个端口对应的根目录
mkdir -p /usr/local/httpd/htdocs/web80
mkdir -p /usr/local/httpd/htdocs/web8080
mkdir -p /usr/local/httpd/htdocs/web8081
# 添加测试页面
echo "这是80端口站点" > /usr/local/httpd/htdocs/web80/index.html
echo "这是8080端口站点" > /usr/local/httpd/htdocs/web8080/index.html
echo "这是8081端口站点" > /usr/local/httpd/htdocs/web8081/index.html
# 赋予权限
chmod -R 755 /usr/local/httpd/htdocs/
4. 开放防火墙端口(关键)
bash
# CentOS/RHEL(firewalld)
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --permanent --add-port=8081/tcp
firewall-cmd --reload
# 验证端口开放情况
firewall-cmd --list-ports
5. 配置生效与验证
bash
# 1. 检查配置语法
/usr/local/httpd/bin/apachectl -t
# 2. 平滑重载Apache
/usr/local/httpd/bin/apachectl graceful
# 3. 验证站点
# 本地验证
curl http://127.0.0.1:80
curl http://127.0.0.1:8080
curl http://127.0.0.1:8081
# 远程验证:浏览器访问 http://服务器IP:80、http://服务器IP:8080、http://服务器IP:8081
四、模式3:基于IP的虚拟主机(多IP场景)
服务器绑定多个独立IP,每个IP对应一个网站,适合需要独立IP的场景(如SSL证书独立部署)。
1. 为服务器绑定多个IP
先确认服务器网卡名称(如 ens33),添加额外内网/公网IP:
bash
# 临时添加IP(重启网卡失效,用于测试)
ifconfig ens33:0 192.168.1.101 netmask 255.255.255.0 up
ifconfig ens33:1 192.168.1.102 netmask 255.255.255.0 up
# 永久添加IP:编辑网卡配置文件(CentOS为例)
vi /etc/sysconfig/network-scripts/ifcfg-ens33:0
添加以下内容(按需修改IP和网卡信息):
plain
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.1.101
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
重启网卡生效:
bash
systemctl restart network
2. 编辑虚拟主机配置文件
bash
vi /usr/local/httpd/conf/extra/httpd-vhosts.conf
添加不同IP对应的虚拟主机配置:
plain
# IP1:192.168.1.100(80端口)
<VirtualHost 192.168.1.100:80>
ServerName web100
DocumentRoot "/usr/local/httpd/htdocs/web100"
<Directory "/usr/local/httpd/htdocs/web100">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog "logs/web100_error.log"
CustomLog "logs/web100_access.log" combined
</VirtualHost>
# IP2:192.168.1.101(80端口)
<VirtualHost 192.168.1.101:80>
ServerName web101
DocumentRoot "/usr/local/httpd/htdocs/web101"
<Directory "/usr/local/httpd/htdocs/web101">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog "logs/web101_error.log"
CustomLog "logs/web101_access.log" combined
</VirtualHost>
3. 创建网站根目录与测试页面
bash
mkdir -p /usr/local/httpd/htdocs/web100
mkdir -p /usr/local/httpd/htdocs/web101
echo "这是IP 192.168.1.100 对应的站点" > /usr/local/httpd/htdocs/web100/index.html
echo "这是IP 192.168.1.101 对应的站点" > /usr/local/httpd/htdocs/web101/index.html
chmod -R 755 /usr/local/httpd/htdocs/
4. 配置生效与验证
bash
# 1. 检查配置语法
/usr/local/httpd/bin/apachectl -t
# 2. 平滑重载Apache
/usr/local/httpd/bin/apachectl graceful
# 3. 验证站点
curl http://192.168.1.100
curl http://192.168.1.101
# 远程浏览器访问对应IP即可
五、日常管理常用操作
1. 添加新站点
- 根据托管模式,配置域名解析/添加监听端口/绑定新IP;
- 在
httpd-vhosts.conf中添加新的<VirtualHost>配置块; - 创建对应网站根目录与页面;
- 检查语法(
apachectl -t)→ 平滑重载(apachectl graceful); - (端口模式)开放防火墙新端口。
2. 修改现有站点配置
- 编辑
httpd-vhosts.conf中对应站点的配置; - 检查语法 → 平滑重载(无需重启,不影响业务)。
3. 停用站点
- 注释对应站点的
<VirtualHost>配置块(行首加#); - (端口模式)注释
httpd.conf中的对应监听端口; - 检查语法 → 平滑重载;
- (可选)关闭防火墙对应端口/删除域名解析。
4. 排查故障
bash
# 1. 查看Apache状态
/usr/local/httpd/bin/apachectl status
# 2. 查看站点错误日志(以site1为例)
tail -f /usr/local/httpd/logs/site1_error.log
# 3. 查看端口监听情况
netstat -tulpn | grep httpd
# 4. 重新检查配置语法
/usr/local/httpd/bin/apachectl -t
六、注意事项
- 路径一致性:若编译时自定义了安装路径(如
/opt/httpd),需全程替换默认路径/usr/local/httpd/; - 权限问题:网站根目录权限建议设为
755,避免Apache无法读取文件; - 语法优先:任何配置修改后,必须先执行
apachectl -t检查语法,再重载服务; - 平滑重载:生产环境优先使用
apachectl graceful,而非restart,避免断开现有客户端连接; - 安全防护:非必要不开放过多端口,公网站点建议搭配反向代理或SSL证书(HTTPS配置需加载
mod_ssl模块)。
总结
编译安装Apache托管多网站的核心流程:
- 启用虚拟主机模块,引入
httpd-vhosts.conf; - 选择托管模式(域名/端口/IP),配置对应前置条件(域名解析/监听端口/多IP绑定);
- 编写虚拟主机配置,指定根目录、权限、日志;
- 创建站点目录与测试页面,赋予权限;
- 检查语法 → 平滑重载 → 验证站点;
- 日常管理通过
apachectl工具实现配置生效与故障排查。