如何在Ubuntu 16.04上将Apache默认Web根文件夹更改为新位置
在本文中,我们将学习如何将Apache默认的Web根文件夹更改为新位置。默认情况下,ApacheWeb根目录或Document根目录文件夹位于/var/www/html。
出于安全原因或由于数据大小而引起空间问题,这些类型的更改将非常有用,我们想将文档根文件夹更改为其他位置或装入点。如果我们有多个实例,并且希望将每个网站的数据保存在各自的卷或文件夹中,这将很有帮助。
先决条件
具有计算机上Sudo权限的用户的Ubuntu16.04服务器。
机器上安装的Apache2Web服务器
用于移动站点信息或文件的新安装位置或新文件夹位置。
将网站数据移到新位置
众所周知,ApacheWeb服务器的默认位置是/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命令,我们会将所有文件复制到一个新文件夹位置,在该位置我们要移动ApacheWeb服务器的默认文档根文件夹。
例如,在我们的环境中,新的Web根文档文件夹将为'/mnt/newdatavol'
$ sudo rysnc –av /var/www/html /mnt/newdatavol
修改Apache配置文件
Apache使用两种类型的配置文件,一种是全局的,另一种是特定于站点的配置,因为我们正在使用现有安装。我们将修改使用grep命令找到的虚拟主机文件。
默认情况下,我们必须更改Apache000-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>我们保存了文件,现在我们将在下面将SSL端口的ApacheWeb服务器更改为默认文件夹位置,以查看用于编辑default-ssl.conf的命令
$ 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>重新启动ApacheWeb服务器
将配置更改更改为默认文档根目录后,我们需要重新启动Apache服务器以使更改生效。在此之前,我们将检查语法错误
$ sudo apachectl configtest Syntax OK
正如我们看到的消息语法正确,现在我们可以继续并重新启动服务器以使更改生效。
$ sudo systemctl restart apache2
在以上文章中,我们学习了如何在Ubuntu16.04上更改Apache服务器的默认文档位置。这很容易备份我们发布的网站,而无需任何系统默认文件,当我们在不同位置拥有多个网站或客户网站数据时,它们将非常有用。我们还可以使用其他网络存储设备(例如NAS和SAN)安全地存储日期。