網站搜尋

如何在 Ubuntu 18.04 LTS 中配置 Apache 虛擬主機


什麼是 Apache 虛擬主機?

虛擬主機一詞指的是在單一系統上運行多個網站的方法,例如 host1.domain.com、host2.domain.com,或 www.domain1.com、www.domain2.com 等。在 Apache 中有兩種類型的虛擬主機,即 基於 IP 的虛擬主機和 基於名稱的虛擬主機。使用基於 IP 的虛擬主機時,您可以在同一系統上託管多個網站或域名,但每個網站/域名都有不同的 IP 地址。使用基於名稱的虛擬主機時,您可以在同一個 IP 地址上託管多個網站/域名。如果您想在單一的實體伺服器或 VPS 上託管多個網站和域名,虛擬主機會非常有用。希望您已經了解了 Apache 虛擬主機的基本概念。今天,我們將看看如何在 Ubuntu 18.04 LTS 中配置 Apache 虛擬主機。

在 Ubuntu 18.04 LTS 中配置 Apache 虛擬主機

我的測試盒子 IP 位址是 192.168.225.22,主機名稱是 ubuntuserver。

首先,我們將看到如何在 Apache 網頁伺服器中配置基於名稱的虛擬主機。

配置基於名稱的虛擬主機

1. 安裝 Apache 網頁伺服器

確保你已經安裝了 Apache 網頁伺服器。要在 Ubuntu 上安裝它,請執行:

$ sudo apt-get install apache2

一旦安裝了 Apache,通過在瀏覽器中瀏覽 Apache 測試頁面來測試它是否正常工作。

打開您的網頁瀏覽器,並指向 http://IP_Address 或 http://localhost。您應該會看到如下頁面。

好!Apache 網頁伺服器已啟動並運作中!!

2. 為每個主機建立網頁目錄

我將要建立兩個虛擬主機,分別是 ostechnix1.lan 和 ostechnix2.lan。

讓我們為第一個虛擬主機 ostechnix1.lan 建立一個目錄。此目錄是用來存放我們虛擬主機的資料所必需的。

要這麼做,請輸入:

$ sudo mkdir -p /var/www/html/ostechnix1.lan/public_html

同樣地,為第二個虛擬主機 ostechnix2.lan 建立一個目錄,如下所示。

$ sudo mkdir -p /var/www/html/ostechnix2.lan/public_html

上述兩個目錄的所有權屬於 root 使用者。我們需要將所有權更改為一般使用者。

要做到這一點,請執行:

$ sudo chown -R $USER:$USER /var/www/html/ostechnix1.lan/public_html
$ sudo chown -R $USER:$USER /var/www/html/ostechnix2.lan/public_html

這裡,$USER 指的是當前登入的使用者。

接下來,使用以下命令將讀取權限設置到 Apache 根目錄,即 /var/www/html/:

$ sudo chmod -R 755 /var/www/html/

我們這麼做是因為我們已經為每個虛擬主機建立了單獨的目錄來存放它們的資料。因此,我們將 Apache 根目錄設為只讀,除了 root 使用者以外的所有使用者都不能修改。

我們已經建立了用於存儲每個虛擬主機數據的必要目錄,並設置了適當的權限。現在,是時候創建一些範例頁面,這些頁面將由每個虛擬主機提供服務。

3. 為每個主機建立示範網頁

讓我們為 ostechnix1.lan 網站建立一個範例頁面。為此,請執行:

$ sudo vi /var/www/html/ostechnix1.lan/public_html/index.html

在其中添加以下幾行:

<html>
 <head>
 <title>www.ostechnix.lan</title>
 </head>
 <body>
 <h1>Hello, This is a test page for ostechnix1.lan website</h1>
 </body>
</html>

儲存並關閉檔案。

同樣,為 ostechnix2.lan 網站創建一個範例頁面:

$ sudo vi /var/www/html/ostechnix2.lan/public_html/index.html

在其中添加以下幾行:

<html>
 <head>
 <title>www.ostechnix.lan</title>
 </head>
 <body>
 <h1>Hello, This is a test page for ostechnix2.lan website</h1>
 </body>
</html>

保存並關閉檔案。

4. 為每個主機建立設定檔

接下來,我們需要為每個虛擬主機建立配置檔案。首先,讓我們為 ostechnix1.lan 網站建立這個檔案。

將名為 000-default.conf 的預設虛擬主機檔案內容複製到新的虛擬主機檔案,如下所示。

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ostechnix1.lan.conf
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ostechnix2.lan.conf

請注意,您必須將所有配置文件保存為以 .conf 為結尾的檔案,否則它將無法運作。

現在,修改配置文件以與我們的虛擬主機相匹配。

編輯 ostechnix.lan1.conf 檔案:

$ sudo vi /etc/apache2/sites-available/ostechnix1.lan.conf

編輯/修改 ServerAdmin、ServerName、ServerAlias 和 DocumentRoot 的值,使其與虛擬主機相匹配。

<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@ostechnix1.lan
   ServerName ostechnix1.lan
   ServerAlias www.ostechnix1.lan
   DocumentRoot /var/www/html/ostechnix1.lan/public_html

# 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>

儲存並關閉檔案。

接下來,編輯 ostechnix2.lan.conf 文件:

$ sudo vi /etc/apache2/sites-available/ostechnix2.lan.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@ostechnix2.lan
    ServerName ostechnix2.lan
    ServerAlias www.ostechnix2.lan
    DocumentRoot /var/www/html/ostechnix2.lan/public_html

# 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>

儲存/關閉檔案。

5. 啟用虛擬主機配置文件

在完成必要的更改後,停用預設的虛擬主機配置文件,例如 000.default.conf,並啟用所有新建立的虛擬主機配置文件,如下所示。

$ sudo a2dissite 000-default.conf
$ sudo a2ensite ostechnix1.lan.conf
$ sudo a2ensite ostechnix2.lan.conf

重新啟動 Apache 網頁伺服器以使更改生效。

$ sudo systemctl restart apache2

就是這樣。我們已經成功配置了 Apache 的虛擬主機。讓我們繼續檢查它們是否正常運作。

6. 測試虛擬主機

在任何編輯器中打開 /etc/hosts 文件:

$ sudo vi /etc/hosts

像下面這樣一個一個添加你所有的虛擬網站/域名。

[...]
192.168.225.22   ostechnix1.lan
192.168.225.22   ostechnix2.lan
[...]

請注意,如果您想從任何遠端系統訪問虛擬主機,您必須在每個遠端系統的 /etc/hosts 文件中添加上述行。

儲存並關閉檔案。

打開您的網頁瀏覽器並指向 http://ostechnix1.lan 或 http://ostechnix2.lan。

ostechnix1.lan 測試頁面:

ostechnix2.lan 測試頁面:

恭喜!你現在可以訪問你所有的網站了。從現在起,你可以上傳資料並從不同的網站提供服務。

如你所注意到的,我們已經為主機使用了相同的 IP 位址(即 192.168.225.22)來承載兩個不同的網站(http://ostechnix1.lan 和 http://ostechnix2.lan)。這就是我們所說的基於名稱的虛擬主機。希望這對你有幫助。我將在下一個指南中向你展示如何配置基於 IP 的虛擬主機。在此之前,敬請期待!

資源:

  • Apache 虛擬主機文件