如何在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+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告