CentOS7如何配置httpd虚拟主机

admin3年前云服务器75

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-v

serverversion:apache/2.4.6(centos)

serverbuilt:nov14201618:04:44

[root@host~]$cat/etc/centos-release

centoslinuxrelease7.3.1611(core)

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

[root@host~]$systemctlstarthttpd.service

[root@host~]$systemctlstatushttpd.service

●httpd.service-theapachehttpserver

loaded:loaded(/usr/lib/systemd/system/httpd.service;enabled;vendorpreset:disabled)

active:active(running)sincethu2017-06-0103:03:12cst;5sago#active表示正常运行

docs:man:httpd(8)

man:apachectl(8)

process:6473execstop=/bin/kill-winch${mainpid}(code=exited,status=0/success)

mainpid:6485(httpd)

status:"processingrequests..."

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

jun0103:03:11hostsystemd[1]:startingtheapachehttpserver...

jun0103:03:12hostsystemd[1]:startedtheapachehttpserver.

使用curl命令访问

[root@host~]$ipashowens38#查看ip

3:ens38:<broadcast,multicast,up,lower_up>mtu1500qdiscpfifo_faststateupqlen1000

link/ether00:0c:29:dc:18:5fbrdff:ff:ff:ff:ff:ff

inet192.168.55.128/24brd192.168.55.255scopeglobaldynamicens38

valid_lft1752secpreferred_lft1752sec

inet6fe80::20c:29ff:fedc:185f/64scopelink

valid_lftforeverpreferred_lftforever

[root@host~]$curlhttp://192.168.55.128#访问

<!doctype>

<h1>

centos7.3

</h1>

创建指定文件目录

[root@hostconf.d]$mkdir-pv/web/vhosts/www1

[root@hostconf.d]$mkdir-pv/web/vhosts/www2

[root@hostconf.d]$mkdir-pv/var/log/httpd/www2

[root@hostconf.d]$mkdir-pv/var/log/httpd/www1

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

#path/etc/httpd/conf.d/vir.conf#配置文件全路径

#virtualhost1  #虚拟主机1的配置

<virtualhost192.168.55.128:80>

errorlog"/var/log/httpd/www1/error_log"

customlog"/var/log/httpd/www1/access_log"combined

<location/server-status>

sethandlerserver-status

</location>

<directory/web/vhosts/www1>

<requireall>

requireallgranted

requirenotip192.168.1

</requireall>

</directory>

</virtualhost>

#virtualhost2#虚拟主机2的配置

<virtualhost192.168.55.128:80>

servernamewww2.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>

  requireallgranted

</requireall>

</directory>

</virtualhost>

创建www1和www2的index页面

[root@hostconf.d]$cat/web/vhosts/www1/index.html

welcometowww1

thankyou

[root@hostconf.d]$cat/web/vhosts/www2/index.html

welcometowww2

thankyou

重载httpd配置文件

[root@hostconf.d]$httpd-t

syntaxok

[root@hostconf.d]$systemctlreloadhttpd.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站点的访问状态错误

免责声明:本文内容来自用户上传并发布,站点仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请核实广告和内容真实性,谨慎使用。

相关文章

外汇交易金融平台部署在香港服务器有什么优势

外汇交易金融平台部署在香港服务器的优势:1. 香港作为国际金融中心,国际投行和外汇交易所在香港都有办事处,香港地区的外汇交易网络专线直连英国伦敦交易中心和美国纽约交易中心,网络延迟低,能够确保投资者能...

国外虚拟主机租用能放多少个网站

国外虚拟主机租用能放的网站数量要看:1、国外虚拟主机的配置,配置越高,能放的网站数量越多;2、网站日常流量,如高流量网站所消耗的带宽量多,可建站的数量少;3、业务拓展情况,如果网站业务快速增长,会导致...

美国外贸服务器的缺点有哪些

美国外贸服务器的缺点有:1、美国外贸服务器机房不做特殊服务;2、美国外贸服务器不做系统内部服务,服务器出现问题,需要自己做技术处理,如破解密码等;3、美国外贸服务器不为大陆出口负责;4、美国外贸服务器...

美国服务器用于查询的系统命令有哪些

美国服务器用于查询的系统命令有:1、find命令,主要用来查找文件;2、grep命令,主要用来在特定文件夹下的文件查找字符串;3、which命令,主要用来在环境变量指定的路径中查找文件;4、where...

使用云服务器有哪些好处

使用云服务器的好处:1.通过云服务器,可以自动完成此操作,使您的公司更安全、更高效。2. 使用云服务器允许您在任何地方工作,可以通过移动设备和台式计算机访问软件和文档,提供更大的灵活性。3. 借助云服...

选择美国云主机有哪些优势

选择美国云主机的优势有:1、美国云主机机房设置在美国,线路故障、人为破坏等情况发生的几率小,能有效保证云主机外部因素的稳定性;2、美国云主机不用备案,能为用户省去繁琐的备案过程,节省用户的时间,实现快...