如何在 Ubuntu 20.04 上将 Apache 网站根目录移动到新位置

介绍

在Ubuntu上,Apache Web 服务器默认地为存储在var/www/html目录中的文档提供服务.这个目录被称为 document root. 当管理员或用户向服务器提出请求时,它将从文档根中响应相应的文件。

这个目录通常位于 root 文件系统上与操作系统的其余部分一起,但有时将文档 root 移动到另一个位置有帮助,例如单独安装的文件系统,例如,如果您从相同的 Apache 实例中服务多个网站,将每个网站的文档 root 放置在其自己的体积上,可让您根据特定网站或客户端的需求进行扩展。

在本指南中,您将将一个Apache文档根从其默认位置var/www/html移动到一个新的位置。

前提条件

要完成本指南,您将需要:

您可以了解更多关于如何设置具有这些特权的用户在我们的 初始服务器设置与Ubuntu 20.04 指南。

<$>[info] 如果您正在使用DigitalOcean的区块存储量, 本指南将向您展示如何创建和粘贴您的音量。 您的新文档根位置可以根据您的需求进行配置。 如果您正在将文档根移动到不同的存储设备,则您将希望在设备的安装点下选择一个位置。

步骤 1 – 将文件复制到新位置

如果你遵循前提教程,你将创建一个新的文档根在 /var/www/your_domain. 你也可能有额外的文档根在相应的 VirtualHost 指令。

通过使用grep命令搜索/etc/apache2/sites-enabled目录,查找文档根的位置,从而将焦点限制在活跃的网站上。

1grep -R "DocumentRoot" /etc/apache2/sites-enabled

下面的输出显示了当前DocumentRoot的位置:

1[secondary_label Output]
2/etc/apache2/sites-enabled/your_domain-le-ssl.conf:  DocumentRoot /var/www/your_domain
3/etc/apache2/sites-enabled/your_domain.conf:         DocumentRoot /var/www/your_domain

如果您有先前存在的设置,您的结果可能与这里所示不同. 在任何情况下,您可以使用grep的输出来确保您正在移动所需的文件并更新相应的配置文件。

现在你已经确认了你的文档根的位置,将文件复制到新的位置,用rsync:

<$>[注] 注: 使用 rsync 命令时,请注意两件事:

  • 請注意,在「your_domain」之後會出現「後續縮小」。 如果您不在此列出後續縮小,您正在將此目錄複製至新文件根作為子目錄。例如,您的新文件根將具有這樣的結構: /var/www/mnt/volume_nyc3_01/your_domain. 在試圖從新文件根中服務您的 index.html 檔案時會出現問題。
  • 其次,新文件根目錄上會有 no後續縮小。

美元

1sudo rsync -av /var/www/your_domain/ /mnt/volume_nyc3_01

-a 旗保留了权限和其他目录属性,而 -v 旗提供了无语音的输出,因此您可以跟踪同步的进展。

你的输出应该包括以下内容:

1[secondary_label Output]
2sending incremental file list
3./
4index.html
5
6sent 265 bytes received 38 bytes 606.00 bytes/sec
7total size is 134 speedup is 0.44

有了您的文件,您可以继续修改您的Apache配置以反映这些更改。

步骤 2 – 更新配置文件

查找并将文件复制到新文档根后,您可以配置虚拟主机文件以指向该新位置。

