配置NFS共享 、 HTTP服務基礎

  1. 案例1:普通NFS共享的實現(xiàn)
  2. 案例2:獨立Web站點的快速部署
  3. 案例3:虛擬Web主機的部署

1 案例1:普通NFS共享的實現(xiàn)

1.1 問題

本例要求在虛擬機 server0 上配置NFS服務,完成以下任務:

  1. 只讀的方式共享目錄 /public,只能被 example.com 域中的系統(tǒng)訪問
  2. 可讀寫共享目錄/protected,能被 example.com 域中的系統(tǒng)訪問

然后在虛擬機 desktop0 上訪問NFS共享目錄

  1. 將 server0 的 /public 掛到本地 /mnt/nfsmount
  2. 這些文件系統(tǒng)在系統(tǒng)啟動時自動掛載

1.2 方案

對于普通NFS共享來說:

  • 服務端需要運行系統(tǒng)服務 nfs-server.service
  • 客戶端不需要運行特定的系統(tǒng)服務

配置NFS共享目錄的記錄格式:

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. 文件夾絕對路徑 客戶地址1(ro或rw等控制參數(shù)) 客戶地址2(ro或rw等控制參數(shù)) .. ..

</pre>

1.3 步驟

實現(xiàn)此案例需要按照如下步驟進行。

步驟一:在server0上發(fā)布NFS共享目錄

1)準備需要共享的文件夾

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# mkdir /public
  2. [root@server0 ~]# mkdir /protected

</pre>

2)建立NFS共享配置

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# vim /etc/exports
  2. /public 172.25.0.0/24(ro)
  3. /protected 172.25.0.0/24(rw)

</pre>

3)啟動系統(tǒng)服務nfs-server,并設置開機自啟

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# systemctl restart nfs-server
  2. [root@server0 ~]# systemctl enable nfs-server
  3. ln -s '/usr/lib/systemd/system/nfs-server.service' '/etc/systemd/system/nfs.target.wants/nfs-server.service'

</pre>

步驟二:在desktop0上掛載NFS共享目錄/public

1)創(chuàng)建掛載點

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@desktop0 ~]# mkdir /mnt/nfsmount

</pre>

2)列出server0上提供的NFS共享資源

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@desktop0 ~]# showmount -e server0.example.com
  2. Export list for server0.example.com:
  3. /protected 172.25.0.0/24
  4. /public 172.25.0.0/24

</pre>

3)配置開機掛載server0的NFS共享目錄/public

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@desktop0 ~]# vim /etc/fstab
  2. .. ..
  3. server0.example.com:/public /mnt/nfsmount nfs _netdev 0 0

</pre>

4)測試掛載配置

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@desktop0 ~]# mount -a
  2. [root@desktop0 ~]# df -hT /mnt/nfsmount/
  3. Filesystem Type Size Used Avail Use% Mounted on
  4. server0.example.com:/public nfs4 10G 3.2G 6.8G 32% /mnt/nfsmount

</pre>

2 案例2:獨立Web站點的快速部署

2.1 問題

本例要求為 http://server0.example.com 配置Web站點,要求如下:

  1. http://classroom/pub/materials/station.html下載一個主頁文件,將其重命名為 index.html
  2. 將此文件拷貝到站點的 DocumentRoot 目錄下,不要對文件 index.html 的內(nèi)容作任何修改
  3. 使用 elinks 或firefox 瀏覽上述Web站點

2.2 方案

Web網(wǎng)站服務端:軟件包httpd、系統(tǒng)服務httpd

Web網(wǎng)站瀏覽器:軟件包elinks或fireox

傳輸協(xié)議及端口:TCP 80

