Server: 172.25.1.211

[root@server ~]# yum -y install httpd policycoreutils-python # policycoreutils-python 软件包提供 semanage 命令
[root@server ~]# cp /usr/share/doc/httpd/httpd-2.4.6/httpd-vhost.conf /etc/httpd/conf.d/
[root@server ~]# mkdir /var/www/virtual
[root@server ~]# mkdir /web
[root@server ~]# echo '<h1>html</h1>' > /var/www/html/index.html
[root@server ~]# echo '<h1>virtual</h1>' > /var/www/virtual/index.html
[root@server ~]# echo '<h1>web</h1>' > /web/index.html

相同端口,不同域名

[root@server ~]# nmcli con mod static +ipv4.addresses 172.25.254.212/24 +ipv4.addresses 172.25.254.213/24
[root@server ~]# vim /etc/httpd/conf.d/httpd-vhost.conf 
	<VirtualHost *:80>
    	DocumentRoot "/var/www/html"
    	ServerName html.example.com
	</VirtualHost>

	<VirtualHost *:80>
    	DocumentRoot "/var/www/virtual"
    	ServerName virtual.example.com
	</VirtualHost>

	<VirtualHost *:80>
    	DocumentRoot "/web"
    	ServerName web.example.com
	</VirtualHost>

	<Directory "/web">
    	AllowOverride None
        Require all granted
    </Directory>
[root@server ~]# ls -Zd /var/www/html
	drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
[root@server ~]# semanage fcontext -a -t httpd_sys_content_t '/web(/.*)?'
[root@server ~]# restrorecon -RFv /web
	restorecon reset /web context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
	restorecon reset /web/index.html context unconfined_u:object_r:default_t:s0->system_u:object_r:httpd_sys_content_t:s0
[root@server ~]# firewall-cmd --permanent --add-port=80/tcp
[root@server ~]# firewall-cmd --reload
[root@server ~]# systemctl restart httpd

相同IP,不同端口

[root@server ~]# vim /etc/httpd/conf.d/httpd-vhost.conf 
    <VirtualHost *:80>
    	DocumentRoot "/var/www/html"
    	ServerName 172.25.254.211:80
	</VirtualHost>
    Listen 808
	<VirtualHost *:808>
    	DocumentRoot "/var/www/virtual"
    	ServerName 172.25.254.211:808
	</VirtualHost>
    Listen 8088
	<VirtualHost *:8088>
    	DocumentRoot "/web"
    	ServerName 172.25.254.211:8088
	</VirtualHost>

	<Directory "/web">
    	AllowOverride None
        Require all granted
    </Directory>
[root@server ~]# semanage fcontext -a -t httpd_sys_content_t '/web(/.*)?'
[root@server ~]# restrorecon -RFv /web
[root@server ~]# 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
[root@server ~]# semanage port -a -t http_port_t -p tcp 808
[root@server ~]# semanage port -a -t http_port_t -p tcp 8088
[root@server ~]# firewall-cmd --permanent --add-port=80/tcp
[root@server ~]# firewall-cmd --permanent --add-port=808/tcp
[root@server ~]# firewall-cmd --permanent --add-port=8088/tcp
[root@server ~]# firewall-cmd --reload
[root@server ~]# systemctl restart httpd

不同IP

[root@server ~]# vim /etc/httpd/conf.d/httpd-vhost.conf 
    <VirtualHost 172.25.254.211:80>
    	DocumentRoot "/var/www/html"
    	ServerName 172.25.254.211:80
	</VirtualHost>

	<VirtualHost 172.25.254.212:80>
    	DocumentRoot "/var/www/virtual"
    	ServerName 172.25.254.212
	</VirtualHost>

	<VirtualHost 172.25.254.213:80>
    	DocumentRoot "/web"
    	ServerName 172.25.254.213
	</VirtualHost>

	<Directory "/web">
    	AllowOverride None
        Require all granted
    </Directory>
[root@server ~]# semanage fcontext -a -t httpd_sys_content_t '/web(/.*)?'
[root@server ~]# restrorecon -RFv /web
[root@server ~]# firewall-cmd --permanent --add-port=80/tcp
[root@server ~]# firewall-cmd --reload
[root@server ~]# systemctl restart httpd

个人用户主页

[root@server ~]# vim /etc/httpd/conf.d/userdir.conf
	#UserDir disable
    UserDir public_html
[root@server ~]# useradd webmaster
[root@server ~]# mkdir /home/webmaster/public_html
[root@server ~]# echo '<h1>webmaster</h1>' > /home/webmaster/public_html/index.html
[root@server ~]# chmod o+x /home/webmaster/
[root@server ~]# getsebool -a | grep httpd
	......
	httpd_enable_cgi --> on
	httpd_enable_ftp_server --> off
!	httpd_enable_homedirs --> off
	httpd_execmem --> off
	httpd_graceful_shutdown --> on
	httpd_manage_ipa --> off
	......
[root@server ~]# setsebool -P httpd_enable_homedirs on
[root@server ~]# firewall-cmd --permanent --add-port=80/tcp
[root@server ~]# firewall-cmd --reload
[root@server ~]# systemctl restart httpd

Desktop: 172.25.254.210

修改 hosts 文件,通过浏览器访问对应域名或 ip:port

hosts 文件追加如下内容
相同IP,不同域名&相同IP,不同端口

172.25.254.211	html.exampe.com	virtual.example.com	web.example.com

不同IP

172.25.254.211  html.example.com
172.25.254.212  virtual.example.com
172.25.254.213  web.example.com