如何将Amazon EC2实例公网DNS绑定至自定义域名部署静态网站?
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.compointing 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 towww.mydomain.comif 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.conffile (typically at/etc/httpd/conf/httpd.conffor Amazon Linux/RHEL, or/etc/apache2/apache2.conffor 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:
You should see the content from yourcurl http://localhostmywebsitedirectory. - Once DNS has propagated, test from your local machine by visiting
http://www.mydomain.comin a browser. If it’s not working immediately, clear your browser cache or runnslookup www.mydomain.comto 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 inhttpd.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_logor/var/log/apache2/error.log) for specific issues
内容的提问来源于stack exchange,提问作者ScalaBoy

