Apache has some directories from which it automatically loads configurations, and some where it doesn’t and where the files in there are just stored waiting enabled. To illustrate this, take a look at the directory structure beneath /etc/apache2/
- (also mods-avalable and mods-enabled, but these aren’t relevant in this context)
*-enabled directories only contain links to the actual files which are stored in
*-available. You’ve probably seen commands like
sudo a2ensite xxx and
sudo a2enconf xxx before; these commands are just a convenient way of making a link from a file in the
*-enabled. Then, upon reloading / restarting apache2, it loads configuration from these enabled files.
conf-available directory is where you’d want to store some specific configuration for your server, like how much information the server should display about itself, should you request an invalid URL. The
sites-available directory is where you’d store your personal sites like the one you’re trying to add. The files there typically contain one
<VirtualHost ...> ... </VirtualHost> element each, where you declare configuration about your site; they may, however, also contain more virtual hosts.
To emhpasize, configuring your server with sites and other configurations can essentially be done in a single file, but the layout / directory structure employed by apache2 makes it modular and thus easier to maintain and control.
Now on to your problem, which I will address by showing my setup (not actual names used). I have the redirect site placed in a file of its own called
sites-available, containing a minor variant of the 4 liner redirect example you posted (HTTP port 80). Then I have another site in a file called
my-site.conf where I have the virtual host declaring the nextcloud configuration on the HTTPS port
<VirtualHost *:443> ...</VirtualHost>. Both files have been enabled by
sudo a2ensite http-redirect and
sudo a2ensite my-site followed by a restart / reload of apache2.
From your example, I can spot some oddities. For example the
ServerName directive; it should be the domain name where your nextcloud instance can be found - like
cloud.example.com. Apache uses these
ServerName directives internally to match incoming requests to the correct sites (you could have a site from where nextcloud could be served, described in its own file -
cloud.example.com - and a site where a static web page could be served, likewise described in its own file - with a
www.example.com. Upon receiving a request, Apache maps the requested URL to the correct site based on the
Keep in mind your SSL certificate should also reflect this domain name you choose. In your example, you have chosen an IP address for a domain name. I think it can work, but I would probably recommend you find a proper domain name, as IPs can change, but domain names are static and may point to any IP address you like.
Sorry for the long post, dunno if I explained a bit too much