开始使用您偏好的编辑器打开 `/etc/apache2/sites-enabled/your_domain.conf。

1sudo nano /etc/apache2/sites-enabled/your_domain.conf

在此示例中,新的根位置是 /mnt/volume_nyc3_01:

 1[label /etc/apache2/sites-enabled/your_domain.conf]
 2<VirtualHost *:80>
 3    ServerAdmin sammy@your_domain
 4    ServerName your_domain
 5    ServerAlias www.your_domain
 6    DocumentRoot /mnt/volume_nyc3_01
 7    ErrorLog ${APACHE_LOG_DIR}/error.log
 8    CustomLog ${APACHE_LOG_DIR}/access.log combined
 9RewriteEngine on
10RewriteCond %{SERVER_NAME} =www.your_domain [OR]
11RewriteCond %{SERVER_NAME} =your_domain
12RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
13</VirtualHost>

在此<VirtualHost>指令之后,添加这些突出的行,以确保服务器在目录中遵循符号链接:

1[label /etc/apache2/sites-enabled/your_domain.conf]
2. . .
3</VirtualHost>
4
5<Directory /mnt/volume_nyc3_01>
6    Options FollowSymLinks
7    AllowOverride None
8    Require all granted
9</Directory>

总的来说,您的 /etc/apache2/sites-enabled/your_domain.conffile 应该包含您新的 DocumentRoot 位置的所有下列行:

 1[label /etc/apache2/sites-enabled/your_domain.conf]
 2<VirtualHost *:80>
 3    ServerAdmin sammy@your_domain
 4    ServerName your_domain
 5    ServerAlias www.your_domain
 6    DocumentRoot /mnt/volume_nyc3_01
 7    ErrorLog ${APACHE_LOG_DIR}/error.log
 8    CustomLog ${APACHE_LOG_DIR}/access.log combined
 9RewriteEngine on
10RewriteCond %{SERVER_NAME} =www.your_domain [OR]
11RewriteCond %{SERVER_NAME} =your_domain
12RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
13</VirtualHost>
14
15<Directory /mnt/volume_nyc3_01>
16    Options FollowSymLinks
17    AllowOverride None
18    Require all granted
19</Directory>

您可以通过按CTRL + X来退出nano,然后按Y,然后按ENTER

完成这些更改后,您可以将注意力转向 SSL 配置。 打开 /etc/apache2/sites-enabled/your_domain-le-ssl.conf 文件:

1sudo nano /etc/apache2/sites-enabled/your_domain-le-ssl.conf

像以前的配置文件一样,修改DocumentRoot,以反映新的位置:

 1[label  /etc/apache2/sites-enabled/your_domain-le-ssl.conf]
 2<IfModule mod_ssl.c>
 3<VirtualHost *:443>
 4    ServerAdmin sammy@your_domain
 5    ServerName your_domain
 6    ServerAlias www.your_domain
 7    DocumentRoot /mnt/volume_nyc3_01
 8    ErrorLog ${APACHE_LOG_DIR}/error.log
 9    CustomLog ${APACHE_LOG_DIR}/access.log combined
10. . .
11</VirtualHost>
12</IfModule>

保存并关闭此文件。

您现在已经对必要的配置进行了更改,以反映您的文档根的新位置。

步骤 3 – 重启 Apache

完成更改后,您可以重新启动 Apache 并测试结果。

首先,请确保你的新配置的语法与configtest正确:

1sudo apachectl configtest

如果你的语法是正确的,你的输出应该显示如下:

1[secondary_label Output]
2AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
3Syntax OK

<$>[注] 注: 如果您想在此输出中抑制顶行,请在 /etc/apache2/apache2.conf 添加一个 ServerName 指令到您的全球 Apache 配置文件。

打开文件与您的编辑器:

1sudo nano /etc/apache2/apache2.conf

在配置文件的底部放置ServerName指令,其中包含your_domain或服务器的IP地址:

1[label /etc/apache2/apache2.conf]
2# This is the main Apache server configuration file. It contains the
3# configuration directives that give the server its instructions.
4# See http://httpd.apache.org/docs/2.4/ for detailed information about
5# the directives and /usr/share/doc/apache2/README.Debian about Debian specific
6# hints.
7...
8
9ServerName your_domain

保存并退出您的编辑器。

然而,这只是一个消息,不会影响您的网站的功能,只要输出包含Syntax OK,您就可以继续。

使用以下命令重新启动 Apache:

1sudo systemctl reload apache2

当服务器重新启动时,请访问受影响的网站,以确保它们按预期工作. 一旦您感到舒适,一切顺序,请不要忘记删除数据的原始副本:

1sudo rm -Rf /var/www/your_domain

您现在已成功将您的 Apache 文档根移到新的位置。

结论

在本教程中,您了解了如何将Apache文档根更改到一个新的位置,这可以帮助您进行基本的Web服务器管理,如有效地在单一服务器上托管多个网站,还允许您利用替代的存储设备,如网络块存储,这可能有助于扩展一个网站,因为它的需求发生变化。

Published At
Categories with 技术
comments powered by Disqus