Apache之虚拟主机

什么是虚拟主机?

一台服务器部署多个站点。

分类

基于域名
基于端口
基于IP
实例:
www.heqiuyu.com /var/html/www
blog.heqiuyu.com /var/html/blog
bbs.heqiuyu.com /var/html/bbs
1.创建网站根目录:

[root@xiaoyu var]# tree /var/html/
/var/html/
├── bbs
├── blog
└── www

2.写入index.html内容

[root@xiaoyu html]# for name in www blog bbs;do echo "http://$name.heqiuyu.com" > /var/html/$name/index.html;done

3.编辑vhosts文件,并去掉http.conf注释

# Virtual hosts
Include conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
    ServerAdmin heqiuyu@dummy-host.example.com
    DocumentRoot "/var/html/www"
    ServerName www.heqiuyu.com
    ServerAlias heqiuyu.com
    ErrorLog "logs/www_error_log"
    CustomLog "logs/www_access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin heqiuyu@dummy-host.example.com
    DocumentRoot "/var/html/blog"
    ServerName blog.heqiuyu.com
    ErrorLog "logs/blog_log"
    CustomLog "logs/blog_access" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin heqiuyu@dummy-host.example.com
    DocumentRoot "/var/html/bbs"
    ServerName bbs.heqiuyu.com
    ErrorLog "logs/bbs_log"
    CustomLog "logs/bbs_access" common
</VirtualHost>

4.在apachep配置文件内添加虚拟主机的目录权限

<Directory "/var/html" >
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
    Options FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all

</Directory>
#
[root@xiaoyu ~]# /application/apache/bin/apachectl -t  检查语法
httpd: apr_sockaddr_info_get() failed for xiaoyu
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
Syntax OK
[root@xiaoyu ~]# /application/apache/bin/apachectl graceful 平滑重启

6.修改/etc/hosts文件,测试虚拟主机访问成功

host文件
10.10.9.201 www.heqiuyu.com
10.10.9.201 bbs.heqiuyu.com
10.10.9.201 blog.heqiuyu.com
测试虚拟主机
[root@xiaoyu ~]# lynx www.heqiuyu.com
[root@xiaoyu ~]# lynx bbs.heqiuyu.com
[root@xiaoyu ~]# lynx blog.heqiuyu.com

7.错误提示原因

[root@xiaoyu bin]# ./apachectl graceful
httpd: apr_sockaddr_info_get() failed for xiaoyu
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
解决方法:
修改http.conf   #ServerName www.example.com:80
成


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,001评论 19 139
  • 导语 Apache web 服务器运行着互联网上超过半数的活跃的 web 站点,它凭借着灵活且强劲的功能而广受欢迎...
    xuyan0阅读 5,460评论 1 7
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,637评论 25 708
  • 我爱 你不完美的人生 我爱 你慢慢老去的容颜 我爱 你生命里浅浅的微笑 我爱 你迎向冰冷刺骨的冬天
    细水缘阅读 239评论 0 2
  • 这个七月,绝对是读书丰收月,阅读量创历史新高,加油!
    黑鳗阅读 393评论 0 0