Ubuntu 16.04 LTS下Apache多域名无法访问,提示ERR_CONNECTION_TIMED_OUT求助
Hey there! Let's work through your two main issues—domain timeouts and only seeing one site internally—step by step.
1. Fixing ERR_CONNECTION_TIMED_OUT for Your Domains
This usually boils down to dynamic IP sync issues, firewall blocks, or ISP restrictions. Here's what to check:
Verify Namecheap Dynamic DNS is Updating Your IP
Since you're using a dynamic public IP, Namecheap needs real-time updates when your IP changes. Set up a simple auto-update script on Ubuntu to keep things in sync:# Create a script (replace placeholders with your domain and Namecheap DDNS password) sudo nano /usr/local/bin/update-namecheap-dns.shPaste this into the script:
#!/bin/bash DOMAIN="yourdomain.com" PASSWORD="your_ddns_password_from_namecheap" curl "https://dynamicdns.park-your-domain.com/update?host=@&domain=$DOMAIN&password=$PASSWORD"Make it executable:
sudo chmod +x /usr/local/bin/update-namecheap-dns.shAdd a cron job to run it every 15 minutes:
crontab -eAdd this line at the bottom:
*/15 * * * * /usr/local/bin/update-namecheap-dns.shSave and exit—this ensures your Namecheap records always match your current public IP.
Check Firewalls (Ubuntu & Router)
- On Ubuntu, confirm UFW allows port 80:
If "80/tcp" isn't listed as allowed, run:sudo ufw statussudo ufw allow 80/tcp - Double-check your router's port forwarding rule for 80 →
10.0.0.111—make sure it's enabled and targeting the correct internal IP. Some ISPs block port 80 by default: test this by temporarily forwarding port 8080 to your Ubuntu machine and accessingyourdomain.com:8080from your phone's 4G network. If that works, your ISP is blocking 80, and you'll need to use an alternative port or request unblocking.
- On Ubuntu, confirm UFW allows port 80:
Confirm Public IP Accessibility
On your Ubuntu machine, runcurl ifconfig.meto get your current public IP. Visit that IP directly from your phone's 4G (e.g.,http://123.45.67.89). If this times out too, the issue is with your router's forwarding or ISP restrictions, not Apache.
2. Making All Sites Accessible Internally
Right now, Apache is serving its default site when you visit 10.0.0.111 because you haven't set up virtual hosts to map each domain to its own content. Here's how to fix that:
Create Site Directories & Set Permissions
For each domain, make a root directory for its files:# Replace with your actual domains sudo mkdir -p /var/www/yourfirstdomain.com/public_html sudo mkdir -p /var/www/yourseconddomain.com/public_html sudo mkdir -p /var/www/yourthirddomain.com/public_htmlSet ownership so you can edit files without sudo:
sudo chown -R $USER:$USER /var/www/yourfirstdomain.com/public_html sudo chown -R $USER:$USER /var/www/yourseconddomain.com/public_html sudo chown -R $USER:$USER /var/www/yourthirddomain.com/public_html sudo chmod -R 755 /var/wwwAdd unique
index.htmlfiles to eachpublic_htmlfolder so you can distinguish the sites.Set Up Apache Virtual Hosts
Copy the default config as a template for each domain:sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/yourfirstdomain.com.conf sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/yourseconddomain.com.conf sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/yourthirddomain.com.confEdit each config file (replace with your domain):
sudo nano /etc/apache2/sites-available/yourfirstdomain.com.confModify it to look like this:
<VirtualHost *:80> ServerAdmin webmaster@yourfirstdomain.com ServerName yourfirstdomain.com ServerAlias www.yourfirstdomain.com DocumentRoot /var/www/yourfirstdomain.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>Repeat this for your other two domains.
Enable the Sites & Restart Apache
Enable each virtual host config:sudo a2ensite yourfirstdomain.com.conf sudo a2ensite yourseconddomain.com.conf sudo a2ensite yourthirddomain.com.confDisable the default site to avoid conflicts (optional but recommended):
sudo a2dissite 000-default.confRestart Apache to apply changes:
sudo systemctl restart apache2Test Internally with Hosts File
To test domains from your Windows 10 machine, map them to the Ubuntu internal IP via thehostsfile:- Open Notepad as Administrator.
- Open
C:\Windows\System32\drivers\etc\hosts. - Add these lines at the bottom (replace with your domains):
10.0.0.111 yourfirstdomain.com 10.0.0.111 yourseconddomain.com 10.0.0.111 yourthirddomain.com - Save the file. Now visiting each domain in your Windows browser will load the corresponding site.
内容的提问来源于stack exchange,提问作者Michael H Graf Sr.

