求助:如何在Amazon AWS Cloud9环境中创建可访问的子域名
Hey there! I get that setting up a subdomain in Cloud9 feels a bit different from EC2—let's break this down simply, since Cloud9's domain is managed directly by AWS and we can't adjust its DNS records like we would with a regular EC2 instance. Here's how to make your subdomain work:
Step 1: Understand the Cloud9 Domain Quirk
Unlike EC2 where you can control DNS via Route53 or your own registrar, Cloud9's assigned domain (like xxxxxxxxxxxxxxxx.vfs.cloud9.eu-west-1.amazonaws.com) is locked down. We can't create actual DNS subdomains for it, but we can use Apache's virtual hosts to route requests based on the Host header in the HTTP request. That's how we'll mimic a subdomain setup.
Step 2: Create a New Virtual Host Configuration
First, let's set up a separate virtual host for your subdomain:
- Create a directory for your subdomain's content (replace
subdomainwith your desired name):sudo mkdir /var/www/subdomain sudo chown -R $USER:$USER /var/www/subdomain - Add a test HTML file to verify later:
echo "<h1>Hello from subdomain!</h1>" > /var/www/subdomain/index.html - Create a new Apache site configuration file. Let's call it
subdomain.conf:sudo nano /etc/apache2/sites-available/subdomain.conf - Paste this configuration into the file, replacing the
ServerNamewith your full subdomain URL:<VirtualHost *:8080> ServerAdmin webmaster@localhost ServerName subdomain.xxxxxxxxxxxxxxxx.vfs.cloud9.eu-west-1.amazonaws.com DocumentRoot /var/www/subdomain ErrorLog ${APACHE_LOG_DIR}/subdomain-error.log CustomLog ${APACHE_LOG_DIR}/subdomain-access.log combined </VirtualHost>
Step 3: Enable the New Site and Restart Apache
- Enable the new virtual host:
sudo a2ensite subdomain.conf - Restart Apache to apply changes:
sudo systemctl restart apache2
Step 4: Verify Your Setup
Now try accessing subdomain.xxxxxxxxxxxxxxxx.vfs.cloud9.eu-west-1.amazonaws.com in your browser. You should see the test "Hello from subdomain!" message.
Why the EC2 Tutorial Didn't Work
EC2 tutorials usually focus on configuring DNS records (like in Route53) to point a subdomain to your instance's IP. But Cloud9 doesn't let you modify its domain's DNS—instead, we rely on Apache to look at the incoming request's Host header and route it to the correct directory. That's the key difference here!
内容的提问来源于stack exchange,提问作者Fifi

