江湖道

庙堂,江湖,学术!

返回顶部

基于apache的虚拟主机

apche支持3种类型虚拟主机:
基于IP的虚拟主机
基于端口的虚拟主机
基于主机名的虚拟主机


配置两个站点虚拟主机:
/data/web1
/data/web2

一、基于IP虚拟主机
添加一个IP:[root@localhost ~]# nmcli connection modify ens33 +ipv4.addresses 192.168.95.10/24
重启网络服务:systemctl restart network

1、修改主配置文件
[root@localhost ~]# cd /etc/httpd/conf
[root@localhost conf]# cp httpd.conf{,.bak}

#DocumentRoot "/var/www/html"
注释默认的网站主页,其他保持默认

2、增加虚拟主机配置

cd /etc/httpd/conf.d

[root@localhost conf.d]# vi virtualhost.conf
<Directory "/data">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

<VirtualHost 192.168.95.10:80>
DocumentRoot /data/web1
ServerName 192.168.95.10
ErrorLog /data/web1/logs/error_log
CustomLog /data/web1/logs/access_log combined
</VirtualHost>

<VirtualHost 192.168.95.11:80>
DocumentRoot /data/web2
ServerName 192.168.95.11
ErrorLog /data/web2/logs/error_log
CustomLog /data/web2/logs/access_log combined
</VirtualHost>

3、创建虚拟主机目录及主页
[root@localhost conf.d]# mkdir -p /data/web{1,2}/logs
[root@localhost conf.d]# echo "web1 test page" >/data/web1/index.html
[root@localhost conf.d]# echo "web2 test page" >/data/web2/index.html

4、重启测试
[root@localhost ~]# yum install elinks -y
[root@localhost ~]# elinks -dump http://192.168.95.10
web1 test page
[root@localhost ~]# elinks -dump http://192.168.95.11
web2 test page


二、基于端口
使用同一个IP,不同的端口
192.168.95.11:80
192.168.95.11:81

1、修改主配置文件
#Listen 80
Listen 192.168.95.11:80
Listen 192.168.95.11:81

2、修改虚拟主机文件
<Directory "/data">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<VirtualHost 192.168.95.11:80>
DocumentRoot /data/web1
ServerName 192.168.95.11
ErrorLog /data/web1/logs/error_log
CustomLog /data/web1/logs/access_log combined
</VirtualHost>

<VirtualHost 192.168.95.11:81>
DocumentRoot /data/web2
ServerName 192.168.95.11
ErrorLog /data/web2/logs/error_log
CustomLog /data/web2/logs/access_log combined
</VirtualHost>

3、重启测试
[root@localhost ~]# elinks -dump http://192.168.95.11
web1 test page
[root@localhost ~]# elinks -dump http://192.168.95.11:81
web2 test page

三、基于名称的虚拟主机
相同的IP,相同的端口,不同的主机名

www.uplook.com -----web1
www.uplooking.com ----web2

DNS准备略,也可以使用hosts文件
1、修改主配置文件
#Listen 80
Listen 192.168.95.11:80
#Listen 192.168.95.11:81

2、修改虚拟主机文件
<Directory "/data">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<VirtualHost 192.168.95.11:80>
DocumentRoot /data/web1
ServerName www.uplook.com
ErrorLog /data/web1/logs/error_log
CustomLog /data/web1/logs/access_log combined
</VirtualHost>

<VirtualHost 192.168.95.11:80>
DocumentRoot /data/web2
ServerName www.uplooking.com
ErrorLog /data/web2/logs/error_log
CustomLog /data/web2/logs/access_log combined
</VirtualHost>

3、重启httpd服务测试
[root@localhost ~]# elinks -dump http://www.uplook.com
web1 test page
[root@localhost ~]# elinks -dump http://www.uplooking.com
web2 test page



posted @ 2019-04-27 14:28  大江东流水  阅读(1191)  评论(0编辑  收藏  举报