How to run multiple subdomain under same IP in apache2

I am trying to run dev1.domain.com and dev2.domain.com under the same IP address. My virtualhost configs are correct. What is happening is when I try to go to dev2.domain.com, it automatically redirected to dev1.domain.com. I ran the following command apachectl -t -D DUMP_VHOSTS which shows me the following output

*:80                   is a NameVirtualHost
         default server dev1.domain.com (/etc/apache2/sites-enabled/conv.conf:1)
         port 80 namevhost dev1.domain.com (/etc/apache2/sites-enabled/conv.conf:1)
                 alias www.dev1.domain.com
         port 80 namevhost dev2.domain.com (/etc/apache2/sites-enabled/find.conf:1)
                 alias www.dev2.domain.com

As You can see apache2 is taking dev1.domain.com as default server. How should I avoid this scenario?

For simplicities sake, I am going to add the virtual host config below

For dev.domain.com

    <VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName dev1.domain.com
    ServerAlias www.dev1.domain.com
    DocumentRoot /var/www/folder/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /var/www/folder/>
         AllowOverride All
    </Directory>
</VirtualHost>

For dev2.domain.com

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName dev2.domain.com
    ServerAlias www.dev2.domain.com
    DocumentRoot /var/www/another_folder/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /var/www/finder/>
         AllowOverride All
    </Directory>
</VirtualHost>

Thanks in advance.


Solution 1:

The default vhost is the vhost served when no other vhost matches the domain in the configuration.

Unless otherwise configured, the default vhost is the first vhost configured.

If your default vhost is being served then the domain you are trying to connect to does not match any vhost that is configured.

Files are read into the apache config using an include statement which read them in lexical order.

In the vhost configs you show, you don't have a dev1 which doesn't really help - details matter.