返回列表 发帖

CentOS8_Apache2.4基于域名的虚拟主机+代理虚拟主机

笺注:这是在 CentOS8安装LAMP+phpMyAdmin 的基础上进行的。


基于域名的虚拟主机(同IP、同端口、不同域名):
指的是在同一台服务器中运行多个Web站点,每个站点都是独立的,不占用整台服务器。这样可以充分利用服务器的硬件资源,从而大大降低站点的运营成本。

实验中,会以一个IP地址192.168.168.154,同时支撑两个网站:
www.zhuohua.store
bbs.zhuohua.store


默认情况下,Apache会加载/etc/httpd/conf.d/下的配置文件:
[root@centos8 ~]# cat /etc/httpd/conf/httpd.conf |grep IncludeOptional
IncludeOptional conf.d/*.conf


自定义的第一个站点:( www.zhuohua.store
[root@centos8 ~]# cat /etc/httpd/conf.d/www.zhuohua.store.conf
<VirtualHost *:80>
DocumentRoot "/var/www/www.zhuohua.store"
ServerName www.zhuohua.store #主机名
ServerAlias ww.zhuohua.store w.zhuohua.store #主机别名
ErrorLog "/etc/httpd/logs/www.zhuohua.store-error_log"
CustomLog "/etc/httpd/logs/www.zhuohua.store-access_log" combined
<Directory "/var/www/www.zhuohua.store">
    SetOutputFilter DEFLATE
    Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
DirectoryIndex index.htm index.html index.php
</Directory>
</VirtualHost>

## 注释:一个虚拟主机使用一个配置文件



### 创建站点的文件存放目录、首页文件:
mkdir -p /var/www/www.zhuohua.store
echo 'This is www.zhuohua.store.' > /var/www/www.zhuohua.store/index.html
chown -R apache.apache /var/www/www.zhuohua.store


修改配置文件后,须要重启一下Apache:
[root@centos8 ~]# systemctl restart httpd



Window客户端远程访问:
在没有DNS服务器解析域名的情况下,可以在文件hosts里绑定:
C:\WINDOWS\system32\drivers\etc\hosts
图片1.png
2021-3-17 15:13


记得设置文件hosts的权限:
图片2.png
2021-3-17 15:13



效果:
图片3.png
2021-3-17 15:14


图片4.png
2021-3-17 15:14


图片5.png
2021-3-17 15:14





############

自定义的第二个站点:( bbs.zhuohua.store
[root@centos8 ~]# cat /etc/httpd/conf.d/bbs.zhuohua.store.conf
<VirtualHost *:80>
DocumentRoot "/var/www/bbs.zhuohua.store"
ServerName bbs.zhuohua.store
#ErrorLog "/etc/httpd/logs/bbs.zhuohua.store-error_log"
#CustomLog "/etc/httpd/logs/bbs.zhuohua.store-access_log" combined
<Directory "/var/www/bbs.zhuohua.store">
    SetOutputFilter DEFLATE
    Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
DirectoryIndex index.htm index.html index.php
</Directory>
</VirtualHost>

## 注释:一个虚拟主机使用一个配置文件


### 创建站点的文件存放目录、首页文件:
mkdir -p /var/www/bbs.zhuohua.store
echo 'This is bbs.zhuohua.store.' > /var/www/bbs.zhuohua.store/index.htm
chown -R apache.apache /var/www/bbs.zhuohua.store


修改配置文件后,须要重启一下Apache:
[root@centos8 ~]# systemctl restart httpd


Window客户端远程访问:
在没有DNS服务器解析域名的情况下,可以在文件hosts里绑定:
图片6.png
2021-3-17 15:15



效果:
图片7.png
2021-3-17 15:15















######

Apache配置了基于虚拟的主机后,就不能通过访问ip地址的方式访问默认站点了:
图片8.png
2021-3-17 15:16



解决方法:(给默认站点创建一个基于域名的虚拟主机)
[root@centos8 ~]# cat /etc/httpd/conf.d/localhost.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html"
ServerName localhost
ServerAlias 192.168.168.154  #主机别名使用服务器的IP地址
ErrorLog "/etc/httpd/logs/error_log"
CustomLog "/etc/httpd/logs/access_log" combined
<Directory "/var/www/html">
    SetOutputFilter DEFLATE
    Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
DirectoryIndex index.htm index.html index.php
</Directory>
</VirtualHost>


修改配置文件后,须要重启一下Apache:
[root@centos8 ~]# systemctl restart httpd


Window客户端远程访问默认站点的子目录:
图片9.png
2021-3-17 15:17



图片10.png
2021-3-17 15:17















######

Apache配置代理虚拟主机

创建一个站点:( daili.zhuohua.store )
[root@centos8 ~]# cat /etc/httpd/conf.d/daili.zhuohua.store.conf
<VirtualHost *:80>
ProxyRequests Off
ServerName daili.zhuohua.store:80

ProxyPass / http://blog.zhuohua.store:80/
ProxyPassReverse / http://blog.zhuohua.store:80/
</VirtualHost>

## 注释:
当客户端访问http://daili.zhuohua.store时,自动跳转到http://blog.zhuohua.store



修改配置文件后,须要重启一下Apache:
[root@centos8 ~]# systemctl restart httpd


Window客户端远程访问:
在没有DNS服务器解析域名的情况下,可以在文件hosts里绑定:
图片11.png
2021-3-17 15:18



代理成功:
图片12.png
2021-3-17 15:18






相关文章:
CentOS8_Apache2.4反向代理

Apache2.4基于域名的虚拟主机+用户授权限制+客户端地址限制

CentOS8_Nginx基于域名的虚拟主机+代理虚拟主机

返回列表