CentOS7配置httpd虛擬主機的操作步驟:1、查看CentOS7系統版本和httpd版本;2、輸入“systemctl start httpd.service ”命令啟動httpd;3、使用curl命令訪問虛擬主機;4、借助mkdir命令創建指定文件目錄;5、打開vir.conf配置文件,根據要求填寫虛擬主機配置信息;6、創建兩個web測試頁面;7、重載httpd配置文件使修改生效;8、 修改客戶端主機的hosts文件,讓主機能夠解析域名;9、在瀏覽器中訪問web測試頁面即可查看結果。
本實驗旨在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 -vserver version: apache/2.4.6 (centos)
server built: nov 14 2016 18:04:44
[root@host ~]$cat /etc/centos-release
centos linux release 7.3.1611 (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 2017-06-01 03:03:12 cst; 5s ago # active 表示正常運行
docs: man:httpd(8)
man:apachectl(8)
process: 6473 execstop=/bin/kill -winch ${mainpid} (code=exited, status=0/success)
main pid: 6485 (httpd)
status: "processing requests..."
cgroup: /system.slice/httpd.service
├─6485 /usr/sbin/httpd -dforeground
├─6486 /usr/sbin/httpd -dforeground
├─6487 /usr/sbin/httpd -dforeground
├─6489 /usr/sbin/httpd -dforeground
├─6490 /usr/sbin/httpd -dforeground
└─6572 /usr/sbin/httpd -dforeground
jun 01 03:03:11 host systemd[1]: starting the apache http server...
jun 01 03:03:12 host systemd[1]: started the apache http server.
使用curl命令訪問
[root@host ~]$ip a show ens38 # 查看ip3: ens38: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state up qlen 1000
link/ether 00:0c:29:dc:18:5f brd ff:ff:ff:ff:ff:ff
inet 192.168.55.128/24 brd 192.168.55.255 scope global dynamic ens38
valid_lft 1752sec preferred_lft 1752sec
inet6 fe80::20c:29ff:fedc:185f/64 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:80>
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.1
</requireall>
</directory>
</virtualhost>
# virtual host 2 # 虛擬主機2的配置
<virtualhost 192.168.55.128:80>
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.htmlwelcome 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 -tsyntax 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
訪問結果
圖1、訪問www1站點
圖2、訪問www2站點
圖3、查看www1站點的訪問狀態——正常
圖4、查看www2站點的訪問狀態錯誤