Ubuntu下配置第二个Apache虚拟主机遇异常求助
Let's break down what's happening here and fix this step by step—this is a super common scenario when setting up multiple virtual hosts, so we'll cover all the likely culprits.
1. First: Fix DNS/Hosts File Misconfiguration
The ping command pointing to your first virtual host tells me there's a problem with how your second domain is being resolved. Here's what to check:
- Local hosts file: If you're testing locally (or even on a live server), make sure your hosts file has an entry for the second domain pointing to your server's IP (e.g.,
127.0.0.1 second-domain.comfor local testing).- On Linux/macOS: Edit
/etc/hostswithsudo nano /etc/hosts - On Windows: Edit
C:\Windows\System32\drivers\etc\hosts(run Notepad as admin)
- On Linux/macOS: Edit
- Live DNS records: If this is a public domain, log into your DNS provider and confirm the A/AAAA record for the second domain points to the correct server IP (same as the first virtual host, since they share the same server).
2. Verify Your Apache Virtual Host Setup
Most 404s with second virtual hosts come from misconfigured Apache configs. Let's validate this:
- Ensure virtual hosts are enabled: Check your main Apache config file (
httpd.confon Windows,apache2.confon Linux) to make sure it includes your virtual host files. Look for lines like:Include conf/extra/httpd-vhosts.conf # Windows Include sites-enabled/*.conf # Linux (Debian/Ubuntu) - Check the second virtual host config: Open your second host's config file (e.g.,
second-domain.confinsites-availableon Linux) and verify these critical settings:<VirtualHost *:80> ServerName second-domain.com # Must match the domain you're trying to access ServerAlias www.second-domain.com # Optional, but include if you use the www subdomain DocumentRoot "/path/to/your/second/site/files" # Double-check this path is correct! ErrorLog "${APACHE_LOG_DIR}/second-domain-error.log" CustomLog "${APACHE_LOG_DIR}/second-domain-access.log" combined <Directory "/path/to/your/second/site/files"> Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted # For Apache 2.4+; use "Allow from all" for older versions </Directory> </VirtualHost> - Enable the site (Linux only): If you're on Debian/Ubuntu, don't forget to enable the site with
sudo a2ensite second-domain.conf - Restart Apache: Always restart after changing configs!
sudo systemctl restart apache2 # Linux httpd -k restart # Windows (run Command Prompt as admin)
3. Check File Permissions & Existence
A 404 can also mean Apache can't access your second site's files:
- Confirm the DocumentRoot path exists: Run
ls /path/to/your/second/site/files(Linux) or check the folder in File Explorer (Windows) to make sure the directory and files are there. - Fix permissions (Linux): Apache runs as a specific user (usually
www-dataon Debian/Ubuntu,apacheon CentOS/RHEL). Set the correct ownership and permissions:sudo chown -R www-data:www-data /path/to/your/second/site/files sudo chmod -R 755 /path/to/your/second/site/files - Check default index files: Make sure your second site's root has a default index file like
index.htmlorindex.php. Apache'sDirectoryIndexsetting (inhttpd.conf) determines which files it looks for—common entries areindex.html index.php.
4. Clear Caches
Old cached data can cause weird behavior:
- Browser cache: Hit
Ctrl+Shift+Deleteto clear your browser's cache, cookies, and history. - DNS cache: Flush your local DNS to ensure your computer picks up the latest domain resolution:
- Windows:
ipconfig /flushdns(admin Command Prompt) - Linux:
sudo systemd-resolve --flush-caches - macOS:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
- Windows:
5. Test Apache Config for Errors
Before restarting, always check for syntax errors in your configs—this saves a lot of headaches:
sudo apache2ctl configtest # Linux httpd -t # Windows
If you see Syntax OK, you're good to go. If there's an error, follow the message to fix the issue (usually a typo in your config file).
内容的提问来源于stack exchange,提问作者Anirban Ghosh

