【Linux基础服务教程】httpd(Apache)虚拟主机功能[单服务器配置多网站]
【Linux基础服务教程】httpd(Apache)虚拟主机功能[单服务器配置多网站]
本教程为第一期教程续集点我查看第一期教程
一、虚拟主机介绍Virtual Host
1.作用
- 在一台服务器可以部署多套网站
- 注意:虚拟主机配置后,主配置文件中部署的网站会失效!!!
2.类型
- 基于名称的虚拟主机(最常用)
- 不同的网站指定不同的ServerName、DocumentRoot
- 基于IP地址的虚拟主机
- 不同的网站监听在不同的IP地址上
3.配置文件格式
/etc/httpd/conf.d/*.conf
<VirtualHost IP:port> #或者*:端口
DocumentRoot xxxxx
ServerName xxxxx
ErrorLog xxxxxxxxxxxx
CustomLog xxxxxxxxxxxxxxxx
</VirtualHost>
二、基于名称的虚拟主机
1.创建网页目录、首页
[root@localhost ~]# mkdir /var/www/html/vedio
[root@localhost ~]# vim /var/www/html/vedio/index.html
<h1> vedio </h1>
[root@localhost ~]# vim /etc/httpd/conf.d/vedio.conf
<VirtualHost 192.168.140.10:80> #这里可以填写"*:80"代表运行在所有IP80端口上
ServerName vedio.linux.com #指定域名
DocumentRoot /var/www/html/vedio #指定网页目录
ErrorLog /var/log/httpd/vedio_error.log #指定访问日志
CustomLog /var/log/httpd/vedio_access.log combined #指定错误日志和记录模式
</VirtualHost>
Directory "/var/www/html/">
Require all granted #配置网页目录权限,不然报错403权限拒绝
</Directory>
2.测试配置文件是否OK
[root@localhost ~]# httpd -t
Syntax OK
如果提示AH00558不是报错
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::20c:29ff:fe0f:55d2. Set the 'ServerName' directive globally to suppress this message
解决方法:编辑主配置文件
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
#ServerName www.example.com:80 #把这里的注释去掉,修改www.example.com为Localhost即可
三、基于IP的虚拟主机
- 网站: python.linux.com 网页目录: /python 192.168.140.100
- 网站: shell.linux.com 网页目录: /shell 192.168.140.200
1.添加多块网卡
[root@localhost network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:8f:c3:68 brd ff:ff:ff:ff:ff:ff
inet 192.168.140.10/24 brd 192.168.140.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe8f:c368/64 scope link
valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:8f:c3:72 brd ff:ff:ff:ff:ff:ff
inet 192.168.140.100/24 brd 192.168.140.255 scope global noprefixroute ens37
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe8f:c372/64 scope link
valid_lft forever preferred_lft forever
4: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:8f:c3:7c brd ff:ff:ff:ff:ff:ff
inet 192.168.140.200/24 brd 192.168.140.255 scope global noprefixroute ens38
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe8f:c37c/64 scope link tentative
valid_lft forever preferred_lft forever
2.配置Python网站
[root@localhost ~]# vim /etc/httpd/conf.d/python.conf
<VirtualHost 192.168.140.100:80>
ServerName python.linux.com
DocumentRoot /python
ErrorLog /var/log/httpd/python_error.log
CustomLog /var/log/httpd/python_access.log combined
</VirtualHost>
<Directory "/python">
Require all granted
</Directory>
3.配置shell网站
[root@localhost ~]# cat /etc/httpd/conf.d/shell.conf
<VirtualHost 192.168.140.200:80>
ServerName shell.linux.com
DocumentRoot /shell
ErrorLog /var/log/httpd/shell_error.log
CustomLog /var/log/httpd/shell_access.log combined
</VirtualHost>
<Directory "/shell">
Require all granted
</Directory>
4.浏览器访问测试
过程省略
- Linux端可以配置自定义DNS服务器或者修改hosts文件
- vim /etc/hosts
- 192.168.140.100 python.linux.com
- 192.168.140.200 shell.linux.com
- Windows端可以配置自定义DNS服务器或者修改hosts文件
- “C:\Windows\System32\drivers\etc\hosts”
- 192.168.140.100 python.linux.com
- 192.168.140.200 shell.linux.com
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 WangShengJJのblog!