1. 基于域名的虚拟主机

  2. server1:172.25.254.1     主机:172.25.254.45(以下都在虚拟机server1上进行)

    首先下载http

    #yum  install  httpd  httpd-manual   -y

    #setenforce  0   临时修改selinux安全

    (1)开启httpd服务,关闭防火墙

     # netstat -antlp | grep httpd     查看80端口是否开启

    #ps -ax    (httpd开启6个进程,1个主进程,5个闲置进程)

    #semanage  fcontext  -l  |grep httpd(查看httpd上下文)

    # mkdir /srv/huipenggang/www  -p

    # mkidr /srv/hpg/www  -p   (两个发布目录,需要说明的是  /srv/xxx/www这个目录的下面的上下文都市 httpd_sys_content_t:s0 ,使用时,只需要用 restorecon  -vv 刷新就好,使用其他目录也可以,要用 chcon  -t   httpd_sys_content_t 修改上下文)

    #echo  huipenggang  > /srv/huipenggang/www/index.html

    #echo hpg > /srv/hpg/www/index.html

    #restorecon  -vvFR /srv (刷新 /srv目录下面的所有上下文)

    #vim  /etc/httpd/conf.d/huipenggang.conf

    <virtualHost   * ::80>

               ServerName  huipenggang.example.com

                ServerAlias   huipenggang

                 DocumentRoot   /srv/huipenggang/www

                customlog  "logs/huipenggang.log"   combained

    </VirtualHost>

    <directory "/srv/huipenggang/www">

                   require all granted

    </directory>

    #cp  /etc/httpd/congf.d/huipenggang.conf     /etc/httpd/conf.d/hpg.conf

    #sed -i 's/huipenggang/hpg/g'   /etc/httpd/conf.d/hpg.conf   (修改)

    #systemctl restart httpd.service  刷新httpd服务

    下来再主机上加上解析

    #echo    172.25.254.1    huipengang.example.com    huipenggang    hpg.example.com   hpd >>/etc/hosts

下来再主机上火狐访问  huipenggang.example.com  或者 huipenggang 时 都会出现 huipengang

访问 hpg.example.com 或hpg  会出现hpg

######此时出现一个问题,访问server1时会出现huipengang ,因为server端没有设置default:

  #vim   /etc/httpd/conf/httpd.coonf

<Virtualhost  _default_:80>

                    documentroot  /var/www/html

</virtualhost>                         (添加的内容,完成后记得刷新服务,这样就好啦)

 

 

 

 

 

2基于IP的虚拟主机  (把上面的huipenggang  hpg   换成ip就好了)

首先给网卡配置多个网络接口:

#ifconfig   eth0:1   172.25.254.2/24

#  ifconfig   eth0:2  172.25.254.3/24

#cd    /etc/httpd/conf.d

#vim huipengang.conf

<VIrtualhost    172.25.254.2:80>

                ServerName   172.25.254.2

                DocumentRoot   /srv/huipengngang/www

                customlog "logs/huipenggng.log"   combined

</VirtualHost>

<directory   "/srv/huipengang/www">

            require  all  granted

<directory>

hpg.conf   里面的东西只需要修改huipenggang。conf里面的ip为172.25.254.3就好了·

刷新httpd服务

在火狐上访问就好了