大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

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…

问题原文

原帖链接 · Karoh · CC BY-SA 4.0

  • 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 [email omitted]
  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>

已采纳解答

原帖链接 · Andrew Schulman · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-12-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。