国内服务器
亚洲服务器
欧洲服务器
北美洲服务器
南美洲服务器
大洋洲服务器
非洲服务器

首页>>建站

CentOS7如何配置httpd虚拟主机

发表时间:2022-08-30 10:01:35

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

server 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  # 查看ip 

3: 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.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:windowssystem32driversetc。在该文件中添加两行

192.168.55.128 www1.stux.com
192.168.55.128 www2.stux.com

访问结果

图1、访问www1站点

图2、访问www2站点

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

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


上一篇 下一篇
最新文章

如何提高云服务器的安全系数

香港独立IP空间有什么优势

香港空间影响百度收录吗

为啥要租用美国VPS

VPS的缺点有哪些

香港vps作用在哪里

VPS的优点有哪些

外贸网站为啥选择美国vps

VPS能建多少个网站

VPS要如何选择位置

相关文章

Linux系统如何修改主机名

使用国外vps建站好处有哪些

海外互联网骨干网互联

香港不VPS服务器的优势与利用场景

服务器托管新加坡:优良服务与创新科技的完善结合

不要钱空间网站打不开有什么原因

SDWAN国际化组网

影响柬埔寨云主机价格的因素有哪些?

如何在云主机上搭建堡垒机?搭建堡垒机需要多长时间?

国内服务器租用问题集锦

X

截屏,微信识别二维码

微信号:muhuanidc

(点击微信号复制,添加好友)

打开微信

微信号已复制,请打开微信添加咨询详情!