码迷,mamicode.com
首页 > Web开发 > 详细

基于CentOS6.6配置Apache虚拟主机(多IP、多主机、多端口)

时间:2019-11-02 17:34:18      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:system   环境   off   yum   httpd服务   led   loopback   serve   sys   

Apache配置虚拟主机(多IP、多主机、多端口)
本文以CentOS6.6 作服务端,Win7作客户端。参考网上诸多文档加以实验,现总结如下。如有问题,有意还请指出。谢谢!


[一 基于多IP地址]
实验:Server=CentOS6.6,Client=win7。我以root权限直接操作了。
1.安装和起服务httpd(即:Apache)
[root@localhost ~]# yum install httpd -y    <==yum安装httpd
[root@localhost ~]# /etc/init.d/httpd start    <==临时启动服务,或# service httpd start

2.关闭iptables防火墙
[root@localhost ~]# /etc/init.d/iptables stop    <==临时关闭。或永久关闭chkconfig iptables off

3.创建访问路径文件夹和主页文件
[root@localhost /]# makdir /var/www/test1    <==创建自定义文件夹"test1"
[root@localhost /]# echo test1>/var/www/test1/1.html    <==创建自定义主页"1.html"
[root@localhost /]# makdir /var/www/test2
[root@localhost /]# echo test2>/var/www/test2/2.html
[root@localhost /]# makdir /var/www/test3
[root@localhost /]# echo test3>/var/www/test3/3.html

4.在现有网口上临时添加多个IP地址(重启网卡后失效)
[root@localhost /]# ifconfig eth0:1 192.168.214.142    <==添加自定义IP"142"
[root@localhost /]# ifconfig eth0:2 192.168.214.143
[root@localhost /]# ifconfig eth0:3 192.168.214.144
[root@localhost /]# ip a    <==查看IP信息
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ea:8a:71 brd ff:ff:ff:ff:ff:ff
    inet 192.168.214.140/24 brd 192.168.214.255 scope global eth0    <==本地IP140
    inet 192.168.214.142/24 brd 192.168.214.255 scope global secondary eth0:1    <==已添加142
    inet 192.168.214.143/24 brd 192.168.214.255 scope global secondary eth0:2
    inet 192.168.214.144/24 brd 192.168.214.255 scope global secondary eth0:3
    inet6 fe80::20c:29ff:feea:8a71/64 scope link
       valid_lft forever preferred_lft forever

5.添加或修改hosts文件
[root@localhost /]# vi /etc/hosts/    <==编辑hosts文件
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.214.142 www.test1.com    <==添加IP对应域名
192.168.214.143 www.test2.com
192.168.214.144 www.test3.com

6.添加httpd.conf配置文件(下面内容需要手工添加)
[root@localhost /]# vi /etc/httpd/conf/httpd.conf
<VirtualHost 192.168.214.142:80>
  serverName www.test1.com    <==自定义域名地址
  DirectoryIndex 1.html    <==如前面未创建"1.html",而是"index.html"则不用添加此条.
  DocumentRoot /var/www/test1/    <==域名访问文件夹路径
  <Directory "/var/www/test1">    <==域名访问文件夹路径
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow From All
  </Directory>
</VirtualHost>

<VirtualHost 192.168.214.143:80>
  serverName www.test2.com
  DirectoryIndex 2.html
  DocumentRoot /var/www/test2/
  <Directory "/var/www/test2">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow From All
  </Directory>
</VirtualHost>

<VirtualHost 192.168.214.144:80>
  serverName www.test3.com
  DirectoryIndex 3.html
  DocumentRoot /var/www/test3/
  <Directory "/var/www/test3">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow From All
  </Directory>
</VirtualHost>

7.重启httpd服务
[root@localhost /]# /etc/init.d/httpd restart    <==或# service httpd restart

8.Win7添加hosts(c:\windows\system32\drivers\etc\hosts)
192.168.214.142 www.test1.com    <==Win7添加IP对应域名
192.168.214.143 www.test2.com
192.168.214.144 www.test3.com

9.Win7浏览器访问(建议在虚拟环境,DNS断开下测试)
http://www.test1.com
http://www.test2.com
http://www.test3.com


[二 基于多主机名]
实验:Server=CentOS6.6,Client=win7。我以root权限直接操作了。
1.安装和起服务httpd(即:Apache)
[root@localhost ~]# yum -y install httpd    <==yum安装httpd
[root@localhost ~]# /etc/init.d/httpd start    <==临时启动服务,或# service httpd start

2.关闭iptables防火墙
[root@localhost ~]# /etc/init.d/iptables stop    <==临时关闭。或永久关闭chkconfig iptables off

3.创建访问路径文件夹和主页文件
[root@localhost /]# makdir /var/www/test1    <==创建自定义文件夹"test1"
[root@localhost /]# echo test1>/var/www/test1/1.html    <==创建自定义主页"1.html"
[root@localhost /]# makdir /var/www/test2
[root@localhost /]# echo test2>/var/www/test2/2.html
[root@localhost /]# makdir /var/www/test3
[root@localhost /]# echo test3>/var/www/test3/3.html

