apache httpd的安装和虚拟主机配置(基于centos 7)

虚拟主机——在一台www服务器上运行多个网站

前提条件

  • 正确安装httpd
    • 网络上安装,
      1. 正确设置yum源
      2. 检查包:yum list |grep httpd
      3. 安装:yum install httpd
      4. 查看服务:service httpd status
      5. 验证:本机打开浏览器,地址栏输入[主机IP],若有返回即表示安装成功(注意:防火墙在关闭状态下或者添加相应端口)
      6. 启动等:service httpd start/stop/status/restart
  • 主机IP:192.168.0.225
  • 防火墙关闭(或者已经开放相应端口)
  • 分别在www目录下新建两个目录:mkdir god.com hell.com,并分别在god.com和hell.com的目录下新建index.html文件如下

god.com目录下的index.html

<html>
<head>hell</head>
<br></br>
<body>
this is hell area
</body>
</html>

hell.com目录下的index.html

<html>
<head>hell</head>
<br></br>
<body>
this is hell area
</body>
</html>

 

配置虚拟主机

两种方式:基于IP和基于主机名

  • 基于IP

  1、基于同IP不同端口(在默认的配置前提下:已有80端口)

    vim /etc/httpd/conf/httpd.conf

      添加监听端口:listen 8080

      注释ServerAdmin:#ServerAdmin root@localhost

      注释documentRoot:#DocumentRoot "/var/www/html"

      在配置文件最后添加以下内容:

NameVirtualHost *:80

<VirtualHost 192.168.0.225:80>
        ServerAdmin admin@god.com
        DocumentRoot /var/www/god.com
        Servername www.god.com
        errorlog logs/god.com-error_log
        customlog logs/god.com-access_log common
</VirtualHost>
<VirtualHost 192.168.0.225:8080>
        ServerAdmin admin@hell.com
        DocumentRoot /var/www/hell.com
        Servername www.hell.com
        errorlog logs/hell.com-error_log
        customlog logs/hell.com-access_log common
</VirtualHost>

 

  验证:分别访问192.168.0.225和192.168.0.225:8080,结果如下:

 


 

    2、基于同端口不同IP(同1,针对有两个IP的服务器,配置中只是IP不同)

  • 基于主机名(ing)

    注释ServerAdmin:#ServerAdmin root@localhost

    注释documentRoot:#DocumentRoot "/var/www/html"

    在配置文件最后添加以下内容:

NameVirtualHost *:80
<VirtualHost *:80>
        ServerAdmin admin@god.com
        DocumentRoot /var/www/god.com
        Servername www.god.com
        errorlog logs/god.com-error_log
        customlog logs/god.com-access_log common
</VirtualHost>

<VirtualHost *:80>
        ServerAdmin admin@hell.com
        DocumentRoot /var/www/hell.com
        Servername www.hell.com
        errorlog logs/hell.com-error_log
        customlog logs/hell.com-access_log common
</VirtualHost>

测试:在本机(win 10)中做如下配置:

  1、编辑文件:C:\Windows\System32\drivers\etc\hosts,加入如下两条数据

192.168.0.225 www.god.com
192.168.0.225 www.hell.com

  2、打开浏览器,输入以上域名,出现如下结果:大功告成

 

重启httpd

service httpd start/stop/restart/status

设置开机启动

1、编辑文件/etc/rc.d/rc.local,添加如下内容:

  /usr/sbin/apachectl start

2、执行如下命令:

  chmod +x rc.local

3、重启测试

 


完事了才发现的一篇文章:http://www.cnblogs.com/hi-bazinga/archive/2012/04/23/2466605.html

 

 

转载于:https://www.cnblogs.com/feer/p/9837439.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值