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

如何将Amazon EC2实例公网DNS绑定至自定义域名部署静态网站?

How to Bind Your Custom Domain to Amazon EC2 and Serve Your Website Directory

Alright, let's get your setup working properly—you’ve already made a great start by updating your httpd.conf file. Here’s the step-by-step breakdown to tie everything together so visitors to www.mydomain.com land on your /var/www/html/mywebsite content:

1. Lock Down Your EC2 Instance's Public IP (Critical!)

EC2 instances come with a temporary public IP/DNS that changes when you stop or restart the instance. To avoid breaking your domain link later, assign an Elastic IP to your EC2 instance:

  • Open the AWS EC2 Console, navigate to "Elastic IPs" under the Network & Security section
  • Allocate a new Elastic IP (or use an existing one you already have)
  • Associate it with your running EC2 instance
    This ensures your instance has a fixed public IP that won’t shift unexpectedly.

2. Update Your Domain's DNS Records

Head to your domain registrar’s dashboard (where you purchased www.mydomain.com) and adjust the DNS settings:

  • Create an A Record for www.mydomain.com pointing directly to your EC2 instance’s Elastic IP (or current public IP if you skip the Elastic IP step—just remember this will break if you restart the instance)
  • Optional: Add a CNAME record for the root domain (mydomain.com) pointing to www.mydomain.com if you want both the root and subdomain to work
    Wait for DNS propagation—this can take anywhere from a few minutes to 48 hours, though it’s usually fast for most registrars.

3. Verify Your Apache Configuration

You already updated DocumentRoot and ServerName, but let’s double-check to avoid common missteps:

  • Open your httpd.conf file (typically at /etc/httpd/conf/httpd.conf for Amazon Linux/RHEL, or /etc/apache2/apache2.conf for Ubuntu/Debian)
    Confirm these lines are set correctly:
    DocumentRoot "/var/www/html/mywebsite"
    ServerName www.mydomain.com:80
    
  • Ensure the <Directory> block for your DocumentRoot has proper access rules:
    <Directory "/var/www/html/mywebsite">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
  • Restart Apache to apply changes:
    # For Amazon Linux/RHEL/CentOS
    sudo systemctl restart httpd
    # For Ubuntu/Debian
    sudo systemctl restart apache2
    

4. Fix File Permissions (Common Pitfall!)

Apache runs as a specific system user (usually apache for RHEL-based distros, www-data for Debian/Ubuntu), so make sure your website files are accessible:

  • Set the correct ownership:
    # Amazon Linux/RHEL/CentOS
    sudo chown -R apache:apache /var/www/html/mywebsite
    # Ubuntu/Debian
    sudo chown -R www-data:www-data /var/www/html/mywebsite
    
  • Set proper file/directory permissions:
    sudo chmod -R 755 /var/www/html/mywebsite
    sudo chmod 644 /var/www/html/mywebsite/*.html # Adjust for other file types like .css/.js as needed
    

5. Test the Setup

  • First, test locally on the EC2 instance to confirm Apache is serving the right content:
    curl http://localhost
    
    You should see the content from your mywebsite directory.
  • Once DNS has propagated, test from your local machine by visiting http://www.mydomain.com in a browser. If it’s not working immediately, clear your browser cache or run nslookup www.mydomain.com to confirm the DNS is pointing to your EC2 IP.

Troubleshooting Tips

  • If you get a "Forbidden" error: Double-check file permissions and the <Directory> block in httpd.conf
  • If the domain doesn’t resolve: Wait longer for DNS propagation, or verify the A record is pointing to the correct IP
  • If Apache won’t restart: Check the error log (/var/log/httpd/error_log or /var/log/apache2/error.log) for specific issues

内容的提问来源于stack exchange,提问作者ScalaBoy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:18:50