Apache VirtualHost和localhost

时间:2012-06-11 13:35:42

标签: apache localhost virtualhost

我在Mac OS X上使用XAMPP。 我正在尝试为客户运行一个symfony网站,我真的不知道(还)symfony,我只是想安装并启动它。

我用这种方式改变了我的etc / hosts:

127.0.0.1 www.mysite.local       
用这种方式

和httpd.conf:

<VirtualHost *:80>
  ServerName www.mysite.local
  DocumentRoot /Applications/MAMP/htdocs/mysite/web
  DirectoryIndex index.php
  <Directory /Applications/MAMP/htdocs/mysite/web>
    AllowOverride All
    Allow from All
  </Directory>
  Alias /sf /Applications/MAMP/htdocs/mysite/lib/vendor/symfony/data/web/sf
  <Directory "/Applications/MAMP/htdocs/mysite/lib/vendor/symfony/data/web/sf">
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>

现在,该网站正在运行(是的!),但我无法再访问其他本地网站,因为 localhost  呈现为 www.mysite.local 。 哪里我错了?

谢谢!

12 个答案:

答案 0 :(得分:90)

这对我有用!

运行http://localhost/projectName

等项目
<VirtualHost localhost:80>
   ServerAdmin localhost
    DocumentRoot path/to/htdocs/
    ServerName localhost
</VirtualHost>

在本地运行http://somewebsite.com等项目

<VirtualHost somewebsite.com:80>
     ServerAdmin webmaster@example.com
     DocumentRoot /path/to/htdocs/somewebsiteFolder
     ServerName www.somewebsite.com
     ServerAlias somewebsite.com
</VirtualHost>

其他网站也是如此

<VirtualHost anothersite.local:80>
     ServerAdmin webmaster@example.com
     DocumentRoot /path/to/htdocs/anotherSiteFolder
     ServerName www.anothersite.local
     ServerAlias anothersite.com
</VirtualHost>

答案 1 :(得分:27)

localhost将始终重定向至127.0.0.1。您可以通过将其他VirtualHost命名为其他本地环回地址(例如127.0.0.2)来欺骗它。确保您还更改了相应的hosts文件以实现此目的。

例如,我的httpd-vhosts.conf看起来像这样:

<VirtualHost 127.0.0.2:80>
    DocumentRoot "D:/6. App Data/XAMPP Shared/htdocs/intranet"
    ServerName intranet.dev
    ServerAlias www.intranet.dev
    ErrorLog "logs/intranet.dev-error.log"
    CustomLog "logs/intranet.dec-access.log" combined

    <Directory "D:/6. App Data/XAMPP Shared/htdocs/intranet">
        Options Indexes FollowSymLinks ExecCGI Includes
        Order allow,deny
        Allow from all
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

