0

Im pulling my hair out with Apache2 on Ubuntu 18.04.

I have a couple of domains that point to Vhosts, they seem to work but for some reasons when browsing to the IP directly, if just forwards to one of the vhosts and not the root of the /var/www/html directory.. What is going on? This works on Centos but not on Ubuntu.

    <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html

            <Directory /var/www/html/>
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
            </Directory>

            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined

            <IfModule mod_dir.c>
                DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
            </IfModule>
</VirtualHost>

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

1 Answers1

0

Do you have a 000-default.conf in sites enabled ? If not maybe create it and move the first virtual into it:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        <Directory /var/www/html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <IfModule mod_dir.c>
            DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
        </IfModule>

Now create a .conf for each of your other virtuals with ascending files like 10-this-domain and 20-that-domain

In your apache2.conf you should maybe have this line

IncludeOptional sites-enabled/*.conf

the directory sites-enabled holds all the hosts files for the virtuals

Works out of the box on my Ubuntu, I just drop new hosts into sites-enabled

Alec
  • 16
  • 1
  • Hi, Yes, the above i posted is the content of the 000-default.conf (the only one in the apache2's sites-enabled folder) – Graham Smart Nov 24 '19 at 17:50
  • I have too many site to have a separate file each. They are all just in one file. Its weird that it works flawless in centos, but ubuntu seems to pick a random vhost to give to the user if they browse by IP only. – Graham Smart Nov 24 '19 at 17:51
  • perhaps try moving the others out of the 000-default.conf into their own conf's ? with ascending order numbers. It might simplify things to diagnose it – Alec Nov 24 '19 at 17:51
  • see here https://serverfault.com/questions/288284/why-might-apache-ignore-a-virtual-host-with-a-servername-matching-the-requested – Alec Nov 24 '19 at 17:55
  • apache2ctl -S should tell you which site apache2 thinks is default, might give you a clue whats going on. – Alec Nov 24 '19 at 18:03
  • Thanks, I quickly threw a script in to recreate them all as new files. It seemed to work. So weird. I guess it doesnt read top down like it used to. – Graham Smart Nov 24 '19 at 18:07
  • Thank for helping, Would upvote if u could. Have selected your answer. – Graham Smart Nov 24 '19 at 18:08