如何在Ubuntu 16.04上将Apache默认Web根文件夹更改到新位置


在本文中,我们将学习如何将Apache默认Web根文件夹更改到新位置。默认情况下,Apache Web根目录或文档根目录位于/var/www/html。

此类更改对于安全原因或由于数据大小导致的空间问题非常有用,我们希望将文档根文件夹更改到另一个位置或挂载点。如果我们有多个实例并且希望将每个网站的数据保存在其自己的卷或文件夹中,这将非常有用。

先决条件

  • 一台Ubuntu 16.04服务器,用户具有机器上的Sudo权限。
  • 机器上已安装Apache2 Web服务器
  • 用于移动站点信息或文件的新挂载位置或新文件夹位置。

将网站数据移动到新位置

我们已经知道Apache Web服务器的默认位置是/var/www/html,或者如果您有多个站点以及为不同站点设置了多个文档根目录的不同设置。

如果我们有多个在Apache虚拟主机中配置并启用的站点,我们可以在/etc/apache2/sites-enabled文件夹中搜索文档根目录。

以下是查找服务器上发布的所有多个站点的文件夹名称的命令。

$ grep –R “DocumentRoot” /etc/apache2/sites-enabled

由于我只有一个站点,因此启用的输出将如下所示:

sites-enabled/000-default.conf DocumentRoot /var/www/html

使用**rsync**命令,我们将所有文件复制到我们要将Apache Web服务器的默认文档根文件夹移动到的新文件夹位置。

例如,在我们的环境中,新的Web根文档文件夹将是'/mnt/newdatavol'

$ sudo rysnc –av /var/www/html /mnt/newdatavol

修改Apache配置文件

Apache使用两种类型的配置文件,一种是全局的,另一种是特定于站点的配置,因为我们正在使用现有安装。我们将修改虚拟主机文件,这些文件可以使用grep命令找到。

默认情况下,我们必须更改Apache附带的两个虚拟主机配置文件000-default.conf和default-ssl.conf。

$ sudo vi /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
   # The ServerName directive sets the request scheme, hostname and port that
   # the server uses to identify itself. This is used when creating
   # redirection URLs. In the context of virtual hosts, the ServerName
   # specifies what hostname must appear in the request's Host: header to
   # match this virtual host. For the default virtual host (this file) this
   # value is not decisive as it is used as a last resort host regardless.
   # However, you must set it for any further virtual host explicitly.
   #ServerName www.example.com
   ServerAdmin webmaster@localhost
   DocumentRoot /mnt/newdatavol
   # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
   # error, crit, alert, emerg.
   # It is also possible to configure the loglevel for particular
   # modules, e.g.
   #LogLevel info ssl:warn
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
   # For most configuration files from conf-available/, which are
   # enabled or disabled at a global level, it is possible to
   # include a line for only one particular virtual host. For example the
   # following line enables the CGI configuration for this host only
   # after it has been globally disabled with "a2disconf".
   #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

我们保存文件,现在我们将更改Apache Web服务器的默认文件夹位置,以查看用于编辑default-ssl.conf的命令,请参见下面的SSL端口。

$ sudo vi /etc/apache2/sites-available/default-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
   ServerAdmin webmaster@localhost
   DocumentRoot /mnt/newdatavol
   # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
   # error, crit, alert, emerg.
   # It is also possible to configure the loglevel for particular
   # modules, e.g.
   #LogLevel info ssl:warn
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
   # For most configuration files from conf-available/, which are
   # enabled or disabled at a global level, it is possible to
   # include a line for only one particular virtual host. For example the
   # following line enables the CGI configuration for this host only
   # after it has been globally disabled with "a2disconf".
   #Include conf-available/serve-cgi-bin.conf
   # SSL Engine Switch:
   # Enable/Disable SSL for this
   ….
   ..
   ..
</VirtualHost>

重新启动Apache Web服务器

一旦我们将配置更改应用于默认文档根目录,我们就需要重新启动Apache服务器以使更改生效。在此之前,我们将检查语法错误。

$ sudo apachectl configtest
Syntax OK

正如我们看到的“语法正确”消息,现在我们可以继续重新启动服务器以使更改生效。

$ sudo systemctl restart apache2

在上面的文章中,我们学习了如何在Ubuntu 16.04上更改Apache服务器的默认文档位置。这将很容易备份我们发布的站点,而无需任何系统默认文件,当我们在不同位置拥有多个站点或客户端网站数据时,它们非常有用。我们还可以使用其他网络存储设备(如NAS和SAN)来安全地存储数据。

更新于:2020年1月21日

12K+ 次浏览

启动您的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.