http虚拟主机

http虚拟主机配置

目录

http虚拟主机配置

相同ip不同端口

配置不同ip相同端口的主机

配置相同IP相同端口不同域名的虚拟主机


相同ip不同端口

[root@node1 ~]# vim httpd.conf
#virtual host 1     # 虚拟主机1的配置
<VirtualHost 192.168.47.129:80>
    ServerName www.wzq.com
    DocumentRoot "/var/www/html/www"
    ErrorLog "/var/log/httpd/www/error_log"
    CustomLog "/var/log/httpd/www/access_log" combined
    <Directory /var/www/html/www>
        <RequireAll>
        Require all granted
        Require not ip 192.168.1
        </RequireAll>
    </Directory>
</VirtualHost>
# virtual host 2     # 虚拟主机2的配置
<VirtualHost 192.168.47.129:8080>
    ServerName blog.wzq.com
    DocumentRoot "/var/www/html/blog"
    ErrorLog "/var/log/httpd/blog/error_log"
    CustomLog "/var/log/httpd/blog/access_log" combined
    <Directory /var/www/html/blog>
        <RequireAll>
          Require all granted
        </RequireAll>
    </Directory>
</VirtualHost>

创建网页目录并修改属主属组

[root@node1 ~]# mkdir -p /var/www/html/{www,blog}
[root@node1 ~]# chown -R apache.apache /var/www/html/
[root@node1 ~]# ll /var/www/html/
total 0
drwxr-xr-x 2 apache apache 6 Dec 27 10:10 blog
drwxr-xr-x 2 apache apache 6 Dec 27 10:10 www
[root@node1 ~]#

添加测试文件

[root@node1 ~]# echo 'www test' > /var/www/html/www/index.html
[root@node1 ~]# echo 'blog test' > /var/www/html/blog/index.html

重启服务查看端口

[root@node1 ~]# systemctl restart httpd
[root@node1 ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port         Peer Address:Port    Process    
LISTEN    0         128                0.0.0.0:22                0.0.0.0:*                  
LISTEN    0         128                      *:8080                    *:*                  
LISTEN    0         128                      *:80                      *:*                  
LISTEN    0         128                   [::]:22                   [::]:*                  
[root@node1 ~]#

验证

[C:\~]$ curl http://192.168.47.129:80
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100     9  100     9    0     0   4800      0 --:--:-- --:--:-- --:--:--  9000
www test

[C:\~]$ curl http://192.168.47.129:8080
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    10  100    10    0     0    848      0 --:--:-- --:--:-- --:--:--   909
blog test

[C:\~]$

配置不同ip相同端口的主机

修改配置使相同IP相同端口不同

[root@node1 conf]# vim httpd.conf
[root@node1 conf]# tail -25 httpd.conf
<VirtualHost 192.168.47.129:80>
    ServerName www.wzq.com
    DocumentRoot "/var/www/html/www"
    ErrorLog "/var/log/httpd/www/error_log"
    CustomLog "/var/log/httpd/www/access_log" combined
    <Directory /var/www/html/www>
        <RequireAll>
           Require all granted
           Require not ip 192.168.1
        </RequireAll>
    </Directory>
</VirtualHost>
# virtual host 2     # 虚拟主机2的配置
<VirtualHost 192.168.47.130:80>
    ServerName blog.wzq.com
    DocumentRoot "/var/www/html/blog"
    ErrorLog "/var/log/httpd/blog/error_log"
    CustomLog "/var/log/httpd/blog/access_log" combined
    <Directory /var/www/html/blog>
        <RequireAll>
	   Require all granted
        </RequireAll>
    </Directory>
</VirtualHost>

[root@node1 conf]#

刷新服务

[root@node1 ~]# systemctl restart httpd
[root@node1 ~]# ss -antl
State      Recv-Q     Send-Q         Local Address:Port         Peer Address:Port     Process     
LISTEN     0          128                  0.0.0.0:22                0.0.0.0:*                    
LISTEN     0          128                        *:8080                    *:*                    
LISTEN     0          128                        *:80                      *:*                    
LISTEN     0          128                     [::]:22                   [::]:*                    
[root@node1 ~]# 

验证

[C:\~]$ curl http://192.168.47.129
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100     9  100     9    0     0   8637      0 --:--:-- --:--:-- --:--:--  9000
www test

[C:\~]$ curl http://192.168.47.130
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    10  100    10    0     0   5707      0 --:--:-- --:--:-- --:--:-- 10000
blog test

[C:\~]$

配置相同IP相同端口不同域名的虚拟主机

修改配置

[root@zhenzhu conf]# vim httpd.conf
[root@zhenzhu conf]# tail -25 httpd.conf
<VirtualHost 192.168.47.129:80>
    ServerName www.wzq.com
    DocumentRoot "/var/www/html/www"
    ErrorLog "/var/log/httpd/www/error_log"
    CustomLog "/var/log/httpd/www/access_log" combined
    <Directory /var/www/html/www>
        <RequireAll>
           Require all granted
           Require not ip 192.168.1
        </RequireAll>
    </Directory>
</VirtualHost>
# virtual host 2     # 虚拟主机2的配置
<VirtualHost 192.168.47.129:80>
    ServerName blog.wzq.com
    DocumentRoot "/var/www/html/blog"
    ErrorLog "/var/log/httpd/blog/error_log"
    CustomLog "/var/log/httpd/blog/access_log" combined
    <Directory /var/www/html/blog>
        <RequireAll>
	   Require all granted
        </RequireAll>
    </Directory>
</VirtualHost>
 
[root@zhenzhu conf]#

刷新服务

[root@node1 ~]# systemctl restart httpd
[root@node1 ~]# ss -antl
State      Recv-Q     Send-Q         Local Address:Port         Peer Address:Port     Process     
LISTEN     0          128                  0.0.0.0:22                0.0.0.0:*                    
LISTEN     0          128                        *:8080                    *:*                    
LISTEN     0          128                        *:80                      *:*                    
LISTEN     0          128                     [::]:22                   [::]:*                    
[root@node1 ~]#

需要本机可访问,修改主机里c:\windows\system32\drivers\etc\hosts的文件

添加上192.168.47.129 www.wzq.com blog.wzq.com

即可成功访问

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值