![](/img/trans.png)
[英]Adding Apache VirtualHost, DocumentRoot of localhost changes to the VirtualHost's DocumentRoot
[英]Apache VirtualHost and localhost
我正在 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 。
我哪里錯了? 謝謝!
這對我有用!
運行像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>
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 塊,這是不必要的。
如果你看到它,這是正常的。 由於它是第一個虛擬主機條目,它將顯示本地主機。
例如,假設您不希望顯示該頁面。 您想要顯示的只是“Apache,它可以工作”頁面,因此您可以在 mysite.local 之前創建一個vhost條目作為本地主機,並將其指向“它可以工作”頁面。
但這是正常的。 我以前也遇到過這個問題,所以不用擔心!
你可能想使用這個:
<VirtualHost *:80>
DocumentRoot "somepath\Apache2.2\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
作為您的第一個虛擬主機(將它放在另一個虛擬主機之前)。
在使用 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 (我的虛擬項目)。
官方文檔中對約翰·史密斯的回答的附加說明。 要了解為什么會這樣。
主要主持人離開
如果要將虛擬主機添加到現有 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>
對於執行此處描述的所有操作但仍然無法訪問的人:
XAMPP 與 Apache HTTP Server 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; 它已被棄用。 你可以使用它,但它沒有任何區別。
然后要編輯主機,您必須以管理員身份運行記事本(如下所述)。 如果您在沒有執行此操作的情況下編輯文件,則您正在編輯一個偽文件,而不是原始文件(是的,它會保存等,但它不是真實文件)
在 Windows 中:
找到記事本圖標,右擊,以管理員身份運行,打開文件,進入C:/WINDOWS/system32/driver/etc/hosts ,勾選“查看所有文件” ,打開主機。
如果您之前編輯過它,可能您會看到它不是您之前未以管理員身份運行時編輯的文件。
然后檢查 Apache 是否正在讀取您的 httpd-vhost.conf ,轉到文件夾xampFolder/apache/bin , Shift + 右鍵單擊,在此處打開終端命令,打開 XAMPP(如您通常所做的那樣),啟動 Apache,然后在命令行,輸入httpd -S
。 您將看到虛擬主機列表。 只需檢查您的something.dev是否在那里。
只需將<VirtualHost *:80>
更改為<VirtualHost 127.0.0.1:80>
。
然后默認的DocumentRoot
將為指向您的服務器的所有域或 IP 地址提供服務,並且指定的VirtualHost
將起作用。
這可能是因為您的 web 文件夾(如提到的“/Applications/MAMP/htdocs/mysite/ web ”)是空的。
我的建議是先做你的項目,然后再做虛擬主機。
我去了類似的情況。 我在httpd-vhosts.confiz的DocumentRoot中使用了一個空文件夾,但無法訪問我的shahg101.com站點。
我正在運行Ubuntu 16.04 (Xenial Xerus)。 這對我有用:
/etc/apache2/sites-available
。 在那里你會找到一個名為000-default.conf
的文件。cp 000-default.conf example.local.conf
<VirtualHost *:80>
更改為<VirtualHost example.local:80>
ServerName example.local
如果需要,添加此行: ServerAlias www.example.local
service Apache2 restart
打開瀏覽器並導航到example.local
。 您應該會看到您的網站。
對於使用Windows和Bitnami 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 配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.