0

I'm configuring a new server (droplet) with Ubuntu 18.04.1 LTS. I installed apache and configured virtual hosts. The server name is www.speedysoftware.com and this is the value of /etc/hostname. The problem is, I'm trying to redirect http://www.speedysoftware.com/ to a specific virtual host, but it's redirected to the default virtual host. On the other hand, http://www.speedysoft.com/ and http://www.speedy-software.com/ are redirected to the correct virtual host, which should be the same as http://www.speedysoftware.com/

File /etc/apache2/sites-available/000-default.conf:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerAdmin webmaster@speedy.net
    DocumentRoot /var/www/general

    <Directory /var/www/general>
        Options +FollowSymLinks
        Options -Indexes
        AllowOverride All
        Require all granted
    </Directory>

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

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

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

File /etc/apache2/sites-available/speedysoftware.conf:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName www.speedysoftware.com
    ServerAlias speedysoftware.com *.speedysoftware.com
    ServerAlias speedy-software.com *.speedy-software.com
    ServerAlias speedysoft.com *.speedysoft.com
    # ... (more ServerAliases)

    ServerAdmin webmaster@speedy.net
    DocumentRoot /var/www/speedysoftware

    <Directory /var/www/speedysoftware>
        Options +FollowSymLinks
        Options -Indexes
        AllowOverride All
        Require all granted
    </Directory>

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

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

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

You can see that http://www.speedy-soft.com/ is redirected to the default virtual host, and this is correct. What is the problem with the configuration?

After I made changes, I reloaded the settings by sudo systemctl restart apache2.

Regarding redirecting, in this case I don't want to redirect. I want them to be considered separate websites although they lead to the same virtual host. In other cases I do redirect, for example I redirect http://www.speedypedia.org/ to http://www.speedypedia.info/, and I redirect http://ww.speedysoftware.com/ (or any other subdomain, including http://speedysoftware.com/) to http://www.speedysoftware.com/.

Update - I checked and http://ww.speedysoftware.com/, http://wwww.speedysoftware.com/ and http://speedysoftware.com/ all lead to the correct virtual host. Only http://www.speedysoftware.com/ doesn't lead to the correct virtual host.

0

I wrote to DigitalOcean Support (they have a great support, by the way) and they wrote me:

The server is Apache, so you have access to the apachectl command which supports the -S flag to get the parsed settings dumped which includes the vhosts and configs where they are established.

I ran apachectl -S from the command line and found out the problem:

*:80                   is a NameVirtualHost
         default server www.speedysoftware.com (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost www.speedysoftware.com (/etc/apache2/sites-enabled/000-default.conf:1)
         ...

Since I didn't define ServerName in /etc/apache2/sites-available/000-default.conf, the droplet name was taken, and this was the problem. So I added ServerName www.speedy-soft.com to this file:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName www.speedy-soft.com

    ServerAdmin webmaster@speedy.net
    DocumentRoot /var/www/general

    <Directory /var/www/general>
        Options +FollowSymLinks
        Options -Indexes
        AllowOverride All
        Require all granted
    </Directory>

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

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

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

And restarted Apache. I ran apachectl -S again:

*:80                   is a NameVirtualHost
         default server www.speedy-soft.com (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost www.speedy-soft.com (/etc/apache2/sites-enabled/000-default.conf:1)
         ...

And now everything is working.

You can see more details on this page: https://stackoverflow.com/questions/5474477/how-to-debug-an-apache-virtual-host-configuration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.