httpd的虚拟主机功能
Posted 人间忽晚,山河以秋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpd的虚拟主机功能相关的知识,希望对你有一定的参考价值。
配置http的默认虚拟主机
一、基于主机域名的虚拟主机
一台服务器可以访问多个网站,每个网站都是一个虚拟主机。
概念:域名(主机名)、DNS、解析域名、hosts
任何一个域名解析到这台机器,都可以访问的虚拟主机就是默认虚拟主机(这里以一个域名为例,一个域名对应一个首页文件,配置多个域名对应不同的首页文件)
1、编辑主配置文件
[root@shell html]# vim /etc/httpd/conf/httpd.conf
#在配置文件的下面加入以下内容(这是一个虚拟主机的配置)
<VirtualHost 10.30.59.219> # IP地址为你系统的地址
DocumentRoot "/var/www" # 目录为存放首页文件的目录
ServerName "www.http.com" # 你想使用的域名
<Directory "/var/www"> #目录为存放首页文件的目录
AllowOverride None
Require all granted
</directory>
</VirtualHost>
2、配置域名解析文件,把ip地址和域名关联在一起(对个域名则在后面加上)
[root@shell html]# vim /etc/hosts
#加入以下内容
10.30.59.219 www.hello.com
3、为了能看出效果,我们修改以下首页文件。使网页显示welcome
[root@shell ]#echo "welcome">/var/www/html/index.html
4、测试以下
[root@shell html]# curl www.hello.com
welcome
因为这个域名解析实在搭建http的Linux系统里写的,所以在Windows浏览器是不能浏览到,如果想要实现同样的结果要在Windows系统的hosts文件里加入地址和域名
二、基于IP地址的虚拟主机
一台服务器,设置多个ip
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-ens192 ifdown-sit ifup-plusb
ifcfg-ens192.bak ifdown-Team ifup-post
ifcfg-lo ifdown-TeamPort ifup-ppp
ifdown ifdown-tunnel ifup-routes
ifdown-bnep ifup ifup-sit
ifdown-eth ifup-aliases ifup-Team
ifdown-ippp ifup-bnep ifup-TeamPort
ifdown-ipv6 ifup-eth ifup-tunnel
ifdown-isdn ifup-ippp ifup-wireless
ifdown-post ifup-ipv6 init.ipv6-global
ifdown-ppp ifup-isdn network-functions
ifdown-routes ifup-plip network-functions-ipv6
[root@localhost network-scripts]# cp ifcfg-ens192 ifcfg-ens192\\:1 # 复制网卡成ifcfg-ens192:1
[root@localhost network-scripts]# vim ifcfg-ens192:1 # 编辑该网卡文件
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
NAME=ens192
DEVICE=ens192:1 # 对应网卡名
ONBOOT=yes
IPADDR=10.30.59.249 # 改一个地址
NETMASK=255.255.255.128
GATEWAY=10.30.59.129
DNS1=114.114.114.114
~
~
~
~
~
~
~
"ifcfg-ens192:1" 12L, 203C 已写入
[root@localhost network-scripts]# systemctl restart network # 重启网络
[root@localhost network-scripts]# ip a # 看看是否有该网卡
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:50:56:a0:b7:0c brd ff:ff:ff:ff:ff:ff
inet 10.30.59.248/25 brd 10.30.59.255 scope global noprefixroute ens192
valid_lft forever preferred_lft forever
inet 10.30.59.249/25 brd 10.30.59.255 scope global secondary noprefixroute ens192:1
valid_lft forever preferred_lft forever
inet6 fe80::250:56ff:fea0:b70c/64 scope link
valid_lft forever preferred_lft forever
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:c9:62:89:95 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
[root@localhost network-scripts]# ping 10.30.59.249 #测试连通性
PING 10.30.59.249 (10.30.59.249) 56(84) bytes of data.
64 bytes from 10.30.59.249: icmp_seq=1 ttl=64 time=0.085 ms
64 bytes from 10.30.59.249: icmp_seq=2 ttl=64 time=0.054 ms
64 bytes from 10.30.59.249: icmp_seq=3 ttl=64 time=0.054 ms
^C
--- 10.30.59.249 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.054/0.064/0.085/0.016 ms
加入了三个ip,测试以下连通性,都可以通
[root@localhost ~]# ping 10.30.59.248
PING 10.30.59.248 (10.30.59.248) 56(84) bytes of data.
64 bytes from 10.30.59.248: icmp_seq=1 ttl=64 time=0.085 ms
64 bytes from 10.30.59.248: icmp_seq=2 ttl=64 time=0.054 ms
^C
--- 10.30.59.248 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.054/0.069/0.085/0.017 ms
[root@localhost ~]# ping 10.30.59.249
PING 10.30.59.249 (10.30.59.249) 56(84) bytes of data.
64 bytes from 10.30.59.249: icmp_seq=1 ttl=64 time=0.063 ms
64 bytes from 10.30.59.249: icmp_seq=2 ttl=64 time=0.062 ms
^C
--- 10.30.59.249 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.062/0.062/0.063/0.007 ms
[root@localhost ~]# ping 10.30.59.250
PING 10.30.59.250 (10.30.59.250) 56(84) bytes of data.
64 bytes from 10.30.59.250: icmp_seq=1 ttl=64 time=0.051 ms
64 bytes from 10.30.59.250: icmp_seq=2 ttl=64 time=0.059 ms
64 bytes from 10.30.59.250: icmp_seq=3 ttl=64 time=0.058 ms
^C
--- 10.30.59.250 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.051/0.056/0.059/0.003 ms
进入网站数据目录,创建不同地址对应的不同首页文件
[root@localhost ]# cd /var/www
[root@localhost www]# ls
cgi-bin html
[root@localhost www]# cd html
在html目录下创建三个目录,是每一个地址对应的首页数据目录,这里为了好识别,我使用以地址最后一段为目录名
[root@localhost html]# mkdir 248 249 250
[root@localhost html]# ls
248 249 250
然后分别创建首页文件,效果就是网页上显示对应地址的ip
[root@localhost html]# echo "ip:10.30.59.248" > 248/index.html
[root@localhost html]# echo "ip:10.30.59.249" > 249/index.html
[root@localhost html]# echo "ip:10.30.59.250" > 250/index.html
然后配置服务主配置文件
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
# files. This usually improves server performance, but must
# be turned off when serving from networked-mounted
# filesystems or if support for these functions is otherwise
# broken on your system.
# Defaults if commented: EnableMMAP On, EnableSendfile Off
#
#EnableMMAP off
EnableSendfile on
# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if
any.
IncludeOptional conf.d/*.conf
<VirtualHost 10.30.59.248>
DocumentRoot "/var/www/html/248"
ServerName "www.local.com"
<Directory "/var/www/html/248">
AllowOverride None
Require all granted
</directory>
</VirtualHost>
<VirtualHost 10.30.59.249>
DocumentRoot "/var/www/html/249"
ServerName "www.local.com"
<Directory "/var/www/html/249">
AllowOverride None
Require all granted
</directory>
</VirtualHost>
<VirtualHost 10.30.59.250>
DocumentRoot "/var/www/html/250"
ServerName "www.local.com"
<Directory "/var/www/html/250">
AllowOverride None
Require all granted
</directory>
</VirtualHost>
然后重启服务
[root@localhost ~]# systemctl restart httpd
浏览网页
以上是关于httpd的虚拟主机功能的主要内容,如果未能解决你的问题,请参考以下文章
httpd Server not started: (13)Permission denied: make_sock: could not bind to address [::]:8888(代码片段