Apache configuration for main host and virtual host

I had configured main host (doc root /var/www/html) and virtual host (doc root /var/www/html/vhosts ), but when I access main host(server2.example.com) it always gets forwarded to virtual host (Main host goes away). Below, I provided httpd.conf, please help me.

ServerAdmin root@localhost
ServerName server2.example.com:80
UseCanonicalName Off
DocumentRoot "/var/www/html"
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

NameVirtualHost *:80
<VirtualHost *:80>
    DocumentRoot /var/www/html/web/test1.example.com
    ServerName test1.example.com
    ServerAdmin [email protected]
    ErrorLog logs/test1.example.com-error_log
    CUstomLog logs/test1.example.com-access_log common
<Directory "/var/www/html/web/test1.example.com">
    order deny,allow
    deny from all
    allow from 192.168.122.1 
</Directory>
</VirtualHost>

Thanks in advance.


Apache serves the first vhost that matches the Host: header or the first vhost defined (if no vhosts match the Host: header) so, when using name based virtual hosting, make both hosts virtualhosts and put the one you want to be the default host first.

This should serve server2.example.com for anything that's not test1.example.com.

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin root@localhost
    ServerName server2.example.com:80
    UseCanonicalName Off
    DocumentRoot "/var/www/html"
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

    <Directory "/var/www/html">
        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
</virtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/html/web/test1.example.com
    ServerName test1.example.com
    ServerAdmin [email protected]
    ErrorLog logs/test1.example.com-error_log
    CUstomLog logs/test1.example.com-access_log common
    <Directory "/var/www/html/web/test1.example.com">
        order deny,allow
        deny from all
        allow from 192.168.122.1 
    </Directory>
</VirtualHost>