专业网站建设B/S软件开发专业微信营销专业技术建设团队做客户满意度最高的软件公司
郑州网站建设、郑州网站开发
 

Web服务器(Apache)虚拟主机的配置

作者:网站建设出处:学众科技发布时间:2020年05月18日点击数:815

一。定义

所谓虚拟主机是指在一台服务器里运行几个网站,提供WEB、FTP、Mail等服务。

二。虚拟主机的实现方法有三种:

基于IP的方法,基于主机名的方法和基于端口的法官法。

①基于IP的方法:

在服务器里绑定多个IP,然后配置WEB服务器,把多个网站绑定在不同的IP上。访问不同的IP,就看到不同的网站。

②基于端口的方法:

一个IP地址,通过不同的端口实在不同网站的访问。

③基于主机名的方法:

设置多个域名的A记录,使它们解析到同一个IP地址上,即同一个服务器上。然后,在服务器上配置WEB服务端,添加多个网站,为每个网站设定一个主机名。因为HTTP协议访问请求里包含有主机名信息,当WEB服务器收到访问请求时,就可以根据不同的主机名来访问不同的网站。

三。三种虚拟主机实现的基本配置

①基于IP虚拟主机的实现:

多个ip,需要把中心主机取消

打开web服务的主配置文档:vim /etc/httpd/conf/httpd.conf

DocumentRoot 注释掉

配置虚拟主机:

DocumentRoot "/www/a.com"

ServerName www.a.com

DocumentRoot "/www/b.com"

ServerName www.b.com

vim /etc/hosts

192.168.0.20 www.a.com

192.168.0.25 www.b.com

浏览器中输入IP地址进行实验效果的验证。

②基于端口:

DocumentRoot "/www/a.com"

ServerName www.a.com

DocumentRoot "/www/b.com"

ServerName www.b.com

③基于主机名:

开启:NameVirtualHost 192.168.0.20:80

ServerAdmin www.a.com

DocumentRoot /etc/httpd/aaa/a.com

ServerName dummy-host.example.com

ErrorLog logs/dummy-host.example.com-error_log

CustomLog logs/dummy-host.example.com-access_log common

ServerAdmin www.b.com

DocumentRoot /etc/httpd/aaa/b.com

四。案例综合实现

建立http服务器,要求:

1)提供两个基于名称的虚拟主机:

(a)www1.ilinux.org,页面文件目录为/var/www/html/www1;错误日志 为/var/log/httpd/www1.err,访问日志为/var/log/httpd/www1.access;

(b)www2.ilinux.org,页面文件目录为/var/www/html/www2;错误日志为/var/log/httpd/www2.err,访问日志为/var/log/httpd/www2.access;

(c)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名;

2)www1主机仅允许192.168.0.0/24网络中的客户机访问;www2主机可以被所有主机访问;

为http服务提供第3个虚拟主机,要求:

1)www3.ilinux.org,页面文件目录为/var/www/html/www3;错误日志为/var/log/httpd/www3.err,访问日志为/var/log/httpd/www3.access;

2)为此虚拟主机提供基本认证功能,并为其提供两个虚拟用户webuser1和webuser2,

密码均为redhat,要求允许此两用户在提供密码的情况下访问此站点;

配置过程如下:

①安装web服务:yum -y install httpd

②进入主配置文档vim /etc/httpd/conf/httpd.conf

DocumentRoot "/var/www/html/www1"

ServerName www1.ilinux.org

Errorlog /var/log/httpd/www1.err

CustomLog /var/log/httpd/www1.access common

Options Indexes

AllowOverride None

Order allow,deny

Allow from 192.168.0.0/24


DocumentRoot "/var/www/html/www2"

ServerName www2.ilinux.org

Errorlog /var/log/httpd/www2.err

CustomLog /var/log/httpd/www2.access common


Options Indexes

AllowOverride None

Order allow,deny

Allow from all


DocumentRoot /var/www/html/www3

ServerName www3.ilinux.org

ErrorLog /var/log/httpd/www3.err

CustomLog /var/log/httpd/www3.access combined


Options Indexes

AllowOverride AuthConfig

AuthName "AuthConfig"

AuthType basic

AuthUserFile /etc/httpd/.htpasswd

require user webuser1 webuser2

Order allow,deny

Allow from all


htpasswd -cm /etc/httpd/.htpasswd webuser1

htpasswd -m /etc/httpd/.htpasswd webuser2

③分别在/var/www/html目录下创建www1,www2,www3目录

vim /var/www/html/www1/index.html

This is www1 test!

vim /var/www/html/www2/index.html

This is www2 test!

vim /var/www/html/www3/index.html

This is www3 test!

④service httpd start 启动web服务

⑤进行实验效果的验证:浏览器中分别输入www1.ilinux.org www2.ilinux.org www3.ilinux.org

沙澧街