4.添加或修改hosts文件
[root@localhost /]# vi /etc/hosts/
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.214.140 www.test1.com    <==添加IP对应域名
192.168.214.140 www.test2.com
192.168.214.140 www.test3.com

5.添加或修改httpd.conf配置文件(下面内容需要手工添加)

[root@localhost /]# vi /etc/httpd/conf/httpd.conf
NameVirtualHost *:80
<VirtualHost *:80>
  ServerName *
  DocumentRoot /www/
</VirtualHost>

<VirtualHost *:80>
  ServerName www.test1.com    <==自定义域名地址
  DocumentRoot /var/www/test1    <==域名访问文件夹路径
  DirectoryIndex 1.html        <==如前面未创建"1.html",而是"index.html"则不用添加此条.
  <Directory "/var/www/test1">    <==域名访问文件夹路径
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName www.test2.com
  DocumentRoot /var/www/test2/
  DirectoryIndex 2.html
  <Directory "/var/www/test2">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName www.test3.com
  DocumentRoot /var/www/test3/
  DirectoryIndex 3.html
  <Directory "/var/www/test3">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

6.重启httpd服务
[root@localhost /]# /etc/init.d/httpd restart    <==或service httpd restart

7.Win7添加hosts(c:\windows\system32\drivers\etc\hosts)
192.168.214.142 www.test1.com    <==Win7添加IP对应域名
192.168.214.143 www.test2.com
192.168.214.144 www.test3.com

8.Win7浏览器访问(建议在虚拟环境,DNS断开下测试)
http://www.test1.com
http://www.test2.com
http://www.test3.com


[三 基于多端口]
实验:Server=CentOS6.6,Client=win7。我以root权限直接操作了。
1.安装和起服务httpd(即:Apache)
[root@localhost ~]# yum -y install httpd    <==yum安装httpd
[root@localhost ~]# /etc/init.d/httpd start    <==临时启动服务,或# service httpd start

2.关闭iptables防火墙和SELINUX
关闭iptables服务
[root@localhost ~]# /etc/init.d/iptables stop    <==临时关闭。或永久关闭chkconfig iptables off

永久关闭SELINUX
[root@localhost ~]# vi /etc/selinux/config    <==修改SELINUX配置文件
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled    <==将默认"enforcing"修改为"disabled"关闭。此处注释掉无效,需修改
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost ~]# sestatus    <==查看SELINUX状态
SELinux status:                 disabled    <==显示为关闭

3.创建访问路径文件夹和主页文件
[root@localhost /]# makdir /var/www/test1    <==创建自定义文件夹"test1"
[root@localhost /]# echo test1>/var/www/test1/1.html    <==创建自定义主页"1.html"
[root@localhost /]# makdir /var/www/test2
[root@localhost /]# echo test2>/var/www/test2/2.html
[root@localhost /]# makdir /var/www/test3
[root@localhost /]# echo test3>/var/www/test3/3.html
 
4.添加或修改httpd.conf配置文件(下面内容需要手工添加)

[root@localhost /]# vi /etc/httpd/conf/httpd.conf
Listen 80    <==此条为默认
Listen 8080    <==此条为手工添加
Listen 8081    <==此条为手工添加

<VirtualHost 192.168.214.140:80>    <==设定IP地址对应80端口
  ServerName www.test1.com    <==自定义域名地址
  DocumentRoot /var/www/test1/    <==域名访问文件夹路径
  DirectoryIndex 1.html        <==如前面未创建"1.html",而是"index.html"则不用添加此条.
</VirtualHost>

<VirtualHost 192.168.214.140:8080>
  Servername www.test2.com
  DocumentRoot /var/www/test2/
  DirectoryIndex 2.html
</VirtualHost>

<VirtualHost 192.168.214.140:8081>
  Servername www.test3.com
  DocumentRoot /var/www/test3/
  DirectoryIndex 3.html
</VirtualHost>

5.重启httpd服务
[root@localhost /]# /etc/init.d/httpd restart    <==或service httpd restart

6.Win7添加hosts(c:\windows\system32\drivers\etc\hosts)
192.168.214.140 www.test1.com    <==Win7添加IP对应域名
192.168.214.140 www.test2.com
192.168.214.140 www.test3.com

7.客户端网址访问即可看到实验结果(建议在虚拟环境,DNS断开下测试)
http://www.test1.com    <==访问时默认即为80端口
http://www.test2.com:8080
http://www.test3.com:8081

基于CentOS6.6配置Apache虚拟主机(多IP、多主机、多端口)

标签:system   环境   off   yum   httpd服务   led   loopback   serve   sys   

原文地址:https://www.cnblogs.com/qf1420/p/11783031.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!