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

Ubuntu下配置第二个Apache虚拟主机遇异常求助

Troubleshooting Apache Dual Virtual Host Issues

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.com for local testing).
    • On Linux/macOS: Edit /etc/hosts with sudo nano /etc/hosts
    • On Windows: Edit C:\Windows\System32\drivers\etc\hosts (run Notepad as admin)
  • 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.conf on Windows, apache2.conf on 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.conf in sites-available on 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-data on Debian/Ubuntu, apache on 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.html or index.php. Apache's DirectoryIndex setting (in httpd.conf) determines which files it looks for—common entries are index.html index.php.

4. Clear Caches

Old cached data can cause weird behavior:

  • Browser cache: Hit Ctrl+Shift+Delete to 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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:46:10