httpd配置虚拟主机三种方式——实验

httpd配置虚拟主机实验

配置前

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf 
# If your host doesn't have a registered DNS name, enter its IP address here.
ServerName 0.0.0.0:80             //修改主配置文件
······
[root@localhost ~]# vim /usr/share/doc/httpd/httpd-vhosts.conf                                //打开虚拟主机的示例文件
[root@localhost ~]# vim /etc/httpd/conf.d/vhosts.conf                                  //在con.f下创建虚拟主机配置文件,并将示例文件粘贴进去,再进行进一步的修改
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/var/www/dummy-host.example.com"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "/var/log/httpd/dummy-host.example.com-error_log"                //错误日志
    CustomLog "/var/log/httpd/dummy-host.example.com-access_log" common              //访问日志
</VirtualHost>

同IP不同端口

[root@localhost ~]# vim /etc/httpd/conf.d/vhosts.conf 
<VirtualHost  192.168.133.152:80>                           //80端口
        ServerName 192.168.133.152                          //域名或ip地址
        DocumentRoot /www/xiaopang                         //网站根目录
</VirtualHost>
<VirtualHost  192.168.133.152:8888>                       //8888端口
        ServerName 192.168.133.152
        DocumentRoot /www/xiaopang1
</VirtualHost>
LISTEN 8888                                                              //全局定义监听8888端口
<Directory /www>                                                       //定义/www的目录标签
        AllowOverride none                                            //.htaccess中权限不生效
        Require all granted                                             //访问控制列表允许所有主机访问
</Directory>
[root@localhost ~]# mkdir -p /www/{xiaopang,xiaopang1}
[root@localhost ~]# echo "xiaopang" > /www/xiaopang/index.html
[root@localhost ~]# echo "this is xiaopang1" > /www/xiaopang/index.html
[root@localhost xiaopang]# curl 192.168.133.152
xiaopang
[root@localhost xiaopang]# curl 192.168.133.152:8888
this is xioapang1 

不同IP同端口

[root@localhost ~]# vim /etc/httpd/conf.d/vhosts.conf 
<VirtualHost  192.168.133.152:80>
        ServerName 192.168.133.152
        DocumentRoot /www/xiaopang
</VirtualHost>
<VirtualHost  192.168.133.153:80>
        ServerName 192.168.133.153
        DocumentRoot /www/zhongpang
</VirtualHost>
<Directory /www>
        AllowOverride none
        Require all granted
</Directory>
[root@localhost ~]# mkdir -p /www/zhongpang
[root@localhost ~]# echo "zhongpang" > /www/zhongpang/index.html
[root@localhost xiaopang]# curl 192.168.133.152
xiaopang
[root@localhost xiaopang]# curl 192.168.133.153
zhongpang

同IP同端口不同域名

[root@localhost ~]# vim /etc/httpd/conf.d/vhosts.conf 
<VirtualHost  192.168.133.150:80>
        ServerName www.dapang.com
        DocumentRoot /www/dapang
</VirtualHost>
<VirtualHost  192.168.133.150:80>
        ServerName www.dapang.com
        DocumentRoot /www/dapang
</VirtualHost>
<Directory /www>
        AllowOverride none
        Require all granted
</Directory>
[root@localhost ~]# mkdir  /www/dapang
[root@localhost ~]# echo "dapang" > /www/dapang/index.html
[root@localhost ~]# vim /etc/hosts 
192.168.133.150 www.dapang.com www.dapang1.com
[root@localhost ~]# systemctl restart httpd.service 
[root@localhost xiaopang]# curl http://www.dapang.com
dapang
[root@localhost xiaopang]# curl http://www.dapang1.com
dapang
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿瑾~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值