美文网首页城市故事想法成长励志
apache部署(四)虚拟机

apache部署(四)虚拟机

作者: zhangxiaohao | 来源:发表于2019-06-17 06:18 被阅读0次
介绍

虚拟机可以使一个WEB服务器同时发布多个WEB站点。
虚拟主机有三种实现方式:

  • 基于IP地址
  • 基于监听端口
  • 基于域名(host)
基于ip地址的虚拟主机
  • 开启虚拟主机功能
vim /usr/local/apache/conf/httpd.conf
Inclue conf/extra/httpd-vhosts.conf 本行#去掉
  • 设置ip
    给服务器配置多个ip,有几个虚拟主机就配置几个ip地址.
    这里以两个ip为例:192.168.22.66 192.168.22.88
  • 创建web网站
    创建web网站web1,web2
mkdir /usr/local/apche/htdocs/web{1..2}
echo hello web1>usr/local/apache/htdocs/web1/index.html
echo hello web2>usr/local/apache/htdocs/web2/index.html
  • 修改子配置文件
cd /usr/local/apche/conf/extra
vim httpd-vhosts.conf
#####更改形式如下:
<VirtualHost 192.168.22.66:80>
    DocumentRoot "/usr/local/apache/htdocs/web1"
    #ServerName dummy-host.example.com
    #ErrorLog "logs/dummy-host.example.com-error_log"
    #CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>

<VirtualHost 192.168.22.88:80>
    DocumentRoot "/usr/local/apache/htdocs/web2"
    #ServerName dummy-host2.example.com
    #ErrorLog "logs/dummy-host2.example.com-error_log"
    #CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>

DocumentRoot:站点根目录
ServerName:服务名
ErrorLog:错误日志
CustomLog:访问日志

  • 重启服务
/usr/local/apache/bin/apachectl -t # 检查配置
killall httpd
usr/local/apache/bin/apachectl
  • 测试
elinks http://192.168.22.66 -dump
elinks http://192.168.22.88 -dump
基于端口的虚拟机
  • 开启虚拟主机功能
    vim /usr/local/apache/conf/httpd.conf
Inclue conf/extra/httpd-vhosts.conf 本行#去掉
  • 设置ip
    这里ip为:192.168.22.66
  • 创建web网站
    创建web网站web1,web2
mkdir /usr/local/apche/htdocs/web{1..2}
echo hello web1>usr/local/apache/htdocs/web1/index.html
echo hello web2>usr/local/apache/htdocs/web2/index.html
  • 修改子配置文件
cd /usr/local/apche/conf/extra
vim httpd-vhosts.conf
#####更改形式如下:
<VirtualHost *80>
    DocumentRoot "/usr/local/apache/htdocs/web1"
    #ServerName dummy-host.example.com
    #ErrorLog "logs/dummy-host.example.com-error_log"
    #CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>
Listen 81  #开端口
<VirtualHost *:81>
    DocumentRoot "/usr/local/apache/htdocs/web2"
    #ServerName dummy-host2.example.com
    #ErrorLog "logs/dummy-host2.example.com-error_log"
    #CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>
  • 重启服务
/usr/local/apache/bin/apachectl -t # 检查配置
killall httpd
usr/local/apache/bin/apachectl
  • 测试
elinks http://192.168.22.66 -dump
elinks http://192.168.22.66:81 -dump
基于域名的虚拟主机
  • 环境说明
    ip:192.168.22.66
    运行两个站点: web1.test.com web2.test.com
  • 设置多个域名
    生产环境中可以直接在dns解析域名到主机IP.
    实验中如果没有域名和DNS,使用hosts文件做解析测试效果。
vim cat /etc/hosts
####增加以下两句
192.168.22.66 web1.test.com
192.168.22.66 web2.test.com
  • 开启虚拟主机功能
vim /usr/local/apache/conf/httpd.conf
Inclue conf/extra/httpd-vhosts.conf 本行#去掉
  • 创建web网站
    创建web网站web1,web2
mkdir /usr/local/apche/htdocs/web{1..2}
echo hello web1>usr/local/apache/htdocs/web1/index.html
echo hello web2>usr/local/apache/htdocs/web2/index.html
  • 修改子配置文件
cd /usr/local/apche/conf/extra
vim httpd-vhosts.conf
#####更改形式如下:
<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/web1"
    ServerName web1.test.com
    #ErrorLog "logs/dummy-host.example.com-error_log"
    #CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs/web2"
    ServerName web2.test.com
    #ErrorLog "logs/dummy-host2.example.com-error_log"
    #CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>
  • 重启服务
/usr/local/apache/bin/apachectl -t # 检查配置
killall httpd
usr/local/apache/bin/apachectl
  • 测试
elinks http:// web1.test.com  -dump
elinks http://web2.test.com  -dump

相关文章

网友评论

    本文标题:apache部署(四)虚拟机

    本文链接:https://www.haomeiwen.com/subject/csvmxctx.html