CentOS7配置httpd虚拟主机

时间:2023-11-24 21:39:02

本实验旨在CentOS7系统中,httpd-2.4配置两台虚拟主机,主要有以下要求:

(1) 提供两个基于名称的虚拟主机:

  www1.stuX.com,页面文件目录为/web/vhosts/www1;错误日志为/var/log/httpd/www1/error_log,访问日志为/var/log/httpd/www1/access_log;

  www2.stuX.com,页面文件目录为/web/vhosts/www2;错误日志为/var/log/httpd/www2/error_log,访问日志为/var/log/httpd/www2/access_log;

(2) 通过www1.stuX.com/server-status输出其状态信息,且要求只允许提供账号的用户访问;

(3) www1不允许192.168.1.0/24网络中的主机访问;

查看系统版本和httpd版本

[root@host ~]$httpd -v
Server version: Apache/2.4. (CentOS)
Server built: Nov ::
[root@host ~]$cat /etc/centos-release
CentOS Linux release 7.3. (Core)

启动httpd,测试能否正常运行

[root@host ~]$systemctl start httpd.service
[root@host ~]$systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Thu -- :: CST; 5s ago # active 表示正常运行
Docs: man:httpd()
man:apachectl()
Process: ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=/SUCCESS)
Main PID: (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─ /usr/sbin/httpd -DFOREGROUND
├─ /usr/sbin/httpd -DFOREGROUND
├─ /usr/sbin/httpd -DFOREGROUND
├─ /usr/sbin/httpd -DFOREGROUND
├─ /usr/sbin/httpd -DFOREGROUND
└─ /usr/sbin/httpd -DFOREGROUND Jun :: host systemd[]: Starting The Apache HTTP Server...
Jun :: host systemd[]: Started The Apache HTTP Server.

使用curl命令访问

[root@host ~]$ip a show ens38    # 查看ip
: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::dc::5f brd ff:ff:ff:ff:ff:ff
inet 192.168.55.128/ brd 192.168.55.255 scope global dynamic ens38
valid_lft 1752sec preferred_lft 1752sec
inet6 fe80::20c:29ff:fedc:185f/ scope link
valid_lft forever preferred_lft forever
[root@host ~]$curl http://192.168.55.128 # 访问
<!DOCTYPE>
<h1>
CentOS 7.3
</h1>

创建指定文件目录

[root@host conf.d]$mkdir -pv /web/vhosts/www1
[root@host conf.d]$mkdir -pv /web/vhosts/www2
[root@host conf.d]$mkdir -pv /var/log/httpd/www2
[root@host conf.d]$mkdir -pv /var/log/httpd/www1

根据要求填写虚拟主机配置信息

# path /etc/httpd/conf.d/vir.conf      # 配置文件全路径
#virtual host 1   # 虚拟主机1的配置
<VirtualHost 192.168.55.128:>
ErrorLog "/var/log/httpd/www1/error_log"
CustomLog "/var/log/httpd/www1/access_log" combined
<Location /server-status>
SetHandler server-status
</Location>
<Directory /web/vhosts/www1>
<RequireAll>
Require all granted
Require not ip 192.168.
</RequireAll>
</Directory>
</VirtualHost>
# virtual host 2 # 虚拟主机2的配置
<VirtualHost 192.168.55.128:>
ServerName www2.stuX.com
DocumentRoot "/web/vhosts/www2"
ErrorLog "/var/log/httpd/www2/error_log"
CustomLog "/var/log/httpd/www2/access_log" combined
<Directory /web/vhosts/www2>
<RequireAll>
  Require all granted
</RequireAll>
</Directory>
</VirtualHost>

创建www1和www2的index页面

[root@host conf.d]$cat /web/vhosts/www1/index.html
welcome to www1
thank you
[root@host conf.d]$cat /web/vhosts/www2/index.html
welcome to www2
thank you

重载httpd配置文件

[root@host conf.d]$httpd -t
Syntax OK
[root@host conf.d]$systemctl reload httpd.service

修改客户端主机的hosts文件,以便能解析域名

hosts在windows环境下的路径为C:\Windows\System32\drivers\etc。在该文件中添加两行

192.168.55.128 www1.stuX.com
192.168.55.128 www2.stuX.com

访问结果

CentOS7配置httpd虚拟主机

图1  访问www1站点

CentOS7配置httpd虚拟主机

图2  访问www2站点

CentOS7配置httpd虚拟主机

图3  查看www1站点的访问状态——正常

CentOS7配置httpd虚拟主机

图4  查看www2站点的访问状态错误