简体   繁体   中英

Second Apache virtual host not loading

I have Apache installed on a Ubuntu 20.04 server and it was working fine. And then I started to mess around with virtual hosts because I wanted to add a subdomain.

My goal is to have two separate projects accessible on two separate URLs:

  • Main project - example.com
  • Sub project - subdomain.example.com

On my file server I have these file structure:

- /var/www/html
  - index.php
- /var/www/subdomain.example.com
  - index.php

On /etc/apache2/sites-available/example.conf I have the following:

<VirtualHost *:80>
    ServerAdmin email@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin email@example.com
    ServerName subdomain.example.com
    ServerAlias www.subdomain.example.com
    DocumentRoot /var/www/subdomain.example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I ran the following commands after saving the file:

# Enable the new Apache configuration
sudo a2ensite example.conf

# Disable the default Apache configuration
sudo a2dissite 000-default.conf

# Activate the new Apache configuration
sudo systemctl reload apache2

# Restart Apache server
sudo systemctl restart apache2

Once that's all done, I go visit http://example.com and it works fine. But when I visit http://subdomain.example.com I get the following error in Chrome:

subdomain.example.com's server IP address could not be found.

I went ahead and edited /etc/hosts and have the following:

127.0.0.1 localhost
127.0.0.1 example.com
127.0.0.1 subdomain.example.com

I'm not sure if I need to do anything after modifying /etc/hosts but no changes even after I saved the new hosts file.

How do I fix my virtual hosts so that I end up with the goal I mentioned at the beginning of this question?

I figured out that the issue isn't with how Apache is configured.

On the Cloud DNS from the Google Cloud Platform dashboard I had to add two record sets:

  • A record subdomain.example.com with the correct IP address
  • CNAME record www.subdomain.example.com

And now it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM