配置httpd虚拟主机
相同ip不同端口
设置主机名
[root@liu ~]# vim /usr/local/apache/conf/httpd.conf
...
# If your host doesn't have a registered DNS name, enter its IP address here.
#
#取消此行前面的#号注释
ServerName www.example.com:80
在配置文件/usr/local/apache/conf/的最后加上虚拟主机的配置内容
[root@liu conf]# vim httpd.conf
#virtual host 1 # 虚拟主机1的配置说明
<VirtualHost 192.168.29.128:80>
ServerName www.liu.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
</RequireAll>
</Directory>
</VirtualHost>
# virtual host 2 # 虚拟主机2的配置说明
<VirtualHost 192.168.29.128:8080>
ServerName blog.liu.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>
创建网页www blog并更改属组
#在html目录下创建www 和 blog
[root@liu ~]# mkdir -p /var/www/html/{www,blog}
[root@liu ~]# cd /var/www/html/
[root@liu html]# ls
blog www
#chown将指定文件的拥有者改为指定的用户或组
[root@liu html]# chown -R apache.apache blog
[root@liu html]# chown -R apache.apache www
[root@liu html]# ll
total 0
drwxr-xr-x 2 apache apache 6 Dec 26 20:33 blog
drwxr-xr-x 2 apache apache 6 Dec 26 20:33 www
创建网页测试test
[root@liu html]# ls
blog www
[root@liu html]# echo 'www test' > www/index.html
[root@liu html]# echo 'blog test' > blog/index.html
#创建出对应的网页日志目录
[root@liu ~]# mkdir -p /var/log/httpd/{blog,www}
[root@liu ~]# ls /var/log/httpd
blog www
在httpd.conf配置文件中的Listen添加8080端口
[root@liu ~]# vim /usr/local/apache/conf/httpd.conf
...
#Listen 12.34.56.78:80
Listen 80
Listen 8080 #在这添加8080端口号
...
启动服务查看是否有80端口
[root@liu ~]# systemctl start httpd
[root@liu ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:58247 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:20048 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:43549 [::]:*
LISTEN 0 128 *:8080 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:20048 [::]:*
LISTEN 0 128 [::]:22 [::]:*
使用curl访问测试
[root@liu ~]# curl http://192.168.29.128:80
www test
[root@liu ~]# curl http://192.168.29.128:8080
blog test
不同ip相同端口
修改配置文件httpd.conf
#将端口都修改为80端口,IP做出变更128和129
[root@liu conf]# vim httpd.conf
#virtual host 1 # 虚拟主机1的配置说明
<VirtualHost 192.168.29.128:80>
ServerName www.liu.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
</RequireAll>
</Directory>
</VirtualHost>
# virtual host 2 # 虚拟主机2的配置说明
<VirtualHost 192.168.29.129:80>
ServerName blog.liu.com
DocumentRoot "/var/www/html/blog"
ErrorLog "/var/log/httpd/blog/error_log"
CustomLog "/var/log/httpd/blog/access_log" combined
...
测试添加临时ip
[root@liu ~]# ip addr add 192.168.29.129/24 dev eth0
[root@liu ~]# ip a
...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:3c:8a:33 brd ff:ff:ff:ff:ff:ff
inet 192.168.29.128/24 brd 192.168.29.255 scope global dynamic noprefixroute eth0
valid_lft 1523sec preferred_lft 1523sec
inet 192.168.29.129/24 scope global secondary eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe3c:8a33/64 scope link noprefixroute
valid_lft forever preferred_lft forever
重启服务使用curl命令测试访问
#重启服务
[root@liu ~]# systemctl restart httpd
#curl测试访问
[root@liu ~]# curl http://192.168.29.128
www test
[root@liu ~]# curl http://192.168.29.129
blog test
相同ip不同域名
修改配置文件httpd.conf
#将端口都修改为80端口,IP做出变更128
[root@liu conf]# vim httpd.conf
#virtual host 1 # 虚拟主机1的配置说明
<VirtualHost 192.168.29.128:80>
ServerName www.liu.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
</RequireAll>
</Directory>
</VirtualHost>
# virtual host 2 # 虚拟主机2的配置说明
<VirtualHost 192.168.29.128:80>
ServerName blog.liu.com
DocumentRoot "/var/www/html/blog"
ErrorLog "/var/log/httpd/blog/error_log"
CustomLog "/var/log/httpd/blog/access_log" combined
...
修改host文件
windows的host文件位置:c:\windows\system32\drivers\hosts
#将文件拉出到桌面上用写字板打开添加好设定的ip和域名后再放回去
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
添加项 192.168.29.128 www.liu.com blog.liu.com
测试访问
#重启服务
[root@liu ~]# systemctl restart httpd
#命令curl测试访问
[root@liu ~]# curl www.liu.com
www test
[root@liu ~]# curl blog.liu.com
www test
标签:httpd,www,log,虚拟主机,配置,blog,liu,root
From: https://www.cnblogs.com/lqy0917/p/17007134.html