赞
踩
centos7最小化安装,本机IP为192.168.184.150
1.基于IP的虚拟机IP为192.168.184.151和192.168.184.152
2.基于端口的虚拟机为192.168.184.150:81和192.168.184.150:82
3.基于域名的虚拟主机为192.168.184.155
1.yum下载httpd
yum install -y net-tools vim
yum install -y httpd
2.启动httpd
systemctl start httpd
3.查看httpd状态,Active显示running
systemctl status httpd
● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Sat 2021-02-20 18:21:30 CST; 53s ago Docs: man:httpd(8) man:apachectl(8) Main PID: 1400 (httpd) Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: /system.slice/httpd.service ├─1400 /usr/sbin/httpd -DFOREGROUND ├─1401 /usr/sbin/httpd -DFOREGROUND ├─1402 /usr/sbin/httpd -DFOREGROUND ├─1403 /usr/sbin/httpd -DFOREGROUND ├─1404 /usr/sbin/httpd -DFOREGROUND └─1405 /usr/sbin/httpd -DFOREGROUND Feb 20 18:21:30 localhost.localdomain systemd[1]: Starting The Apache HTTP Server... Feb 20 18:21:30 localhost.localdomain httpd[1400]: AH00558: httpd: Could not reliably determine ...age Feb 20 18:21:30 localhost.localdomain systemd[1]: Started The Apache HTTP Server. Hint: Some lines were ellipsized, use -l to show in full.
4.临时关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
5.浏览器访问本机ip测试httpd
1.配置虚拟主机IP,虚拟主机IP与本机IP在同一网段,这里临时配置两个IP,重启网卡会失效
ifconfig ens33:1 192.168.184.151/24
ifconfig ens33:2 192.168.184.152/24
2.创建虚拟主机的根目录,并创建页面文件
mkdir /var/www/151
mkdir /var/www/152
echo '192.168.184.151' >> /var/www/151/index.html
echo '192.168.184.152' >> /var/www/152/index.html
3.编辑httpd虚拟机配置文件
mkdir /etc/httpd/vhost
vim /etc/httpd/vhost/151.conf
<VirtualHost 192.168.184.151>
DocumentRoot "/var/www/151"
DirectoryIndex index.html
<Directory "/var/www/151">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
vim /etc/httpd/vhost/152.conf
<VirtualHost 192.168.184.152>
DocumentRoot "/var/www/152"
DirectoryIndex index.html
<Directory "/var/www/152">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
4.编辑httpd主配置文件,最后一行添加引入虚拟主机的子配置文件
vim /etc/httpd/conf/httpd.conf
IncludeOptional vhost/*.conf
5.重启httpd服务
systemctl restart httpd
6.浏览器访问虚拟主机ip测试
7.Linux测试,返回结果一样
curl 192.168.184.151
curl 192.168.184.152
1.修改httpd主配置文件
vim /etc/httpd/conf/httpd.conf
在80后面添加81和82端口的监听
Listen 80
Listen 81
Listen 82
最后一行保留引入虚拟机的子配置文件
IncludeOptional vhost/*.conf
2.创建虚拟主机的根目录,并创建页面文件
mkdir /var/www/81
mkdir /var/www/82
echo 'This is 81 port' >> /var/www/81/index.html
echo 'This is 82 port' >> /var/www/82/index.html
3.编辑httpd虚拟机配置文件
vim /etc/httpd/vhost/81.conf
<VirtualHost 192.168.184.150:81>
DocumentRoot "/var/www/81"
DirectoryIndex index.html
<Directory "/var/www/81">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
vim /etc/httpd/vhost/82.conf
<VirtualHost 192.168.184.150:82>
DocumentRoot "/var/www/82"
DirectoryIndex index.html
<Directory "/var/www/82">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
4.重启httpd服务
systemctl restart httpd
5.浏览器访问ip:81和ip:82测试
6.Linux测试,返回结果相同
curl 192.168.184.150:81
curl 192.168.184.150:82
1.再临时配置一个IP
ifconfig ens33:5 192.168.184.155/24
2.创建虚拟主机根目录和页面文件
mkdir /var/www/webserver1
mkdir /var/www/webserver2
echo 'www.webserver1.com' >> /var/www/webserver1/index.html
echo 'www.webserver2.com' >> /var/www/webserver2/index.html
3.编辑httpd虚拟机配置文件
vim /etc/httpd/vhost/webserver1.conf
<VirtualHost 192.168.184.155>
ServerName www.webserver1.com
DocumentRoot "/var/www/webserver1"
DirectoryIndex index.html
<Directory "/var/www/webserver1">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
vim /etc/httpd/vhost/webserver2.conf
<VirtualHost 192.168.184.155>
ServerName www.webserver2.com
DocumentRoot "/var/www/webserver2"
DirectoryIndex index.html
<Directory "/var/www/webserver2>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
4.确保httpd主配置文件引入虚拟机的子配置文件
5.重启httpd服务
systemctl restart httpd
6.Windows添加域名解析,添加两条解析信息。www.webserver1.com和www.webserver2.com未注册,不能直接访问。该操作需要管理员权限,也可以将文件拖到其他位置修改完在覆盖
文件位置:C:\Windows\System32\drivers\etc\hosts
192.168.184.155 www.webserver1.com
192.168.184.155 www.webserver2.com
7.Linux在hosts末尾添加解析进行测试,返回值相同
vim /etc/hosts
192.168.184.155 www.webserver1.com
192.168.184.155 www.webserver2.com
curl www.webserver1.com
curl www.webserver2.com
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。