当前位置: > Linux服务器 > apache >

在 Apache2.2.23 上通过 httpd.conf 配置虚拟主机

时间:2015-09-28 22:55来源:linux.it.net.cn 作者:IT
虚拟主机不要放在一个共同的端口,否则默认情况下只进入每一个虚拟主机目录。

    ServerRoot "/var/lib/apache2.2"

    DocumentRoot "/var/lib/apache2.2/htdocs"

    ErrorLog "logs/error_log"

    # apache 解析 .php 文件
    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

    # myproject.com

    Listen 192.168.55.9:80

    <VirtualHost *:80>
         DocumentRoot /var/lib/apache2.2/htdocs/myproject
         ServerName myproject.com
         # ScriptAlias /cgi-bin/ "/var/lib/apache2.2/htdocs/myproject/cgi-bin/"
    </VirtualHost>

  
    # myproject1.com

   Listen 192.168.55.9:81

    <VirtualHost *:81>
         DocumentRoot /var/lib/apache2.2/htdocs/myproject1
         ServerName myproject1.com
         # ScriptAlias /cgi-bin/ "/var/lib/apache2.2/htdocs/myproject1/cgi-bin/"
    </VirtualHost>

   

   而我们在客户端的hosts文件里面也需要做IP映射,即:

   192.168.55.9 myproject.com

   192.168.55.9  myproject1.com

    但是我们不需要在服务器端的hosts里面做IP映射。

   实际上我们做请求时,还是用80端口来响应。

   

    经测试,上面这种做法存在一个问题:即使是通过 myproject1.com 来访问,进入的还是 myproject.com 。

    改为下面的可正常跳转:

    ServerRoot "/var/lib/apache2.2"

    DocumentRoot "/var/lib/apache2.2/htdocs"

    ErrorLog "logs/error_log"
    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

    NameVirtualHost *:80

    Listen 192.168.55.9:80

    # myproject.com  

    <VirtualHost *:80>
         DocumentRoot /var/lib/apache2.2/htdocs/myproject
         ServerName myproject.com
         # ScriptAlias /cgi-bin/ "/var/lib/apache2.2/htdocs/myproject/cgi-bin/"
    </VirtualHost>

    # myproject1.com

    <VirtualHost *:80>
         DocumentRoot /var/lib/apache2.2/htdocs/myproject1
         ServerName myproject1.com
         # ScriptAlias /cgi-bin/ "/var/lib/apache2.2/htdocs/myproject1/cgi-bin/"
    </VirtualHost>

(责任编辑:IT)
------分隔线----------------------------