一、环境准备
我们需要两台虚拟机,而且都需要关闭防火墙和selinux
二、认识配置文件
// 虚拟机A:构建基本Web服务
// 我们需要先装包
]# yum -y install httpd
]# rpm -q httpd
// 先进行简单的测试
// /var/www/html/index.html ,其中index.html是我们默认的主页
// /var/www/html 是我们默认的网页根目录
]# echo test > /var/www/html/index.html
]# systemctl start httpd #启动服务
]# curl http://10.0.0.200 #测试访问
test
// 主配置文件:/etc/httpd/conf/httpd.conf
// 我们打开主配置文件
[root@tk ~]# cat /etc/httpd/conf/httpd.conf
// ....
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
// 该处的Listen表示httpd服务监听的端口,我们可以配置多个Listen
Listen 80
// ...
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
// 下面是httpd服务需要使用的用户和组
User apache
Group apache
// ...
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
// 该处的<Directory /> 里面写的是我们的web访问规则
// Directory 后面写的是网页存放路径,而且该规则是会继承的
// Require all denied #拒绝所有人访问
// Require all granted #允许所有人访问
# <Directory> blocks below.
#
<Directory />
AllowOverride none
Require all denied
</Directory>
// ...
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
// 该处的DocumentRoot 表示网页的根目录,我们访问web的时候,此时就会到该目录下寻找index.html
DocumentRoot "/var/www/html"
// ...
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here. If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
// 该处是我们的错误日志的目录
ErrorLog "logs/error_log"
//....
三、虚拟Web主机
// 虚拟Web主机
// 由同一台服务器提供多个不同的Web站点
// 区分方式
// 基于域名的虚拟主机
// 基于端口的虚拟主机
// 基于IP地址的虚拟主机
// 配置文件路径
// /etc/httpd/conf/httpd.conf #主配置文件
// /etc/httpd/conf.d/*.conf #调用配置文件
// 为每个虚拟站点添加配置
// <VirtualHost IP地址:端口>
// ServerName 此站点的DNS名称
// DocumentRoot 此站点的网页根目录
// </VirtualHost>
// 基于域名
[root@tk ~]# cat /etc/httpd/conf.d/test1.conf
<VirtualHost *:80>
ServerName www.test1.com
DocumentRoot /var/www/test1
</VirtualHost>
// 修改nds解析,解析到本机
[root@tk ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6q
10.0.0.200 www.test1.com
// 书写测试的html
[root@tk ~]# echo "test1" > /var/www/test1/index.html
// 重启服务
[root@tk ~]# systemctl restart httpd
// 测试访问
[root@tk ~]# curl http://www.test1.com
test1
[root@tk ~]#
// 基于端口的虚拟Web主机
// 我们只需要将端口修改即可
[root@tk ~]# cat /etc/httpd/conf.d/test1.conf
<VirtualHost *:80>
ServerName www.test1.com
DocumentRoot /var/www/test1
</VirtualHost>
<VirtualHost *:8080>
ServerName www.test1.com
DocumentRoot /var/www/test2
</VirtualHost>
[root@tk ~]#
// 注意:此时要在我们的主配置文件中开启8080端口
[root@tk ~]# mkdir /var/www/test2
[root@tk ~]# echo "test1:8080" > /var/www/test2/index.html
[root@tk ~]# systemctl restart httpd
test1:8080
// 基于IP进行配置
[root@tk ~]# cat /etc/httpd/conf.d/test1.conf
<VirtualHost *:80>
ServerName 10.0.0.200
DocumentRoot /var/www/test1
</VirtualHost>
<VirtualHost *:8080>
ServerName 10.0.0.201
DocumentRoot /var/www/test2
</VirtualHost>
[root@tk ~]#
[root@tk ~]# systemctl restart httpd
[root@tk ~]# curl http://10.0.0.200
test1
[root@tk ~]# curl http://10.0.0.201
test1
[root@tk ~]# curl http://10.0.0.201:8080
test1:8080