Search code examples
apache2certbotdebian-buster

Certbot not working correctly for subdomain


I have a VPS server with SSL Certbot, in Debian with apache, but not working correctly for subdomains. When accessing my subdomain with http://, it goes directly to the directory that I configure.

But, when accessing with https:// protocol, it goes directly to the root directory, that is /var/www/html.

I am new to the VPS servers, and I not know what's the problem.

This is my /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.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # 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
        RewriteEngine on
        RewriteCond %{SERVER_NAME} =app.mysite.com [OR]
        RewriteCond %{SERVER_NAME} =mysite.com [OR]
        RewriteCond %{SERVER_NAME} =www.mysite.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

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

<VirtualHost *:80>
   ServerName app.mysite.com
   DocumentRoot /var/www/html/app
</VirtualHost>

Thanks for your time


Solution

  • Your configuration sample is missing the configuration for https / port 443.

    From your http configuration

    <VirtualHost *:80>
       ServerName app.mysite.com
       DocumentRoot /var/www/html/app
    </VirtualHost>
    

    So you should change the DocumentRoot of your SSL-configuration file also to DocumentRoot /var/www/html/app. Probably this file is called 000-default-ssl.conf.

    Usually it is a good idea to create is own files and use them instead of the default ones. Especially when you do a dist upgrade and configuration files change this is much less hassle, because when changing default files you get a dialog and have to decide which version you want to keep. Selecting the wrong and you have to restore your changed config-file from a backup.