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)容的信息,方便我們稍后能更直觀地檢查效果。

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

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

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

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

    [root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf
    ………………省略部分輸出信息……………… 
    113 <VirtualHost 192.168.10.10:6111>
    114 DocumentRoot "/home/wwwroot/6111"
    115 ServerName www.linuxprobe.com
    116 <Directory "/home/wwwroot/6111">
    117 AllowOverride None
    118 Require all granted
    119 </Directory> 
    120 </VirtualHost>
    121 <VirtualHost 192.168.10.10:6222>
    122 DocumentRoot "/home/wwwroot/6222"
    123 ServerName bbs.linuxprobe.com
    124 <Directory "/home/wwwroot/6222">
    125 AllowOverride None
    126 Require all granted
    127 </Directory>
    128 </VirtualHost>
    ………………省略部分輸出信息………………

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

    [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
    [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111
    [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6111/*
    [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222
    [root@linuxprobe ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/6222/*
    [root@linuxprobe ~]# restorecon -Rv /home/wwwroot/
    restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
    restorecon reset /home/wwwroot/6111 context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
    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
    restorecon reset /home/wwwroot/6222 context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
    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
    [root@linuxprobe ~]# systemctl restart httpd
    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ù)允許的端口列表。

    [root@linuxprobe ~]# semanage port -l | grep http
    http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
    http_cache_port_t udp 3130
    http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
    pegasus_http_port_t tcp 5988
    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所示。

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

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

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

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)