2012-06-11 49 views
45

我正在Mac OS X上使用XAMPP。 我想正確運行一個客戶端的symfony網站,我真的不知道(還)symfony,我只是想安裝和啓動它。Apache VirtualHost和localhost

我改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> 

,該網站工作(!耶),但我無法訪問我的其他本地網站沒有,因爲本地主機 被呈現爲www.mysite.local。 我在哪裏錯了?

謝謝!

回答

18

這是正常的,如果你看到它。由於它是第一個虛擬主機條目,它將顯示本地主機。

比方說,例如,你不希望該頁面顯示。所有你想要顯示的是apache它的工作頁面,所以你可以在本地主機mysite.local之前創建一個虛擬主機條目並將它指向它的工作頁面。

但這是正常的。之前我有這個問題,所以不用擔心!

+2

好吧,我已經在第一個指令後添加了localhost的VirtualHost,現在它可以工作。謝謝:) –

+1

沒有問題:)多數民衆贊成什麼即時消息這裏! –

2

根據此文件:Name-based Virtual Host Support

您可能會丟失以下指令:

NameVirtualHost *:80 
+1

我已經添加了指令,但沒有任何更改:當我打開http:// localhost /我看到站點配置爲http://www.mysite.local。 –

+0

同樣的問題,良好的舊的Mac OS;/ – alchemication

+0

這解決了我的問題,謝謝! – Fedcomp

24

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這意味着,虛擬主機的此塊將僅影響請求IP地址127.0.0.2端口80,這就是生活。 。默認端口爲HTTP

爲了路由名稱intranet.dev正確,我hosts輸入行是這樣的:

127.0.0.2 intranet.dev 

這樣,它將阻止您爲localhost創建另一個VirtualHost塊,這是不必要的。

+0

使用其他本地環回地址是一個非常巧妙的技巧。獎勵! – Terix

+2

專業提示:不要忘記更改'httpd.conf'中的'Listen'參數以匹配您配置的環回地址。 – Noir

9

您可能需要使用此:

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

作爲你的第一個虛擬主機(之前另一個虛擬主機放置)

+0

這就解決了我在小牛隊的問題 – alchemication

+0

在Ubuntu 14.04上爲我工作! – Konsole

+0

爲我工作,你的夢幻般的靈魂! –

72

這爲我工作!

要運行的項目,如http://localhost/projectName

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

要運行的項目,如http://somewebsite.com本地

<VirtualHost somewebsite.com:80> 
    ServerAdmin [email protected] 
    DocumentRoot /path/to/htdocs/somewebsiteFolder 
    ServerName www.somewebsite.com 
    ServerAlias somewebsite.com 
</VirtualHost> 

同爲descripted其他網站

<VirtualHost anothersite.local:80> 
    ServerAdmin [email protected] 
    DocumentRoot /path/to/htdocs/anotherSiteFolder 
    ServerName www.anothersite.local 
    ServerAlias anothersite.com 
</VirtualHost> 
+9

我覺得這應該是被接受的答案。沒有港口互換,OP是如何設想的。 –

+6

謝謝,太棒了!這幫助了我。另外我想說,如果想要運行'projectname.com'這樣的項目,他需要將'projectname.com'添加到'hosts'文件(Ubuntu上的'/ etc/hosts') –

+1

,但是這種配置不能用於同一時間? – albanx

5

爲某人做的一切這裏仍然無法訪問:
XAMPP和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在這裏,阿帕奇自身進行配置文件。 不需要NameVirtualHost *:80,它的棄用,你可以使用它,但沒有什麼區別。

然後編輯主機你必須運行NOTEPAD作爲管理員(描述爲BELLOW),如果你在沒有這樣做的情況下編輯文件,你正在編輯一個僞文件,而不是原始文件(是的,它節省了時間,但是它不是真正的文件)
在Windows中:

查找記事本圖標,點擊右鍵,以管理員身份運行,打開的文件中,轉至c:/窗/ SYSTEM32 /司機/ etc/hosts文件,勾選「看所有文件「,打開主機。

如果您在之前對其進行編輯,可能會在未以管理員身份運行時看到它以前編輯的文件。

然後檢查Apache是​​否讀你的httpd-vhost.conf,去xampFolder /阿帕奇/ bin中,Shift +右鍵,打開終端命令在這裏,打開XAMPP(如你通常做),啓動apache ,然後在命令行中鍵入httpd -S,您將看到虛擬主機列表,只需檢查您的something.dev是否存在即可。

+0

此外,命令'httpd -S'幫助我修復錯誤 – Abylay

0

這可能是因爲您的網絡文件夾(如「/ Applications/MAMP/htdocs/mysite/web」)爲空。

我的建議是首先讓你的項目,然後製作虛擬主機。 我去過類似的情況。 我用在的DocumentRoot一個空文件夾中的httpd-vhosts.confiz,我不能訪問我的shahg101.com網站

+1

可能不是一個明確的答案,幫助使用確切的解決方案 – Mostafiz

+0

虛擬驅動器將不會創建,除非你在文件夾中有INDEX.php,所以我在說你的文件夾可能爲空(不包含index.php文件) – Shahmir

4

我訪問本地主機的同一個問題與虛擬主機的工作時。 我在虛擬主機添加名字聽似如下─

在我主機文件,我在下面的代碼添加代碼解析(C:\ WINDOWS \ SYSTEM32 \ DRIVERS \等\主機) -

127.0.0.1  main_live 

,在我的httpd.conf下面的代碼我已經添加 -

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

就是這樣。它的工作原理,我可以同時使用本地主機,phpmyadmin,以及main_live(我的虛擬項目)。

+0

爲我工作。謝謝@Raj –

+0

修改主機文件在這裏很重要。 –

+0

在我的情況下,在瀏覽器上打main_live並沒有第一次工作。然後我嘗試http:// main_live,它工作。然後main_live(沒有http)開始工作。非常奇怪 –

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. 添加以下行:​​如果你需要的話,加入這行:ServerAlias www.example.local
  8. 保存文件並重新啓動Apache:service Apache2 restart

打開瀏覽器,然後導航至example.local。你應該看到你的網站。

3

John Smith的其他描述answer from official documentation。理解它是爲什麼。

主要宿主消失

如果要添加虛擬主機到現有的Web服務器,則必須 也創建現有主機塊。 此虛擬主機中包含的ServerName和DocumentRoot應與全局ServerName和DocumentRoot相同,爲 。首先在配置文件中列出此虛擬主機 ,以便它將充當 默認主機。

例如與XAMPP正常工作,防止虛擬主機覆蓋主主機,添加如下行到httpd-vhosts.conf

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

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

其實,我沒有使用'ServerName localhost',只是複製(從httpd.conf)'DocumentRoot'和''標籤中。 –

1

只要改變<VirtualHost *:80><VirtualHost 127.0.0.1:80>

然後,默認DocumentRoot將用於指向您的服務器的所有域或IP,並指定VirtualHost將起作用。