多语言展示
当前在线:615今日阅读:113今日分享:31

如何基于端口号

基于端口号的虚拟主机功能可以让用户通过访问服务器上面指定的端口号来找到想要访问的网站资源,而用apache配置虚拟主机功能中最复杂的也莫过于是基于端口号的了,因为咱们不光需要考虑到httpd服务程序的配置因素,还需要考虑到SELinux服务对于新开设端口号的监控,占用服务器中80、443、8080类似的端口号是网站服务比较合理的请求,但再去占用其他的端口号就会遭受到SELinux服务的限制了,因此咱们接下来的实验中既要考虑到文件上面SELinux安全上下文的限制,还要考虑到SELinux域对httpd网站服务程序功能的管控。详情请关注《linux就该这么学》。
工具/原料

一台装有RHEL 7.0操作系统的电脑

方法/步骤
1

分别在/home/wwwroot中创建两个用于保存不同网站数据的目录,并向其中分别写入网站的首页文件,每个首页文件中应有明确区分不同网站内容的字样信息,方便咱们稍后能更直观的检查效果:[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服务的配置文件中大约43行后追加上监听6111和6222端口号的参数:[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  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服务的配置文件中大约114行处,分别追加写入两个基于端口号的虚拟主机网站参数,保存退出文件后记得要重启httpd服务才能生效哦:[root@linuxprobe ~]# vim /etc/httpd/conf/httpd.conf ………………省略部分输出信息……………… DocumentRoot '/home/wwwroot/6111' ServerName www.linuxprobe.com  AllowOverride None Require all granted DocumentRoot '/home/wwwroot/6222' ServerName bbs.linuxprobe.com  AllowOverride None Require all granted ………………省略部分输出信息………………

4

还是因为咱们将网站数据目录存放在了/home/wwwroot中,因此还是必须要把网站数据目录文件上面的SELinux安全上下文设置好,让文件上面的SELinux安全上下文与网站服务功能相吻合,最后还是要记得用restorecon命令让新配置的SELinux安全上下文立即生效呢:[root@linuxprobe ~]# semanage fcontext -a -t httpd_user_content_t /home/wwwroot [root@linuxprobe ~]# semanage fcontext -a -t httpd_user_content_t /home/wwwroot/6111 [root@linuxprobe ~]# semanage fcontext -a -t httpd_user_content_t /home/wwwroot/6111/* [root@linuxprobe ~]# semanage fcontext -a -t httpd_user_content_t /home/wwwroot/6222 [root@linuxprobe ~]# semanage fcontext -a -t httpd_user_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_user_content_t:s0 restorecon reset /home/wwwroot/6111 context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_user_content_t:s0 restorecon reset /home/wwwroot/6111/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_user_content_t:s0 restorecon reset /home/wwwroot/6222 context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_user_content_t:s0 restorecon reset /home/wwwroot/6222/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_user_content_t:s0 [root@linuxprobe ~]# systemctl restart httpd Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.什么??!!在咱们把httpd服务程序和SELinux安全上下文都配置妥当后,重启服务为什么会竟然出现报错信息呢?这是因为SELinux服务检测到6111和6222端口原本不属于apache应该需要的服务资源,但现在却被以httpd服务程序的名义监听使用了,便会直接拒绝掉了,咱们可以用semanage命令查询并过滤出所有与http协议相关的端口号SElinux允许列表:[root@linuxprobe ~]# semanage port -l| grep http http_cache_port_t tcp 8080, 8118, 8123, 1 http_cache_port_t udp 3130 http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000pegasus_http_port_t tcp 5988 pegasus_https_port_t tcp 5989

5

SELinux允许http协议使用的端口号中默认没有包含咱们的6111和6222,因此需要手动的添加进去就可以了,操作会立即生效,且重启过后依然有效,因此设置后再重启一下httpd服务程序就能看到网页内容了,如图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, 1http_cache_port_t udp 3130 http_port_t tcp  6222, 6111, 80, 81, 443, 488, 8008, 8009, 8443, 9000pegasus_http_port_t tcp 5988 pegasus_https_port_t tcp 5989 [root@linuxprobe ~]# systemctl restart httpd [root@linuxprobe ~]# firefox

推荐信息