gaoke 发表于 2018-11-17 13:09:32

Apache虚拟主机配置详解

  Apache虚拟主机配置详解
1、配置环境说明
  系统环境:CentOS7
  Apache环境:编译安装的httpd-2.4.7
  系统限制:关闭了防火墙和selinux
  hosts文件中配置以下域名解析
  192.168.115.150    www.web1.com
  192.168.115.150    www.web2.com
2、Apache虚拟主机简介
  虚拟主机是指在一个机器上运行多个网站(例如:www.web1.com和www.web2.com)。每个站点希望用不同的域名和站点目录,或者是不同端口、不同的IP就需要虚拟主机。
3、Apache虚拟主机的分类
  (1)基于IP地址的虚拟主机:一台服务器上使用不同IP地址对应不同站点
  (2)基于域名的虚拟主机:一台服务器上使用不同的域名对应不同站点
  (3)基于端口的虚拟主机:一台服务器上使用不同的端口对应不同站点
4、Apache基于IP的虚拟主机配置方法
(1)创建web1和web2站点的目录和网页文件
  # mkdir -p /www/web{1..2}/logs/
  #echo 'www.web1.com'>/www/web1/index.html
  # echo 'www.web2.com'>/www/web2/index.html
  # tree /www
  /www
  ├── web1
  │   ├── index.html
  │   └── logs
  └── web2
  ├── index.html
  └── logs
(2)修改Apache相关配置文件
  # vim /usr/local/apache2/conf/httpd.conf
  Include conf/extra/httpd-vhosts.conf   删除这一行的注释
  # vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
  
  DocumentRoot "/www/web1"
  ServerName www.web1.com
  ServerAlias web1.com
  ErrorLog "/www/web1/logs/web1_error.log"
  CustomLog "/www/web1/logs/web1_access.log" common
  
  Options Indexes
  AllowOverride None
  Require all granted
  
  
  
  DocumentRoot "/www/web2"
  ServerName www.web2.com
  ErrorLog "/www/web2/logs/web2_error.log"
  CustomLog "/www/web2/logs/web2_access.log" common
  
  Options Indexes
  AllowOverride None
  Require all granted
  
  
  # /usr/local/apache2/bin/apachectl -t   检查语法
  Syntax OK
  # systemctl restart httpd      重启服务
(3)浏览器或Linux中进行访问测试
  为了方便我这里直接在Linux中测试了
  # curl 192.168.115.150
  www.web1.com
  # curl 192.168.115.160
  www.web2.com
5、Apache虚拟主机基于域名配置方法
(1)创建web1和web2站点的目录和网页文件
  # mkdir -p /www/web{1..2}/logs/
  #echo 'www.web1.com'>/www/web1/index.html
  # echo 'www.web2.com'>/www/web2/index.html
  # tree /www
  /www
  ├── web1
  │   ├── index.html
  │   └── logs
  └── web2
  ├── index.html
  └── logs
(2)修改Apache相关配置文件
  # vim /usr/local/apache2/conf/httpd.conf
  Include conf/extra/httpd-vhosts.conf   删除这一行的注释
  # vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
  
  DocumentRoot "/www/web1"
  ServerName www.web1.com
  ServerAlias web1.com
  ErrorLog "/www/web1/logs/web1_error.log"
  CustomLog "/www/web1/logs/web1_access.log" common
  
  Options Indexes
  AllowOverride None
  Require all granted
  
  
  
  DocumentRoot "/www/web2"
  ServerName www.web2.com
  ErrorLog "/www/web2/logs/web2_error.log"
  CustomLog "/www/web2/logs/web2_access.log" common
  
  Options Indexes
  AllowOverride None
  Require all granted
  
  
  # systemctl restart httpd
(3)浏览器或Linux中进行访问测试
  为了方便我这里直接在Linux中测试了
  # curl www.web1.com
  www.web1.com
  # curl www.web2.com
  www.web2.com
6、Apache虚拟主机基于端口配置方法
(1)创建web1和web2站点的目录和网页文件
  # mkdir -p /www/web{1..2}/logs/
  #echo 'www.web1.com'>/www/web1/index.html
  # echo 'www.web2.com'>/www/web2/index.html
  # tree /www
  /www
  ├── web1
  │   ├── index.html
  │   └── logs
  └── web2
  ├── index.html
  └── logs
(2)修改Apache相关配置文件
  # vim /usr/local/apache2/conf/httpd.conf
  Include conf/extra/httpd-vhosts.conf   删除这一行的注释
  Listen 80      添加监听的端口
  Listen 8080
  # vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
  
  DocumentRoot "/www/web1"
  ServerName www.web1.com
  ServerAlias web1.com
  ErrorLog "/www/web1/logs/web1_error.log"
  CustomLog "/www/web1/logs/web1_access.log" common
  
  Options Indexes
  AllowOverride None
  Require all granted
  
  
  
  DocumentRoot "/www/web2"
  ServerName www.web2.com
  ErrorLog "/www/web2/logs/web2_error.log"
  CustomLog "/www/web2/logs/web2_access.log" common
  
  Options Indexes
  AllowOverride None
  Require all granted
  
  
  # systemctl restart httpd
(3)浏览器或Linux中进行访问测试
  为了方便我这里直接在Linux中测试了
  # curl 192.168.115.150:80
  www.web1.com
  # curl 192.168.115.150:8080
  www.web2.com
  以上就是Apache虚拟主机配置的三种不同方法,可以根据实际需要进行配置。

页: [1]
查看完整版本: Apache虚拟主机配置详解