http虚拟主机配置
目录
相同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
即可成功访问