Fork me on GitHub

Apache虚拟主机

一个IP地址如何绑定多个域名?


测试环境与用例准备

1
2
测试环境:
Ubuntu 14.04 LTS x64
1
2
3
4
测试用例:
主机IP:192.168.20.130
域名1配置:domain1.local
域名2配置:domain2.local

Apache2安装

  • 更新软件源
1
sudo apt-get update
  • 安装Apache2服务器
1
sudo apt-get install apache2

配置虚拟主机站点

  • 分别创建域名站点目录
1
2
3
4
5
6
/**
* domain1_site存放站点一
* domain2_site存放站点二
*/
sudo mkdir -p /var/www/domain1_site
sudo mkdir -p /var/www/domain2_site
  • 分别创建站点
1
2
3
4
/**
* 创建domain1站点
*/
sudo vi /var/www/domain1_site/index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* domain1站点内容
*/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>www.domain1.local</title>
</head>
<body>
This is www.domain1.local!
</body>
</html>
1
2
3
4
5
/**
* 创建domain2站点
*/
sudo cp /var/www/domain1_site/index.html /var/www/domain2_site/index.html
sudo vim /var/www/domain2_site/index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* domain2站点内容
*/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>www.domain2.local</title>
</head>
<body>
This is www.domain2.local!
</body>
</html>

创建虚拟主机配置文件

  • apache有一个默认的虚拟主机文件000-default.conf,以此文件为模版进行修改
  • domain1配置文件
1
2
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/domain1.local.conf
sudo vim /etc/apache2/sites-available/domain1.local.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* #为注释部分
* 需要修改地方:
* ServerAdmin:管理员邮箱
* DocumentRoot:站点地址
* ServerName:域名(如果有多个用空格隔开)
*/
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/domain1_site
ServerName domain1.local

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
  • domain2配置文件
1
2
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/domain2.local.conf
sudo vim /etc/apache2/sites-available/domain2.local.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* #为注释部分
* 需要修改地方:
* ServerAdmin:管理员邮箱
* DocumentRoot:站点地址
* ServerName:域名(如果有多个用空格隔开)
*/
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/domain2_site
ServerName domain2.local

# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

启用配置信息

  • 修改虚拟主机文件后,禁用默认的虚拟主机配置,然后启用新的虚拟主机配置
1
2
3
4
5
6
7
8
//禁用默认的虚拟主机配置
sudo a2dissite 000-default.conf

//启用domain1的配置
sudo a2ensite domain1.local.conf

//启用domain2的配置
sudo a2ensite domain2.local.conf
  • 重启apache服务器
1
sudo service apache2 restart
  • 修改hosts文件
1
sudo vim /etc/hosts
1
2
3
//添加如下内容
192.168.20.130 domain1.local
192.168.20.130 domain2.local

测试配置的虚拟主机


转载请注明出处


Thank you for your support.