ivailoy
By:
ivailoy

How to set the default virtual host on Apache 2?

April 26, 2016 10k views
Apache WordPress Ubuntu

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.

2 comments
  • 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

3 Answers

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
  • How do I control it?

    Do I have to setup a default config? If so what should that look like?

    If not how do I force it to use the one I want?

    Thanks.

    • For the one you want as default, run this, changing DEFAULT_TO_USE to the one you want to use:

      Remember, if the file has ".conf" at the end, you need to also add that the the "DEFAULT_TO_USE" at the top of my script

      ### Modify the next line, and run. It won't return anything. That's to be expected.
      DEFAULT_TO_USE='site-a.com'
      
      ### To make sure it set itself properly, you can run this:
      echo $DEFAULT_TO_USE
      

      Now that you've done that, just copy and paste the next bit:

      a2dissite $DEFAULT_TO_USE
      mv /etc/apache2/sites-available/${DEFAULT_TO_USE} /etc/apache2/00000-${DEFAULT_TO_USE}
      a2ensite 00000-${DEFAULT_TO_USE}
      service apache2 restart
      
      • To put this another way wouldn't it be easier to just go to /sites-enabled

        cp site-a-com.conf 000-default.conf

        Then disable site-a, then enable 000-default?

        That's fine I was just thinking maybe there was something to put in the conf file itself to set the default.

        Thanks.

"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

Have another answer? Share your knowledge.