基于apache的虚拟主机
Posted anttech
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于apache的虚拟主机相关的知识,希望对你有一定的参考价值。
apche支持3种类型虚拟主机:
基于IP的虚拟主机
基于端口的虚拟主机
基于主机名的虚拟主机
配置两个站点虚拟主机:
/data/web1
/data/web2
一、基于IP虚拟主机
添加一个IP:[[email protected] ~]# nmcli connection modify ens33 +ipv4.addresses 192.168.95.10/24
重启网络服务:systemctl restart network
1、修改主配置文件
[[email protected] ~]# cd /etc/httpd/conf
[[email protected] conf]# cp httpd.conf{,.bak}
#DocumentRoot "/var/www/html"
注释默认的网站主页,其他保持默认
2、增加虚拟主机配置
cd /etc/httpd/conf.d
[[email protected] 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、创建虚拟主机目录及主页
[[email protected] conf.d]# mkdir -p /data/web{1,2}/logs
[[email protected] conf.d]# echo "web1 test page" >/data/web1/index.html
[[email protected] conf.d]# echo "web2 test page" >/data/web2/index.html
4、重启测试
[[email protected] ~]# yum install elinks -y
[[email protected] ~]# elinks -dump http://192.168.95.10
web1 test page
[[email protected] ~]# 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、重启测试
[[email protected] ~]# elinks -dump http://192.168.95.11
web1 test page
[[email protected] ~]# 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服务测试
[[email protected] ~]# elinks -dump http://www.uplook.com
web1 test page
[[email protected] ~]# elinks -dump http://www.uplooking.com
web2 test page
以上是关于基于apache的虚拟主机的主要内容,如果未能解决你的问题,请参考以下文章
centos 7 APACHE-虚拟主机-基于域名的虚拟主机