1 案例1:普通NFS共享的實(shí)現(xiàn)
1.1 問題
本例要求在虛擬機(jī) server0 上配置NFS服務(wù)贮预,完成以下任務(wù):
- 只讀的方式共享目錄 /public,只能被 example.com 域中的系統(tǒng)訪問
- 可讀寫共享目錄/protected蜘澜,能被 example.com 域中的系統(tǒng)訪問
然后在虛擬機(jī) desktop0 上訪問NFS共享目錄
- 將 server0 的 /public 掛到本地 /mnt/nfsmount
- 這些文件系統(tǒng)在系統(tǒng)啟動時自動掛載
1.2 方案
對于普通NFS共享來說:
- 服務(wù)端需要運(yùn)行系統(tǒng)服務(wù) nfs-server.service
- 客戶端不需要運(yùn)行特定的系統(tǒng)服務(wù)
配置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(ro或rw等控制參數(shù)) 客戶地址2(ro或rw等控制參數(shù)) .. ..
</pre>
1.3 步驟
實(shí)現(xiàn)此案例需要按照如下步驟進(jìn)行箕戳。
步驟一:在server0上發(fā)布NFS共享目錄
1)準(zhǔn)備需要共享的文件夾
<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;">
- [root@server0 ~]# mkdir /public
- [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;">
- [root@server0 ~]# vim /etc/exports
- /public 172.25.0.0/24(ro)
- /protected 172.25.0.0/24(rw)
</pre>
3)啟動系統(tǒng)服務(wù)nfs-server黔帕,并設(shè)置開機(jī)自啟
<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;">
- [root@server0 ~]# systemctl restart nfs-server
- [root@server0 ~]# systemctl enable nfs-server
- 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)建掛載點(diǎn)
<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;">
- [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;">
- [root@desktop0 ~]# showmount -e server0.example.com
- Export list for server0.example.com:
- /protected 172.25.0.0/24
- /public 172.25.0.0/24
</pre>
3)配置開機(jī)掛載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;">
- [root@desktop0 ~]# vim /etc/fstab
- .. ..
- 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;">
- [root@desktop0 ~]# mount -a
- [root@desktop0 ~]# df -hT /mnt/nfsmount/
- Filesystem Type Size Used Avail Use% Mounted on
- server0.example.com:/public nfs4 10G 3.2G 6.8G 32% /mnt/nfsmount
</pre>
2 案例2:獨(dú)立Web站點(diǎn)的快速部署
2.1 問題
本例要求為 http://server0.example.com 配置Web站點(diǎn)代咸,要求如下:
- 從http://classroom/pub/materials/station.html下載一個主頁文件,將其重命名為 index.html
- 將此文件拷貝到站點(diǎn)的 DocumentRoot 目錄下成黄,不要對文件 index.html 的內(nèi)容作任何修改
- 使用 elinks 或firefox 瀏覽上述Web站點(diǎn)
2.2 方案
Web網(wǎng)站服務(wù)端:軟件包httpd呐芥、系統(tǒng)服務(wù)httpd
Web網(wǎng)站瀏覽器:軟件包elinks或fireox
傳輸協(xié)議及端口:TCP 80
Web網(wǎng)站服務(wù)端配置文件:
- /etc/httpd/conf/httpd.conf
- /etc/httpd/conf.d/*.conf
默認(rèn)首頁文件:index.html
httpd網(wǎng)站文檔的默認(rèn)根目錄:/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;">
- http://服務(wù)器地址[:端口號]/目錄/文件名
</pre>
對于需要驗(yàn)證的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;">
- ftp://用戶名:密碼@服務(wù)器地址[:端口號]/目錄/文件名
</pre>
2.3 步驟
實(shí)現(xiàn)此案例需要按照如下步驟進(jìn)行思瘟。
步驟一:構(gòu)建及部署網(wǎng)站服務(wù)器
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;">
- [root@server0 ~]# yum -y install httpd
- .. ..
</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;">
[root@server0 ~]# cd /var/www/html/ //進(jìn)入網(wǎng)頁目錄
[root@server0 html]# wget http://classroom/pub/materials/station.html -O index.html //下載網(wǎng)頁
.. ..
2016-11-26 19:33:49 (1.36 MB/s) - ‘index.html’ saved [14/14]
[root@server0 html]# cat index.html //檢查網(wǎng)頁文件
Default Site.
</pre>
3)啟動系統(tǒng)服務(wù)httpd荸百,并設(shè)置開機(jī)自啟
<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;">
- [root@server0 html]# systemctl restart httpd
- [root@server0 html]# systemctl enable httpd
- ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
</pre>
步驟二:訪問網(wǎng)站服務(wù)器
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;">
- [root@desktop0 ~]# yum -y install elinks //安裝elinks
- .. ..
- [root@desktop0 ~]# elinks -dump http://server0.example.com/ //訪問指定網(wǎng)址
- Default Site.
</pre>
2)使用firefox瀏覽器查看
Firefox瀏覽器支持更多網(wǎng)頁特性够话,是訪問復(fù)雜網(wǎng)頁、網(wǎng)址的優(yōu)秀工具光绕。
在桌面終端直接運(yùn)行“firefox http://server0.examle.com/”女嘲,或者通過菜單快捷方式打開Firefox瀏覽器再輸入對應(yīng)網(wǎng)址,都可以看到目標(biāo)網(wǎng)頁(如圖-1所示)诞帐。
圖-1
3 案例3:虛擬Web主機(jī)的部署
3.1 問題
本例要求為server0擴(kuò)展Web站點(diǎn)欣尼,新建虛擬主機(jī) http://www0.example.com,具體要求如下:
- 設(shè)置 DocumentRoot 為 /var/www/virtual
- 從 http://classroom/pub/materials/www.html 下載主頁文件停蕉,并重命名為 index.html
- 不要對文件 index.html 的內(nèi)容作任何修改愕鼓,將其放到此虛擬主機(jī)的 DocumentRoot 目錄下
- 確保 fleyd 用戶能在 /var/www/virtual 目錄建文件
- 確保站點(diǎn) 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
虛擬主機(jī)平臺(比如172.25.0.11):
- 在同一套httpd平臺上跑很多個網(wǎng)站
- 多個域名 ---> 不同的網(wǎng)頁內(nèi)容
- 網(wǎng)頁目錄由<VirtualHost ...>區(qū)段配置定義
多個虛擬主機(jī)站點(diǎn)的典型設(shè)置(/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;">
- <VirtualHost *:80>
- ServerName 網(wǎng)站1的FQDN
- DocumentRoot 網(wǎng)站1的網(wǎng)頁根目錄
- </VirtualHost>
- <VirtualHost *:80>
- ServerName 網(wǎng)站2的FQDN
- DocumentRoot 網(wǎng)站2的網(wǎng)頁根目錄
- </VirtualHost>
- .. ..
</pre>
3.3 步驟
實(shí)現(xiàn)此案例需要按照如下步驟進(jì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;">
- [root@server0 ~]# mkdir /var/www/virtual
- [root@server0 ~]# useradd fleyd
- [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;">
[root@server0 ~]# cd /var/www/virtual/
[root@server0 virtual]# wget http://classroom/pub/materials/www.html -O index.html
.. ..
100%[=====================>] 14 --.-K/s in 0s
2016-11-26 20:01:14 (826 KB/s) - ‘index.html’ saved [14/14]
[root@server0 virtual]# cat index.html //檢查網(wǎng)頁文件
Virtual Site.
</pre>
步驟二:配置虛擬主機(jī) http://www0.example.com/
1)為新站點(diǎn)創(chuàng)建獨(dú)立的配置文件
<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;">
- [root@server0 virtual]# vim /etc/httpd/conf.d/01-www0.conf
- <VirtualHost *:80>
- ServerName www0.example.com
- DocumentRoot /var/www/virtual
- </VirtualHost>
- [root@server0 virtual]# httpd -t //確保語法檢查OK
- Syntax OK
</pre>
2)重啟系統(tǒng)服務(wù)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;">
- [root@server0 virtual]# systemctl restart httpd
</pre>
步驟三:訪問虛擬主機(jī) http://www0.example.com/
訪問此虛擬站點(diǎn)慧起,可以看到預(yù)期的網(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;">
- [root@desktop0 ~]# elinks -dump http://www0.example.com/
- Virtual Site.
</pre>
步驟四:完善原始站點(diǎn) http://server0.example.com/
需要注意的是菇晃,原始的獨(dú)立站點(diǎn)可能出現(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;">
- [root@desktop0 ~]# elinks -dump http://server0.example.com/
- Virtual Site.
</pre>
原因是一旦啟用虛擬站點(diǎn)機(jī)制以后:
- 外部的 DocumentRoot完慧、ServerName 會被忽略
- 第1個虛擬站點(diǎn)被視為默認(rèn)站點(diǎn)谋旦,若客戶機(jī)請求的URL不屬于任何已知站點(diǎn),則由第1個站點(diǎn)響應(yīng)
若要解決此異常屈尼,需要將原始站點(diǎn)轉(zhuǎn)換為第一個虛擬主機(jī)册着,啟用順序的設(shè)置可以通過文件名開頭的數(shù)字來實(shí)現(xiàn)。
1)為原始站點(diǎn)建立虛擬主機(jī)配置
<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;">
- [root@server0 ~]# vim /etc/httpd/conf.d/00-default.conf
- <VirtualHost *:80>
- ServerName server0.example.com
- DocumentRoot /var/www/html
- </VirtualHost>
</pre>
2)重啟系統(tǒng)服務(wù)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;">
- [root@server0 virtual]# systemctl restart httpd
</pre>
3)訪問兩個虛擬站點(diǎn)脾歧,確保各自的網(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;">
- [root@desktop0 ~]# elinks -dump http://server0.example.com/
- Default Site.
- [root@desktop0 ~]# elinks -dump http://www0.example.com/
- Virtual Site.
</pre>