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

介绍

在Ubuntu上,Nginx Web 服务器(https://www.nginx.com/)将其文档存储在 /var/www/html中,该文档通常位于 root 文件系统和其他操作系统上。

在本指南中,您将移动一个 Nginx 文档根到一个新的位置。

前提条件

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

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

我们将在本教程中使用域名 example.com,但您应该用自己的域名来代替。

  • 您的文档根的新位置 在本教程中,我们将为我们的新位置使用 /mnt/volume-nyc3-01 目录. 如果您在 DigitalOcean 上使用区块存储, 本指南将向您展示如何创建和粘贴您的卷。 您的新文档根位置可以根据您的需求进行配置。 但是,如果您正在将文档根移动到不同的存储设备,您将希望在设备的安装点下选择一个位置。

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

在新的 Nginx 安装中,文档根位于 /var/www/html。 但是,通过遵循前提指南,您创建了一个新的文档根, /var/www/example.com/html. 您也可能有额外的文档根。

您可以使用grep搜索文档根的位置。让我们在/etc/nginx/sites-enabled目录中搜索,以限制我们的关注到活跃的网站。

1grep -R "root" /etc/nginx/sites-enabled

如果您在新鲜的服务器上遵循前提教程,结果将是这样的:

1[secondary_label Output]
2/etc/nginx/sites-enabled/example.com:           root /var/www/example.com/html;
3/etc/nginx/sites-enabled/default:               root /var/www/html;
4/etc/nginx/sites-enabled/default:               # deny access to .htaccess files, if Apache's document root
5/etc/nginx/sites-enabled/default:#              root /var/www/example.com;

如果您有已有的设置,您的结果可能与这里所示不同. 在任何情况下,您可以使用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
3created directory /mnt/volume-nyc3-01
4html/
5html/index.html
6
7sent 318 bytes received 39 bytes 714.00 bytes/sec
8total size is 176 speedup is 0.49

有了我们的文件,让我们继续修改我们的 Nginx 配置,以反映这些变化。

步骤 2 – 更新配置文件

Nginx 使用全球和特定站点的配置文件. 有关配置文件等级的背景,请参阅 如何在虚拟私人服务器上配置 Nginx Web 服务器. 我们将更改我们的 example.com’ 项目的 [服务器封锁文件](https://andsky.com/tech/tutorials/how-to-install-nginx-on-ubuntu-18-04#step-5-%E2%80%93-setting-up-server-blocks-(recommended)): /etc/nginx/sites-enabled/example.com`。

<$>[注] 注: 请记住,在您的情况下,‘example.com’将是‘your_domain_name’,并且您将在步骤 1 中执行‘grep’命令时输出的服务器封锁文件进行修改。

首先,在编辑器中打开/etc/nginx/sites-enabled/example.com:

1sudo nano /etc/nginx/sites-enabled/example.com

找到以「root」开头的行,并将其更新到新的根位置. 在我们的情况下,这将是 /mnt/volume-nyc3-01/html:

1[label /etc/nginx/sites-enabled/example.com]
2server {
3
4        root /mnt/volume-nyc3-01/html;
5        index index.html index.htm index.nginx-debian.html;
6        . . .
7}
8. . .

请注意在步骤 1 中看到原始文档根路径的任何其他位置,包括代名或重写,您还需要更新这些位置以反映新文档根位置。

完成所有必要的更改后,保存并关闭文件。

步骤 3 – 重新启动 Nginx

一旦您完成了配置更改,您可以重新启动 Nginx 并测试结果。

首先,确保语法是正确的:

1sudo nginx -t

如果一切顺利,它应该返回:

1[secondary_label Output]
2nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
3nginx: configuration file /etc/nginx/nginx.conf test is successful

如果测试失败,请跟踪并修复问题。

通过测试后,重新启动 Nginx:

1sudo systemctl restart nginx

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

1sudo rm -Rf /var/www/example.com/html

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

结论

在本教程中,我们涵盖了如何将 Nginx 文档根更改到一个新的位置,这可以帮助您进行基本的 Web 服务器管理,例如在单个服务器上有效管理多个网站。

如果您正在管理繁忙或日益增长的网站,您可能对学习(如何使用 HTTP/2)(https://andsky.com/tech/tutorials/how-to-set-up-nginx-with-http-2-support-on-ubuntu-18-04)来利用其高传输速度的内容感兴趣,您还可以在这篇比较中了解更多有关提高生产体验的信息(https://andsky.com/tech/tutorials/5-ways-to-improve-your-production-web-application-server-setup)。

Published At
Categories with 技术
comments powered by Disqus