Ubuntu – Moving nextcloud into an apache VirtualHost block for use as a subdomain

apache-2.4nextcloudUbuntuubuntu-20.04

  • Nextcloud is currently being served at my-domain.dev/nextcloud.
  • I'd like to have it be served at cloud.my-domain.dev.
  • I've already set up an A record for cloud.my-domain.dev to point to my servers' IP address.
  • Sitting alongside my /var/www/nextcloud, I've also got a /var/www/my-domain.dev, my main site which I've set up to use HTTPS using Let's-Encrypt.
  • I'm using Server version: Apache/2.4.41 (Ubuntu).

Now I need to set up the vhosts to play together. I'm assuming I'll have to convert the nextcloud.conf to sit within a virtual host, maybe under a different port? How do I proceed?

nextcloud.conf as recommended by nextcloud.

Alias /nextcloud "/var/www/nextcloud/"

<Directory /var/www/nextcloud/>
  Require all granted
  AllowOverride All
  Options FollowSymLinks MultiViews

  <IfModule mod_dav.c>
    Dav off
  </IfModule>    

  SetEnv HOME /var/www/nextcloud
  SetEnv HTTP_HOME /var/www/nextcloud

</Directory>

my-domain.dev.conf

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

WHAT WORKED FOR ME:

Andrew Schulman's answer almost worked for me, I just had to make a few small changes:

  • Remove "Require all granted".
  • Move "AllowOverride All" and "Options FollowSymLinks MultiViews" into a directory tag.

Could someone explain why these steps were necessary?

Thanks Andrew!

nextcloud.conf:
  1 <VirtualHost *:80>
  2         ServerAdmin webmaster@b-t.dev
  3         ServerName cloud.b-t.dev
  4         DocumentRoot /var/www/nextcloud
  5
  6         #ErrorLog ${APACHE_LOG_DIR}/error.log
  7         #CustomLog ${APACHE_LOG_DIR}/access.log combined
  8
  9         <Directory "/var/www/nextcloud">
 10                 AllowOverride All
 11                 Options FollowSymLinks MultiViews
 12         </Directory>
 13
 14         #Satisfy Any
 15
 16         <IfModule mod_dav.c>
 17                 Dav off
 18         </IfModule>
 19
 20         SetEnv HOME /var/www/nextcloud
 21         SetEnv HTTP_HOME /var/www/nextcloud
 22 </VirtualHost>

Best Answer

You just need to add a new VirtualHost definition, in place of the Alias and Directory directives:

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  ServerName cloud.my-domain.dev
  DocumentRoot /var/www/nextcloud

  Require all granted
  AllowOverride All
  Options FollowSymLinks MultiViews

  <IfModule mod_dav.c>
    Dav off
  </IfModule>    

  SetEnv HOME /var/www/nextcloud
  SetEnv HTTP_HOME /var/www/nextcloud
</VirtualHost>

Both virtual hosts can run on port 80. That's how name-based virtual hosting works - it lets you run multiple hosts on the same IP address and port. Apache matches the value of the Host header that the client sends against the ServerName and ServerAlias directives, to determine which virtual host to serve them.

You should read the Apache Virtual Host documentation, especially the part about name-based virtual hosts.

Related Topic