介绍
在Ubuntu上,Apache Web 服务器(LINK0)将其文档存储在 /var/www/html
中,该文档通常位于 root 文件系统和其他操作系统中。
在本指南中,您将移动一个Apache文档根到一个新的位置。
前提条件
要完成本指南,您将需要:
- 一个 Ubuntu 18.04 服务器和一个非 root 用户具有 sudo 特权. 你可以在我们的 初始服务器设置与 Ubuntu 18.04 指南中了解更多有关如何设置一个用户具有这些特权的信息。
- Apache 安装,随后 如何在 Ubuntu 18.04 上安装Apache Web Server。
- SSL 配置为您的域名,随后 How To Secure Apache with Let's Encrypt on Ubuntu 18.04])。 我们将在本教程中使用域名 example.com,但您应该用自己的域名替换。
- 一个新的位置为您的根文件。 在本教程中,我们将使用我们的新位置的 `/mnt
步骤 1 – 将文件复制到新位置
在新安装的Apache中,文档根位于 /var/www/html
。 但是,通过遵循前提指南,您创建了一个新的文档根, /var/www/example.com/html
. 在相应的‘VirtualHost’指令中,您还可能有额外的文档根。
您可以使用grep搜索您的文档根的位置。让我们在/etc/apache2/sites-enabled
目录中搜索,以限制我们的关注到活跃的网站。
1grep -R "DocumentRoot" /etc/apache2/sites-enabled
如果您在新鲜的服务器上遵循前提教程,结果将是这样的:
1[secondary_label Output]
2/etc/apache2/sites-enabled/example.com-le-ssl.conf: DocumentRoot /var/www/example.com/html
3/etc/apache2/sites-enabled/example.com.conf: DocumentRoot /var/www/example.com/html
如果您有已有的设置,您的结果可能与这里所示不同. 在任何情况下,您可以使用grep
的反馈来确保您正在移动所需的文件并更新相应的配置文件。
现在您已经确认了文档根的位置,您可以使用rsync
将文件复制到新的位置。使用-a
旗帜将保留权限和其他目录属性,而-v
则提供无语音输出,以便您可以跟踪同步的进展:
<$>[注] 注: 请确保目录上没有后续缩减,如果您使用 tab completion 添加,则可能出现后续缩减。
1sudo rsync -av /var/www/example.com/html /mnt/volume-nyc3-01
你会看到这样的输出:
1[secondary_label Output]
2sending incremental file list
3html/
4html/index.html
5
6sent 318 bytes received 39 bytes 714.00 bytes/sec
7total size is 176 speedup is 0.49
有了我们的文件,让我们继续修改我们的Apache配置以反映这些变化。
步骤 2 – 更新配置文件
对于配置文件等级的背景,请参阅 如何在Ubuntu或Debian VPS上配置Apache Web Server。我们将修改(我们为example.com
项目的虚拟主机文件)(https://andsky.com/tech/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04#step-5-%E2%80%94-setting-up-virtual-hosts-(recommended)): /etc/apache2/sites-enabled/example.com.conf
和 /etc/apache2/sites-enabled/example.com-le-ssl.conf
,这是我们为example.com
配置SSL证书时创建的(https://andsky.com/tech/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-18-04)。
<$>[注] 注: 请记住,在您的情况下,‘example.com’将是‘your_domain_name’,并且您将在步骤 1 中执行‘grep’命令时输出的虚拟主机文件进行修改。
首先,打开/etc/apache2/sites-enabled/example.com.conf
:
1sudo nano /etc/apache2/sites-enabled/example.com.conf
找到以DocumentRoot
开头的行,并将其更新到新的根位置. 在我们的情况下,这将是/mnt/volume-nyc3-01/html
:
1[label /etc/apache2/sites-enabled/example.com.conf]
2<VirtualHost *:80>
3 ServerAdmin [email protected]
4 ServerName example.com
5 ServerAlias www.example.com
6 DocumentRoot /mnt/volume-nyc3-01/html
7 ErrorLog ${APACHE_LOG_DIR}/error.log
8 CustomLog ${APACHE_LOG_DIR}/access.log combined
9RewriteEngine on
10RewriteCond %{SERVER_NAME} =www.example.com [OR]
11RewriteCond %{SERVER_NAME} =example.com
12RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
13</VirtualHost>
让我们还添加指令,以确保服务器会遵循目录中的象征链接:
1[label /etc/apache2/sites-enabled/example.com.conf]
2. . .
3<Directory /mnt/volume-nyc3-01/html>
4 Options FollowSymLinks
5 AllowOverride None
6 Require all granted
7</Directory>
请注意在步骤 1 中输出的DocumentRoot
,包括代名或重写,您还需要更新这些,以反映新文档的根位置。
保存这些更改后,让我们把注意力转向SSL配置。 打开 /etc/apache2/sites-enabled/example.com-le-ssl.conf
:
1sudo nano /etc/apache2/sites-enabled/example.com-le-ssl.conf
更改DocumentRoot
,以反映新的位置, /mnt/volume-nyc3-01/html
:
1[label /etc/apache2/sites-enabled/example.com-le-ssl.conf]
2<IfModule mod_ssl.c>
3<VirtualHost *:443>
4 ServerAdmin [email protected]
5 ServerName example.com
6 ServerAlias www.example.com
7 DocumentRoot /mnt/volume-nyc3-01/html
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
如果你想删除顶线,只需将ServerName
指令添加到你的主要(全球)Apache配置文件在/etc/apache2/apache2.conf
。ServerName
可以是你的服务器的域名或IP地址,但这只是一个消息,不会影响你的网站的功能。
使用以下命令重新启动 Apache:
1sudo systemctl reload apache2
当服务器重新启动时,请访问受影响的网站并确保它们按预期工作. 一旦您感到舒适,一切顺序,请不要忘记删除数据的原始副本:
1sudo rm -Rf /var/www/example.com/html
您现在已成功将您的 Apache 文档根移到新的位置。
结论
在本教程中,我们涵盖了如何将Apache文档根更改到一个新的位置,这可以帮助您进行基本的Web服务器管理,例如在单一服务器上有效托管多个网站。
如果您正在管理繁忙或日益增长的网站,您可能对学习 如何加载测试您的 Web 服务器来识别性能瓶颈,然后在生产中遇到它们。