Question

How to set the default virtual host on Apache 2?

I have the WordPress one click install app image and I need to fix some problems with the default server.

I have two sites enabled. Let’s call them site-a and site-b with the requisite config files at /etc/apache2/sites-available/site-a.conf and site-b.conf. Both sites are enabled so symlinks to these files appear at /etc/apache2/sites-enabled/

Both sites are running as they should except for one big problem - apache is closing one of the two sites to be the “default” site at random and it’s choosing exactly the one I don’t want.

Nothing I’ve tried is fixing this. I thought that I just needed to put in the top line of the site I want to be the default this: " <VirtualHost default:80>" but it isn’t working.

What do I have to do here?

Here are my two config files, nothing special.

Site A:

<VirtualHost *:80>
	ServerName site-a.com
        ServerAlias site-a.com www.site-a.com
        ServerAdmin webmaster@site-a.com
        DocumentRoot /var/www/html/site-a.com

        <Directory /var/www/html/site-a.com>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
        
        ErrorLog ${APACHE_LOG_DIR}/site-a_error.log
        CustomLog ${APACHE_LOG_DIR}/site-a_access.log combined
</VirtualHost>

Site B:

<VirtualHost *:80>
	ServerName secondary.site-a.com
        ServerAdmin webmaster@secondary.site-a.com
        DocumentRoot /var/www/html/site-b.com

        <Directory /var/www/html/site-b.com>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
        
        ErrorLog ${APACHE_LOG_DIR}/site-b_error.log
        CustomLog ${APACHE_LOG_DIR}/site-b_access.log combined
</VirtualHost>

Now all I want is for the server to serve up Site A when in doubt. I only want it to serve up Site B when somebody enters exactly http://secondary.site-a.com or http://secondary.site-a.com/some/url/at/subodain/secondary

The problem is it’s doing the opposite. How do I fix this?

Thanks.

Subscribe
Share

HI,

Do you have a file named secondary.site-a.com in apache2/sitesenabled ? Also when you say “when in doubt” do you mean you would like your server site-a.com to be redirected to as long as someone chooses anything.site-a.com other than secondary.site-a.com?

“Do you have a file named secondary.site-a.com in apache2/sitesenabled ?”

Yes.

“Also when you say “when in doubt” do you mean you would like your server site-a.com to be redirected to as long as someone chooses anything.site-a.com other than secondary.site-a.com?”

I mean when somebody visits anything besides http://secondary.site-a.com or http://secondary.site-a.com/some/url/of/this/subdomain I do NOT want that site to be displayed for any reason. This includes visiting the IP address http://123.123.123.123


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

It treats the config files in a top-down approach. It will default to the first vhost based upon alpahbet (if no numbers are present), or the lowest number config file.

For example:

000-default.conf
test.com.conf
site-a.com.conf
site-b.com.conf

In the above, 000-default will be the default, because it goes “numbers, then letters”.

To see which one is being used:

apache2ctl -S

It treats the config files in a top-down approach. It will default to the first vhost based upon alpahbet (if no numbers are present), or the lowest number config file.

For example:

000-default.conf
test.com.conf
site-a.com.conf
site-b.com.conf

In the above, 000-default will be the default, because it goes “numbers, then letters”.

To see which one is being used:

apache2ctl -S

It treats the config files in a top-down approach. It will default to the first vhost based upon alpahbet (if no numbers are present), or the lowest number config file.

For example:

000-default.conf
test.com.conf
site-a.com.conf
site-b.com.conf

In the above, 000-default will be the default, because it goes “numbers, then letters”.

To see which one is being used:

apache2ctl -S

This comment has been deleted

“Do you have a file named secondary.site-a.com in apache2/sitesenabled ?”

Yes.

“Also when you say “when in doubt” do you mean you would like your server site-a.com to be redirected to as long as someone chooses anything.site-a.com other than secondary.site-a.com?”

I mean when somebody visits anything besides http://secondary.site-a.com or http://secondary.site-a.com/some/url/of/this/subdomain I do NOT want that site to be displayed for any reason. This includes visiting the IP address http://123.123.123.123