由于本人很笨,第一次学步骤比较多。想学的可以耐下心来看、来学。
实验环境:ContOS 7,Apache服务器
在ContOS中可以运行一下代码直接下载
[root@localhost ~]# yum install -y httpd
下载完可以查看下载的内容
[root@localhost ~]# rpm -ql httpd | less
配置Selinux文件
[root@localhost ~]# vim /etc/selinux/config
找到并改成下面的
SELINUX=disabled
SELINUXTYPE=targeted
关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
启动httpd服务,访问测试
[root@localhost ~]# systemctl start httpd
然后在浏览器输入你CentOS的ip地址,出现下面的页面说明下载完成。
一:基于多IP地址方式
1.创建配置文件
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
在里面将下面代码写入
<VirtualHost 192.168.253.128:80>
ServerName a.com
DocumentRoot "/www/a.com/"
</VirtualHost>
<VirtualHost 192.168.253.129:80>
ServerName b.com
DocumentRoot "/www/b.com/"
</VirtualHost>
2.网卡绑定多个IP(这里我的网卡名是ens33)
[root@localhost conf.d]# ip addr add 192.168.81.137 dev ens33
[root@localhost conf.d]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:5c:ff:91 brd ff:ff:ff:ff:ff:ff
inet 192.168.81.136/24 brd 192.168.253.255 scope global dynamic eno16777736
valid_lft 1542sec preferred_lft 1542sec
inet 192.168.81.137/32 scope global eno16777736
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe5c:ff91/64 scope link
valid_lft forever preferred_lft forever
3.创建虚拟主机a.com和b.com的主页面
[root@localhost conf.d]# mkdir -pv /www/{a.com,b.com}
mkdir: 已创建目录 "/www"
mkdir: 已创建目录 "/www/a.com"
mkdir: 已创建目录 "/www/b.com"
[root@localhost conf.d]# vim /www/a.com/index.html
进入/a.com/index.html,将下面代码写入
<h1>Hello,a.com</h1>
[root@localhost conf.d]# vim /www/b.com/index.html
进入/b.com/index.html,将下面代码写入
<h1>Hello,b.com</h1>
4.检查配置问价是否正常
[root@localhost conf.d]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
5.重启httpd服务
[root@localhost conf.d]# systemctl restart httpd
6.打开浏览器,查看结果
二、基于多端口方式
1.创建配置文件
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
将下面代码写入(当然里面有的就不写入了)
<VirtualHost 192.168.253.128:80>
ServerName a.com
DocumentRoot "/www/a.com/"
</VirtualHost>
<VirtualHost 192.168.253.128:8080>
ServerName b.com
DocumentRoot "/www/b.com/"
</VirtualHost>
2.修改httpd主配置文件
[root@localhost conf.d]# vim /etc/httpd/conf/httpd.conf
在里面找到Listen 80,然后在下面写入一行Listen 8080
3.创建虚拟主机a.com和b.com的主页面(前面已经创建过了就不用重复创建)
4.检查配置文件
[root@localhost conf.d]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
5.重启httpd服务
[root@localhost conf.d]# systemctl restart httpd
6.打开浏览器,查看结果
三.基于域名的方式
1.创建配置文件
[root@localhost ~]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# vim virtual.conf
写入下面的代码
<VirtualHost *:80>
ServerName a.com
DocumentRoot "/www/a.com/"
</VirtualHost>
<VirtualHost *:80>
ServerName b.com
DocumentRoot "/www/b.com/"
</VirtualHost>
2.修改物理主机hosts文件(C:\Windows\System32\drivers\etc),这里是因为物理机去访问Apache服务器
在hosts文件里面添加
192.168.81.136 a.com
192.168.81.136 b.com
3.物理主机ping域名测试
4.创建虚拟主机a.com和b.com的主页面(这里前面已经创建过了就不重复了)
5.检查配置文件
[root@localhost conf.d]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
6.重启httpd服务
[root@localhost conf.d]# systemctl restart httpd
7.打开浏览器,查看结果
文章来源:https://www.toymoban.com/news/detail-770123.html
文章来源地址https://www.toymoban.com/news/detail-770123.html
到了这里,关于Apache配置虚拟主机三种方式(保姆级教学)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!