如何配置Ubuntu Apache虚拟主机,轻松实现网站多域名高效管理?
- 内容介绍
- 文章标签
- 相关推荐
常见痛点:在实际运营中,站长经常会遇到以下困扰:
- 配置文件语法错误导致 Apache 启动失败。
- 多个域名指向同一服务器时网站互相覆盖、访问不到预期站点。
- 文件权限不当导致 403 Forbidden 错误。
- DNS 解析延迟或未生效,使得新域名无法访问。老实说,
- 忘记启用/禁用站点后仍然使用默认站点。引发冲突,
1. 环境准备:安装 Apache 并确认服务运行
在开始之前,请确保你的 Ubuntu 程序已经安装了 Apache。按理说,如果尚未安装,请执行:
sudo apt update
sudo apt install -y apache2
sudo systemctl enable apache2
sudo systemctl start apache2
检查 Apache 是否正常运行:
sudo systemctl status apache2
2. 添加站点根目录并设置正确权限
以你的真实域名为例,在 /var/www 下创建站点目录:
# 创建目录
sudo mkdir -p /var/www/yourdomain.com
# 将所有权交给 Apache
sudo chown -R www-data:www-data /var/www/yourdomain.com
sudo chmod -R 755 /var/www/yourdomain.com
3. 编写虚拟主机配置文件
在 /etc/apache2/sites-available 目录下新建一个以域名命名的配置文件:
# 使用 nano 编辑
sudo nano /etc/apache2/sites-available/yourdomain.com.conf
将下面的模板粘贴进去。并根据实际情况替换占位符:
ServerAdmin
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/yourdomain.com
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
ErrorLog ${APACHE_LOG_DIR}/yourdomain.com.error.log
CustomLog ${APACHE_LOG_DIR}/yourdomain.com.access.log combined
注意事项
- 方法必须准确无误:DocumentRoot 与 Directory 必须保持一致,否则会出现 404 或 403 错误。
- ServerName 与 ServerAlias:确保把所有想要绑定的子域都写进来避免访问不到子域。
- ErrorLog 与 CustomLog:开启日志可以快速定位配置错误。
4. 启用站点并禁用默认站点
启用新建的虚拟主机:
# 启用站点
sudo a2ensite yourdomain.com.conf
# 禁用默认站点
sudo a2dissite 000-default.conf
# 检查配置语法是否正确
sudo apachectl configtest
If configtest returns “Syntax OK”,proceed;orwise fix reported line numbers.
5. 启用 CGI 支持
If your site needs CGI scripts,手动开启对应模块:
# 启用 CGI 模块及相关依赖
sudo a2enmod cgi
# 若使用 perl/c++ 等语言,还可能需要:
# sudo apt install libapache2-mod-fcgid
# 重启使模块生效
sudo systemctl restart apache2
6. 配置 DNS 解析
登录你的域名注册商控制面板。将以下记录指向服务器公网 IP:
-
A record:
@ → YOUR.SERVER.IP -
至于CNAME,
www → @
*注:DNS 生效可能需要数分钟到数小时请耐心等待。
7. 重启 Apache 并验证效果
# 重新启动。使所有改动生效
sudo systemctl restart apache8 || sudo systemctl restart apache2
# 检查端口是否监听
sudo ss -tulnp | grep :80
打开浏览器,访问。如果看到默认页面或你放置在根目录的 index 文件,即表示配置成功。
8. 常见故障排查步骤
-
"Failed to start Apache HTTP Server":
运行
apachectl configtest,检查是否有拼写错误或重复的 VirtualHost 定义。 - "403 Forbidden": 确认目录所有者是 www-data 且权限为 755; 检查 .htaccess 中是否有拒绝规则。
- "404 Not Found": 确认 DocumentRoot 指向正确方法,且 index.html 或 index.php 存在。
- "Domain not resolved": 确认 DNS A 记录已经正确填写且已传播;使用 `dig yourdomain.com` 验证 IP 是否返回正确。
9. 为多域名站点添加 SSL/TLS 加密
If you plan to serve traffic over HTTPS。use Certbot 自动申请 Let’s Encrypt 免费证书:
# 安装 Certbot 插件
sudo apt install -y certbot python3-certbot-apache
# 为该站点获取并自动配置证书
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
# 验证自动续期任务是否已加入 cron/systemd:
systemctl list-timers | grep certbot
10. 小结与常用方法
- 每个独立域名对应一个独立的 VirtualHost 配置文件,便于后期维护和扩容。
-
始终使用
apachectl configtest检查语法,避免因小错误导致服务不可用。SLA 环境下建议开启日志轮转 防止日志文件过大影响性能。 - CERTBOT 自动续期确保 HTTPS 长期有效,不必手动更新证书。说起来,
-
{若使用多个子域。可考虑通过
ServerAlias *.yourdomain.com简化配置。} - {将网站根目录与代码库分离。通过软链接或 CI/CD 自动部署,提高运维效率。}
以上即为完整、结构化、并嵌入常见使用者痛点的 Ubuntu + Apache 虚拟主机多域名管理教程。祝您部署顺利、网站稳定运行!
常见痛点:在实际运营中,站长经常会遇到以下困扰:
- 配置文件语法错误导致 Apache 启动失败。
- 多个域名指向同一服务器时网站互相覆盖、访问不到预期站点。
- 文件权限不当导致 403 Forbidden 错误。
- DNS 解析延迟或未生效,使得新域名无法访问。老实说,
- 忘记启用/禁用站点后仍然使用默认站点。引发冲突,
1. 环境准备:安装 Apache 并确认服务运行
在开始之前,请确保你的 Ubuntu 程序已经安装了 Apache。按理说,如果尚未安装,请执行:
sudo apt update
sudo apt install -y apache2
sudo systemctl enable apache2
sudo systemctl start apache2
检查 Apache 是否正常运行:
sudo systemctl status apache2
2. 添加站点根目录并设置正确权限
以你的真实域名为例,在 /var/www 下创建站点目录:
# 创建目录
sudo mkdir -p /var/www/yourdomain.com
# 将所有权交给 Apache
sudo chown -R www-data:www-data /var/www/yourdomain.com
sudo chmod -R 755 /var/www/yourdomain.com
3. 编写虚拟主机配置文件
在 /etc/apache2/sites-available 目录下新建一个以域名命名的配置文件:
# 使用 nano 编辑
sudo nano /etc/apache2/sites-available/yourdomain.com.conf
将下面的模板粘贴进去。并根据实际情况替换占位符:
ServerAdmin
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/yourdomain.com
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
ErrorLog ${APACHE_LOG_DIR}/yourdomain.com.error.log
CustomLog ${APACHE_LOG_DIR}/yourdomain.com.access.log combined
注意事项
- 方法必须准确无误:DocumentRoot 与 Directory 必须保持一致,否则会出现 404 或 403 错误。
- ServerName 与 ServerAlias:确保把所有想要绑定的子域都写进来避免访问不到子域。
- ErrorLog 与 CustomLog:开启日志可以快速定位配置错误。
4. 启用站点并禁用默认站点
启用新建的虚拟主机:
# 启用站点
sudo a2ensite yourdomain.com.conf
# 禁用默认站点
sudo a2dissite 000-default.conf
# 检查配置语法是否正确
sudo apachectl configtest
If configtest returns “Syntax OK”,proceed;orwise fix reported line numbers.
5. 启用 CGI 支持
If your site needs CGI scripts,手动开启对应模块:
# 启用 CGI 模块及相关依赖
sudo a2enmod cgi
# 若使用 perl/c++ 等语言,还可能需要:
# sudo apt install libapache2-mod-fcgid
# 重启使模块生效
sudo systemctl restart apache2
6. 配置 DNS 解析
登录你的域名注册商控制面板。将以下记录指向服务器公网 IP:
-
A record:
@ → YOUR.SERVER.IP -
至于CNAME,
www → @
*注:DNS 生效可能需要数分钟到数小时请耐心等待。
7. 重启 Apache 并验证效果
# 重新启动。使所有改动生效
sudo systemctl restart apache8 || sudo systemctl restart apache2
# 检查端口是否监听
sudo ss -tulnp | grep :80
打开浏览器,访问。如果看到默认页面或你放置在根目录的 index 文件,即表示配置成功。
8. 常见故障排查步骤
-
"Failed to start Apache HTTP Server":
运行
apachectl configtest,检查是否有拼写错误或重复的 VirtualHost 定义。 - "403 Forbidden": 确认目录所有者是 www-data 且权限为 755; 检查 .htaccess 中是否有拒绝规则。
- "404 Not Found": 确认 DocumentRoot 指向正确方法,且 index.html 或 index.php 存在。
- "Domain not resolved": 确认 DNS A 记录已经正确填写且已传播;使用 `dig yourdomain.com` 验证 IP 是否返回正确。
9. 为多域名站点添加 SSL/TLS 加密
If you plan to serve traffic over HTTPS。use Certbot 自动申请 Let’s Encrypt 免费证书:
# 安装 Certbot 插件
sudo apt install -y certbot python3-certbot-apache
# 为该站点获取并自动配置证书
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
# 验证自动续期任务是否已加入 cron/systemd:
systemctl list-timers | grep certbot
10. 小结与常用方法
- 每个独立域名对应一个独立的 VirtualHost 配置文件,便于后期维护和扩容。
-
始终使用
apachectl configtest检查语法,避免因小错误导致服务不可用。SLA 环境下建议开启日志轮转 防止日志文件过大影响性能。 - CERTBOT 自动续期确保 HTTPS 长期有效,不必手动更新证书。说起来,
-
{若使用多个子域。可考虑通过
ServerAlias *.yourdomain.com简化配置。} - {将网站根目录与代码库分离。通过软链接或 CI/CD 自动部署,提高运维效率。}
以上即为完整、结构化、并嵌入常见使用者痛点的 Ubuntu + Apache 虚拟主机多域名管理教程。祝您部署顺利、网站稳定运行!

