10.5.3 基于端口號(hào)

2023-05-12 11:04 更新

基于端口號(hào)的虛擬主機(jī)功能可以讓用戶通過指定的端口號(hào)來訪問服務(wù)器上的網(wǎng)站資源。在使用Apache配置虛擬網(wǎng)站主機(jī)功能時(shí),基于端口號(hào)的配置方式是最復(fù)雜的。因此我們不僅要考慮httpd服務(wù)程序的配置因素,還需要考慮到SELinux服務(wù)對(duì)新開設(shè)端口的監(jiān)控。一般來說,使用80、443、8080等端口號(hào)來提供網(wǎng)站訪問服務(wù)是比較合理的,如果使用其他端口號(hào)則會(huì)受到SELinux服務(wù)的限制。

在接下來的實(shí)驗(yàn)中,我們不但要考慮到目錄上應(yīng)用的SELinux安全上下文的限制,還需要考慮SELinux域?qū)ttpd服務(wù)程序的管控。

第1步:分別在/home/wwwroot中創(chuàng)建用于保存不同網(wǎng)站數(shù)據(jù)的兩個(gè)目錄,并向其中分別寫入網(wǎng)站的首頁文件。每個(gè)首頁文件中應(yīng)有明確區(qū)分不同網(wǎng)站內(nèi)容的信息,方便我們稍后能更直觀地檢查效果。

  1. [root@linuxprobe ~]# mkdir -p /home/wwwroot/6111
  2. [root@linuxprobe ~]# mkdir -p /home/wwwroot/6222
  3. [root@linuxprobe ~]# echo "port:6111" > /home/wwwroot/6111/index.html
  4. [root@linuxprobe ~]# echo "port:6222" > /home/wwwroot/6222/index.html

第2步:在httpd服務(wù)配置文件的第43行和第44行分別添加用于監(jiān)聽6111和6222端口的參數(shù)。

  1. [root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf
  2. ………………省略部分輸出信息………………
  3. 33 #
  4. 34 # Listen: Allows you to bind Apache to specific IP addresses and/or
  5. 35 # ports, instead of the default. See also the <VirtualHost>
  6. 36 # directive.
  7. 37 #
  8. 38 # Change this to Listen on specific IP addresses as shown below to
  9. 39 # prevent Apache from glomming onto all bound IP addresses.
  10. 40 #
  11. 41 #Listen 12.34.56.78:80
  12. 42 Listen 80
  13. 43 Listen 6111
  14. 44 Listen 6222
  15. ………………省略部分輸出信息………………

第3步:在httpd服務(wù)的配置文件中大約113行處開始,分別追加寫入兩個(gè)基于端口號(hào)的虛擬主機(jī)網(wǎng)站參數(shù),然后保存并退出。記得需要重啟httpd服務(wù),這些配置才生效。

  1. [root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf
  2. ………………省略部分輸出信息………………
  3. 113 <VirtualHost 192.168.10.10:6111>
  4. 114 DocumentRoot "/home/wwwroot/6111"
  5. 115 ServerName www.linuxprobe.com
  6. 116 <Directory "/home/wwwroot/6111">
  7. 117 AllowOverride None
  8. 118 Require all granted
  9. 119 </Directory>
  10. 120 </VirtualHost>
  11. 121 <VirtualHost 192.168.10.10:6222>
  12. 122 DocumentRoot "/home/wwwroot/6222"
  13. 123 ServerName bbs.linuxprobe.com
  14. 124 <Directory "/home/wwwroot/6222">
  15. 125 AllowOverride None
  16. 126 Require all granted
  17. 127 </Directory>
  18. 128 </VirtualHost>
  19. ………………省略部分輸出信息………………

第4步:因?yàn)槲覀儼丫W(wǎng)站數(shù)據(jù)目錄存放在/home/wwwroot目錄中,因此還是必須要正確設(shè)置網(wǎng)站數(shù)據(jù)目錄文件的SELinux安全上下文,使其與網(wǎng)站服務(wù)功能相吻合。最后記得用restorecon命令讓新配置的SELinux安全上下文立即生效。

  1. [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
  2. [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111
  3. [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111/*
  4. [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222
  5. [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222/*
  6. [root@linuxprobe ~]# restorecon -Rv /home/wwwroot/
  7. restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
  8. restorecon reset /home/wwwroot/6111 context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
  9. restorecon reset /home/wwwroot/6111/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
  10. restorecon reset /home/wwwroot/6222 context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
  11. restorecon reset /home/wwwroot/6222/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
  12. [root@linuxprobe ~]# systemctl restart httpd
  13. Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.

見鬼了!在妥當(dāng)配置httpd服務(wù)程序和SELinux安全上下文并重啟httpd服務(wù)后,竟然出現(xiàn)報(bào)錯(cuò)信息。這是因?yàn)镾ELinux服務(wù)檢測到6111和6222端口原本不屬于Apache服務(wù)應(yīng)該需要的資源,但現(xiàn)在卻以httpd服務(wù)程序的名義監(jiān)聽使用了,所以SELinux會(huì)拒絕使用Apache服務(wù)使用這兩個(gè)端口。我們可以使用semanage命令查詢并過濾出所有與HTTP協(xié)議相關(guān)且SELinux服務(wù)允許的端口列表。

  1. [root@linuxprobe ~]# semanage port -l | grep http
  2. http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
  3. http_cache_port_t udp 3130
  4. http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
  5. pegasus_http_port_t tcp 5988
  6. pegasus_https_port_t tcp 5989

第5步:SELinux允許的與HTTP協(xié)議相關(guān)的端口號(hào)中默認(rèn)沒有包含6111和6222,因此需要將這兩個(gè)端口號(hào)手動(dòng)添加進(jìn)去。該操作會(huì)立即生效,而且在系統(tǒng)重啟過后依然有效。設(shè)置好后再重啟httpd服務(wù)程序,然后就可以看到網(wǎng)頁內(nèi)容了,結(jié)果如圖10-17所示。

  1. [root@linuxprobe ~]# semanage port -a -t http_port_t -p tcp 6111
  2. [root@linuxprobe ~]# semanage port -a -t http_port_t -p tcp 6222
  3. [root@linuxprobe ~]# semanage port -l| grep http
  4. http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
  5. http_cache_port_t udp 3130
  6. http_port_t tcp 6222, 6111, 80, 81, 443, 488, 8008, 8009, 8443, 9000
  7. pegasus_http_port_t tcp 5988
  8. pegasus_https_port_t tcp 5989
  9. [root@linuxprobe ~]# systemctl restart httpd
  10. [root@linuxprobe ~]# firefox

圖10-17 基于端口號(hào)訪問虛擬主機(jī)網(wǎng)站

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)