Web網(wǎng)站服務端配置文件:

  • /etc/httpd/conf/httpd.conf
  • /etc/httpd/conf.d/*.conf

默認首頁文件:index.html

httpd網(wǎng)站文檔的默認根目錄:/var/www/html

URL(Uniform Resource Locator,統(tǒng)一資源定位器)網(wǎng)址的基本組成:

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. http://服務器地址[:端口號]/目錄/文件名

</pre>

對于需要驗證的FTP資源,還需要指定用戶名密碼信息:

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. ftp://用戶名:密碼@服務器地址[:端口號]/目錄/文件名

</pre>

2.3 步驟

實現(xiàn)此案例需要按照如下步驟進行。

步驟一:構建及部署網(wǎng)站服務器

1)安裝軟件包httpd

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# yum -y install httpd
  2. .. ..

</pre>

2)部署網(wǎng)頁

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# cd /var/www/html/ //進入網(wǎng)頁目錄

  2. [root@server0 html]# wget http://classroom/pub/materials/station.html -O index.html //下載網(wǎng)頁

  3. .. ..

  4. 2016-11-26 19:33:49 (1.36 MB/s) - ‘index.html’ saved [14/14]

  5. [root@server0 html]# cat index.html //檢查網(wǎng)頁文件

  6. Default Site.

</pre>

3)啟動系統(tǒng)服務httpd,并設置開機自啟

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 html]# systemctl restart httpd
  2. [root@server0 html]# systemctl enable httpd
  3. ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

</pre>

步驟二:訪問網(wǎng)站服務器

1)使用elinks瀏覽器查看

Elinks瀏覽器可以在命令行模式顯示出網(wǎng)頁文本,經(jīng)常用來測試網(wǎng)站的可用性。

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@desktop0 ~]# yum -y install elinks //安裝elinks
  2. .. ..
  3. [root@desktop0 ~]# elinks -dump http://server0.example.com/ //訪問指定網(wǎng)址
  4. Default Site.

</pre>

2)使用firefox瀏覽器查看

Firefox瀏覽器支持更多網(wǎng)頁特性,是訪問復雜網(wǎng)頁、網(wǎng)址的優(yōu)秀工具。

在桌面終端直接運行“firefox http://server0.examle.com/”,或者通過菜單快捷方式打開Firefox瀏覽器再輸入對應網(wǎng)址,都可以看到目標網(wǎng)頁(如圖-1所示)。

image

圖-1

3 案例3:虛擬Web主機的部署

3.1 問題

本例要求為server0擴展Web站點,新建虛擬主機 http://www0.example.com,具體要求如下:

  1. 設置 DocumentRoot 為 /var/www/virtual
  2. http://classroom/pub/materials/www.html 下載主頁文件,并重命名為 index.html
  3. 不要對文件 index.html 的內(nèi)容作任何修改,將其放到此虛擬主機的 DocumentRoot 目錄下
  4. 確保 fleyd 用戶能在 /var/www/virtual 目錄建文件
  5. 確保站點 http://server0.example.com 仍然可用

3.2 方案

單一網(wǎng)站平臺(比如172.25.0.11):

  • 多個域名 ---> 相同的網(wǎng)頁內(nèi)容
  • 配置文件:/etc/httpd/conf/httpd.conf
  • 網(wǎng)頁目錄定義:DocumentRoot /var/www/html

虛擬主機平臺(比如172.25.0.11):

  • 在同一套httpd平臺上跑很多個網(wǎng)站
  • 多個域名 ---> 不同的網(wǎng)頁內(nèi)容
  • 網(wǎng)頁目錄由<VirtualHost ...>區(qū)段配置定義

多個虛擬主機站點的典型設置(/etc/httpd/conf.d/*.conf):

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. <VirtualHost *:80>
  2. ServerName 網(wǎng)站1的FQDN
  3. DocumentRoot 網(wǎng)站1的網(wǎng)頁根目錄
  4. </VirtualHost>
  5. <VirtualHost *:80>
  6. ServerName 網(wǎng)站2的FQDN
  7. DocumentRoot 網(wǎng)站2的網(wǎng)頁根目錄
  8. </VirtualHost>
  9. .. ..

</pre>

3.3 步驟

實現(xiàn)此案例需要按照如下步驟進行。

步驟一:部署網(wǎng)頁文檔

1)建立網(wǎng)頁目錄

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# mkdir /var/www/virtual
  2. [root@server0 ~]# useradd fleyd
  3. [root@server0 ~]# setfacl -m u:fleyd:rwx /var/www/virtual/

</pre>

2)部署網(wǎng)頁文件

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# cd /var/www/virtual/

  2. [root@server0 virtual]# wget http://classroom/pub/materials/www.html -O index.html

  3. .. ..

  4. 100%[=====================>] 14 --.-K/s in 0s

  5. 2016-11-26 20:01:14 (826 KB/s) - ‘index.html’ saved [14/14]

  6. [root@server0 virtual]# cat index.html //檢查網(wǎng)頁文件

  7. Virtual Site.

</pre>

步驟二:配置虛擬主機 http://www0.example.com/

1)為新站點創(chuàng)建獨立的配置文件

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 virtual]# vim /etc/httpd/conf.d/01-www0.conf
  2. <VirtualHost *:80>
  3. ServerName www0.example.com
  4. DocumentRoot /var/www/virtual
  5. </VirtualHost>
  6. [root@server0 virtual]# httpd -t //確保語法檢查OK
  7. Syntax OK

</pre>

2)重啟系統(tǒng)服務httpd

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 virtual]# systemctl restart httpd

</pre>

步驟三:訪問虛擬主機 http://www0.example.com/

訪問此虛擬站點,可以看到預期的網(wǎng)頁內(nèi)容:

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@desktop0 ~]# elinks -dump http://www0.example.com/
  2. Virtual Site.

</pre>

步驟四:完善原始站點 http://server0.example.com/

需要注意的是,原始的獨立站點可能出現(xiàn)異常,訪問時并不是原始的網(wǎng)頁:

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@desktop0 ~]# elinks -dump http://server0.example.com/
  2. Virtual Site.

</pre>

原因是一旦啟用虛擬站點機制以后:

  • 外部的 DocumentRoot、ServerName 會被忽略
  • 第1個虛擬站點被視為默認站點,若客戶機請求的URL不屬于任何已知站點,則由第1個站點響應

若要解決此異常,需要將原始站點轉換為第一個虛擬主機,啟用順序的設置可以通過文件名開頭的數(shù)字來實現(xiàn)。

1)為原始站點建立虛擬主機配置

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 ~]# vim /etc/httpd/conf.d/00-default.conf
  2. <VirtualHost *:80>
  3. ServerName server0.example.com
  4. DocumentRoot /var/www/html
  5. </VirtualHost>

</pre>

2)重啟系統(tǒng)服務httpd

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@server0 virtual]# systemctl restart httpd

</pre>

3)訪問兩個虛擬站點,確保各自的網(wǎng)頁內(nèi)容正確

<pre class="code sh_javascript snippet-formatted sh_sourceCode" style="font-size: 14px; color: rgb(0, 0, 0); margin-left: 40px; background-color: rgb(238, 238, 238); font-weight: normal; font-style: normal; padding: 1em; line-height: 1.8em; overflow: auto; position: relative; border-radius: 15px; box-shadow: rgb(0, 0, 0) 2px 2px 5px;">

  1. [root@desktop0 ~]# elinks -dump http://server0.example.com/
  2. Default Site.
  3. [root@desktop0 ~]# elinks -dump http://www0.example.com/
  4. Virtual Site.

</pre>

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容