(请注意,在<VirtualHost>部分,我输入了127.0.0.2:80。这意味着此VirtualHost块只会影响对IP地址127.0.0.2端口80的请求,这是HTTP的默认端口。

要正确路由名称intranet.dev,我的hosts输入行是这样的:

127.0.0.2 intranet.dev

这样,它将阻止您为localhost创建另一个VirtualHost块,这是不必要的。

答案 2 :(得分:24)

如果你看到它,这是正常的。由于它是第一个虚拟主机条目,因此它将显示本地主机。

让我们举例来说,你并不希望该页面显示出来。你要显示的只是apache它的工作页面,所以你可以在mysite.local之前创建一个vhost条目作为本地主机并将其指向它的工作页面。

但这是正常的。我之前遇到过这个问题,所以不用担心!

答案 3 :(得分:11)

你可能想要使用它:

<VirtualHost *:80>
    DocumentRoot "somepath\Apache2.2\htdocs"
    ServerName localhost 
</VirtualHost>
<VirtualHost *:80>

作为您的第一个虚拟主机(将其置于另一个虚拟主机之前)

答案 4 :(得分:8)

我在使用virtualHost时遇到了访问localhost的相同问题。 我通过在virtualHost监听代码中添加名称来解决,如下所示 -

在我的主机文件中,我添加了以下代码( C:\ Windows \ System32 \ drivers \ etc \ hosts ) -

127.0.0.1       main_live

在我的 httpd.conf 中,我添加了以下代码 -

<VirtualHost main_live:80>
    DocumentRoot H:/wamp/www/raj/main_live/
    ServerName main_live
</VirtualHost>

那就是它。它可以工作,我可以同时使用 localhost phpmyadmin ,以及 main_live (我的虚拟项目)。

答案 5 :(得分:5)

对于执行此处描述但仍无法访问的所有内容的人:
Xampp with apache 2.4:

在httpd-vhost.conf上

<VirtualHost *>
    DocumentRoot "D:/xampp/htdocs/dir"
    ServerName something.dev
   <Directory "D:/xampp/htdocs/dir">
    Require all granted #apache v 2.4.4 uses just this
   </Directory>
</VirtualHost>


不需要端口或ip,apache在自己的文件上配置它。 不需要NameVirtualHost *:80,不推荐使用它,你可以使用它,但没有区别。

然后编辑主机,你必须运行NOTEPAD作为管理员(描述的BELLOW),如果你在没有这样做的情况下编辑文件,你正在编辑一个伪文件,而不是原始文件(是的,它保存和狗屎,但它不是真正的文件)
在窗口中:

找到记事本图标,右键单击,以管理员身份运行,打开文件,转到c:/ windows / system32 / driver / etc / hosts,选中&#34;查看所有文件&#34;,打开主机。

如果你以前编辑它,可能会看到它不是你以前没有以管理员身份运行时编辑的文件。

然后检查Apache是​​否正在读取你的httpd-vhost.conf ,转到xampFolder / apache / bin,shift +右键单击,在这里打开终端命令,打开xampp(正如你通常那样) ,启动apache,然后在命令行上键入 httpd -S,您将看到虚拟主机列表,只需检查您的something.dev是否存在。

答案 6 :(得分:4)

John Smith对from official documentation的补充说明。要理解它为什么。

  

主要主机消失

     

如果要将虚拟主机添加到现有Web服务器,则必须执行此操作   还为现有主机创建一个块。该   此虚拟主机中包含的ServerName和DocumentRoot应该是   与全局ServerName和DocumentRoot相同。列出这个虚拟   首先在配置文件中托管,以便它充当   默认主机。

例如,要正常使用XAMPP,要防止VirtualHost覆盖主要主机,请将以下行添加到httpd-vhosts.conf

# Main host
<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "/xampp/htdocs"
</VirtualHost>

# Additional host
<VirtualHost *:80>
    # over directives there
</VirtualHost>

答案 7 :(得分:3)

只需将<VirtualHost *:80>更改为<VirtualHost 127.0.0.1:80>

然后默认DocumentRoot将用于指向您的服务器的所有域或IP,并且指定的VirtualHost将起作用。

答案 8 :(得分:2)

根据此文档:Name-based Virtual Host Support

您可能缺少以下指令:

NameVirtualHost *:80

答案 9 :(得分:0)

可能是因为你的网络文件夹(如“/ Applications / MAMP / htdocs / mysite / web ”所述)是空的。

我的建议是先制作你的项目,然后再制作虚拟主机。 我也有类似的情况。 我在 httpd-vhosts.confiz 中的 DocumentRoot 中使用了一个空文件夹,我无法访问 shahg101.com 网站

答案 10 :(得分:0)

我正在运行Ubuntu 16.04,这对我有用:

  1. 打开终端并cd到/etc/apache2/sites-available。那里 你会找到一个名为000-default.conf的文件。
  2. 复制该文件:cp 000-default.conf example.local.conf
  3. 打开新文件(我使用Nano;使用您熟悉的内容)。
  4. 您会看到许多注释行,您可以删除它们。
  5. <VirtualHost *:80>更改为<VirtualHost example.local:80>
  6. 更改文档根目录以反映文件的位置。
  7. 添加以下行:ServerName example.local如果需要,请添加以下行:ServerAlias www.example.local
  8. 保存文件并重新启动Apache:service Apache2 restart
  9. 打开浏览器并导航至example.local。你应该看到你的网站。

答案 11 :(得分:0)

对于任何使用 WindowsBitnami WAMP 堆栈管理器工具的人来说,这个虚拟主机配置应该进入Bitnami\apache2\conf\bitnami\bitnami.conf

注意:目录部分中的某些设置不是必须的。

例如,我的 site.com 虚拟主机配置如下

<VirtualHost site.com:80>
  DocumentRoot "C:/Bitnami/apache2/htdocs/site/docroot"
  
  <Directory "C:/Bitnami/apache2/htdocs/site/docroot">
    Options Indexes FollowSymLinks
    AllowOverride All
    <IfVersion < 2.3 >
      Order allow,deny                          
      Allow from all
    </IfVersion>
    <IfVersion >= 2.3 >
      Require all granted
    </IfVersion>
  </Directory>
</VirtualHost>

记住其他朋友提到的vhost配置,需要above