当前位置:   article > 正文

yum安装httpd配置三种虚拟主机_httpd安装

httpd安装

部署环境

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

一、httpd的安装和启动

1.yum下载httpd

yum install -y net-tools vim
  • 1
yum install -y httpd
  • 1

2.启动httpd

systemctl start httpd
  • 1

3.查看httpd状态,Active显示running

systemctl status httpd
  • 1
● 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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

4.临时关闭防火墙和selinux

systemctl stop firewalld
  • 1
setenforce 0
  • 1

5.浏览器访问本机ip测试httpd
在这里插入图片描述

二、基于IP的虚拟主机

1.配置虚拟主机IP,虚拟主机IP与本机IP在同一网段,这里临时配置两个IP,重启网卡会失效

ifconfig ens33:1 192.168.184.151/24
  • 1
ifconfig ens33:2 192.168.184.152/24
  • 1

2.创建虚拟主机的根目录,并创建页面文件

mkdir /var/www/151
  • 1
mkdir /var/www/152
  • 1
echo '192.168.184.151' >> /var/www/151/index.html
  • 1
echo '192.168.184.152' >> /var/www/152/index.html
  • 1

3.编辑httpd虚拟机配置文件

mkdir /etc/httpd/vhost
  • 1
vim /etc/httpd/vhost/151.conf
  • 1
<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
vim /etc/httpd/vhost/152.conf
  • 1
<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4.编辑httpd主配置文件,最后一行添加引入虚拟主机的子配置文件

vim /etc/httpd/conf/httpd.conf
  • 1
IncludeOptional vhost/*.conf
  • 1

5.重启httpd服务

systemctl restart httpd
  • 1

6.浏览器访问虚拟主机ip测试
在这里插入图片描述
在这里插入图片描述
7.Linux测试,返回结果一样

curl 192.168.184.151
  • 1
curl 192.168.184.152
  • 1

三、基于端口的虚拟主机

1.修改httpd主配置文件

vim /etc/httpd/conf/httpd.conf 
  • 1
在80后面添加81和82端口的监听
Listen 80
Listen 81
Listen 82
最后一行保留引入虚拟机的子配置文件
IncludeOptional vhost/*.conf
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.创建虚拟主机的根目录,并创建页面文件

mkdir /var/www/81
  • 1
mkdir /var/www/82
  • 1
echo 'This is 81 port' >> /var/www/81/index.html
  • 1
echo 'This is 82 port' >> /var/www/82/index.html
  • 1

3.编辑httpd虚拟机配置文件

vim /etc/httpd/vhost/81.conf
  • 1
<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
vim /etc/httpd/vhost/82.conf
  • 1
<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4.重启httpd服务

systemctl restart httpd
  • 1

5.浏览器访问ip:81和ip:82测试
在这里插入图片描述
在这里插入图片描述
6.Linux测试,返回结果相同

curl 192.168.184.150:81
  • 1
curl 192.168.184.150:82
  • 1

四、基于域名的虚拟主机

1.再临时配置一个IP

ifconfig ens33:5 192.168.184.155/24
  • 1

2.创建虚拟主机根目录和页面文件

mkdir /var/www/webserver1
  • 1
mkdir /var/www/webserver2
  • 1
echo 'www.webserver1.com' >> /var/www/webserver1/index.html
  • 1
echo 'www.webserver2.com' >> /var/www/webserver2/index.html
  • 1

3.编辑httpd虚拟机配置文件

vim /etc/httpd/vhost/webserver1.conf
  • 1
<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
vim /etc/httpd/vhost/webserver2.conf
  • 1
<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

4.确保httpd主配置文件引入虚拟机的子配置文件

5.重启httpd服务

systemctl restart httpd
  • 1

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
  • 1
  • 2

在这里插入图片描述
在这里插入图片描述

7.Linux在hosts末尾添加解析进行测试,返回值相同

vim /etc/hosts
  • 1
192.168.184.155 www.webserver1.com
192.168.184.155 www.webserver2.com
  • 1
  • 2
curl www.webserver1.com
  • 1
curl www.webserver2.com
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/669109
推荐阅读
相关标签
  

闽ICP备14008679号