lichaoyue888 发表于 2018-11-19 08:40:00

Apache 配置虚拟主机之2

   
[*]基于 name-based   
[*]基于 IP   
[*]基于 port   
[*]结合
二、基于IP
  配置hosts,httpd.conf,eno
  1、假设服务器已有IP为192.168.136.140,使用ifconfig在同一个网络接口eno16777736(或者eth0)绑定另外2个IP,或者同 nmtui 配置。可以进行ping探测。
# ifconfig eno16777736:192.168.136.143
# ifconfig eno16777736:192.168.136.144


2、修改/etc/hosts文件,添加一一对应的域名

# IP-Based
192.168.136.143    www3.example.com
1921.68.136.144    www4.example.com

  3、建立虚拟主机存放网页的根目录

# /var/www 目录结构
# tree
.
├── cgi-bin
├── html
├── logs
│   ├── www1
│   │   ├── access_log
│   │   └── error_log
│   ├── www2
│   │   ├── access_log
│   │   └── error_log
│   ├── www3
│   │   ├── access_log
│   │   └── error_log
│   └── www4
│   ├── access_log
│   └── error_log
└── vhosts
├── www1
│   └── index.html
├── www2
│   └── index.html
├── www3
│   └── index.html
└── www4
└── index.html

  index.html内容分别如下


# cat index.html ../www4/index.html
Holle world!
Welcom to www3.example.com site with IP-Based.
Holle world!
Welcom to www4.example.com site with IP-Based.

  4、在httpd.conf中将附加配置文件httpd-vhosts.conf包含进来,接着修改httpd-vhosts.conf配置

# IP-Based

ServerAdmin webmaster@www1.example.com
ServerName www3.example.com
DocumentRoot "/var/www/vhosts/www3"
ErrorLog "/var/www/logs/www1/error_log"
CustomLog "/var/www/logs/www1/access_log" combined


ServerAdmin webmaster@www4.example.com
ServerName www4.example.com
DocumentRoot "/var/www/vhosts/www4"
ErrorLog "/var/www/logs/www4/error_log"
CustomLog "/var/www/logs/www4/access_log" combined


  5、配置语法检查 httpd -S,可看到Name-Based和IP-Based共存。

# httpd -S
VirtualHost configuration:
192.168.136.143:80   www3.example.com (/etc/httpd/conf/httpd.conf:98)
192.168.136.144:80   www4.example.com (/etc/httpd/conf/httpd.conf:106)
*:80                   is a NameVirtualHost
default server www1.example.com (/etc/httpd/conf/httpd.conf:82)
port 80 namevhost www1.example.com (/etc/httpd/conf/httpd.conf:82)
alias example1.com
port 80 namevhost www2.example.com (/etc/httpd/conf/httpd.conf:89)
ServerRoot: "/etc/httpd"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/etc/httpd/logs/error_log"
Mutex authn-socache: using_defaults
Mutex default: dir="/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex authdigest-opaque: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
Mutex authdigest-client: using_defaults
Mutex proxy: using_defaults
PidFile: "/run/httpd/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="apache" id=48
Group: name="apache" id=48

  6、重启服务 systemctl restart httpd.service,然后访问4个站点。

# curl www1.example.comwww2.example.com www3.example.com www4.example.com
Holle world!
welcome to www1.example.com site with Name-Based.
Holle world!
welcome to www2.example.com site with Name-Based.
Holle world!
Welcom to www3.example.com site with IP-Based.
Holle world!
Welcom to www4.example.com site with IP-Based.


# 通过IP或者域名访问3,4
# curl www3.example.com192.168.136.143:80www4.example.com 192.168.136.144:80
Holle world!
Welcom to www3.example.com site with IP-Based.
Holle world!
Welcom to www3.example.com site with IP-Based.
Holle world!
Welcom to www4.example.com site with IP-Based.
Holle world!
Welcom to www4.example.com site with IP-Based.

  至此已完成了IP-Based的配置。
  附:http://httpd.apache.org/docs/2.4/vhosts/ip-based.html

  问题:用户如何配置和管理。



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