同一主域名不同TLD的Apache VirtualHosts配置方法咨询
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.
Option 2: Separate .conf files (Ubuntu’s recommended default)
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.conforsudo 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

