You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同一主域名不同TLD的Apache VirtualHosts配置方法咨询

Can I host example.org in the same .conf file as example.com?

Great question—let’s unpack this for you, since there’s no strict "one size fits all" answer, but there are best practices to consider based on your use case.

Absolutely—Apache fully supports defining multiple <VirtualHost> blocks within a single configuration file, so this is a valid approach. That said, whether it’s the best approach depends on how closely related your two sites are.

Option 1: Single .conf file (valid, good for shared/mirror sites)

If example.com and example.org are mirror sites (same content) or share most of their configuration (like log paths, security rules), putting them in the same file can reduce duplication. Here’s what that might look like in your existing example.conf:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
    # Any shared config (like <Directory> rules) goes here too
</VirtualHost>

<VirtualHost *:80>
    ServerName example.org
    ServerAlias www.example.org
    DocumentRoot /var/www/example.org/public_html
    # If using the same logs or root, you can reuse those lines instead
    ErrorLog ${APACHE_LOG_DIR}/example.org-error.log
    CustomLog ${APACHE_LOG_DIR}/example.org-access.log combined
</VirtualHost>

Just make sure to run sudo apache2ctl configtest to catch any syntax errors before restarting Apache with sudo systemctl restart apache2.

Ubuntu’s Apache setup is designed around one .conf file per site (or logical group of sites, like a domain and its www subdomain) because it makes management cleaner:

  • You can enable/disable each site independently with sudo a2ensite example.org.conf or sudo a2dissite example.org.conf
  • Troubleshooting is easier—if one site breaks, you know exactly which file to check
  • It’s more scalable if you add more sites later

To do this, create /etc/apache2/sites-available/example.org.conf with its own <VirtualHost> block, enable it with a2ensite, then restart Apache.

Which should you choose?

  • Go with a single file if the two sites are tightly linked (same content, shared config)
  • Use separate files if the sites are distinct (different content, unique settings) or if you want to follow Ubuntu’s default organizational pattern for easier long-term management

内容的提问来源于stack exchange,提问作者Mohd Abdul Mujib

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:53:10