2012-09-09 61 views
40

按照我在網上可以找到的每個指南,用完了幾個小時。如何在一個apache實例上運行多個站點

我希望有一個單一的Apache運行兩個網站,像這樣 - 192.168.2.8/site1 和 192.168.2.8/site2

我在圈子裏被兜了一圈,但在那一刻我在「網站可用(符號鏈接到網站啓用 - )」,看起來像這 -

<VirtualHost *:2000> 

ServerAdmin [email protected] 
ServerName site1 
ServerAlias site1 

# Indexes + Directory Root. 
DirectoryIndex index.html 
DocumentRoot /home/user/site1/ 

# CGI Directory 
ScriptAlias /cgi-bin/ /home/user/site1/cgi-bin/ 

Options +ExecCGI 

# Logfiles 
ErrorLog /home/user/site1/logs/error.log 
CustomLog /home/user/site1/logs/access.log combined 

</VirtualHost> 

<VirtualHost *:3000> 

ServerAdmin [email protected] 
ServerName site2 
ServerAlias site2 

# Indexes + Directory Root. 
DirectoryIndex index.html 
DocumentRoot /home/user/site2/ 

# CGI Directory 
ScriptAlias /cgi-bin/ /home/user/site2/cgi-bin/ 

Options +ExecCGI 

# Logfiles 
ErrorLog /home/user/site2/logs/error.log 
CustomLog /home/user/site2/logs/access.log combined 

</VirtualHost> 
個conf文件

的http.conf看起來像這 -

NameVirtualHost *:2000 
NameVirtualHost *:3000 

目前我得到這個錯誤 -

[error] VirtualHost *:80 — mixing * ports and non-* ports with a NameVirtualHostaddress is not supported, proceeding with undefined results 

Ports.conf看起來是這樣的 - (雖然沒有導遊提到任何需要編輯這個)

NameVirtualHost *:80 

Listen 80 
<IfModule mod_ssl.c> 
# If you add NameVirtualHost *:443 here, you will also have to change 
# the VirtualHost statement in /etc/apache2/sites-available/default-ssl 
# to <VirtualHost *:443> 
# Server Name Indication for SSL named virtual hosts is currently not 
# supported by MSIE on Windows XP. 
Listen 443 
</IfModule> 

<IfModule mod_gnutls.c> 
Listen 443 
</IfModule> 

任何人都可以給一些簡單的指示,讓這個運行?我發現的每一個指南都是以不同的方式去做,而每一個指南都會導致不同的錯誤。我顯然做錯了事,但沒有找到可能的解釋。

只想要一個站點可以在端口2000上訪問,另一個站點可以在端口3000上訪問(或者其他,只需選擇那些端口來測試)。

我運行Ubuntu服務器12.04 ...

=============

編輯

接着一個 '引導' ......

我現在已經在此設置站點可用:

<VirtualHost *:80> 
    DocumentRoot "/home/user/site1/" 
    ServerName 192.168.2.10/site1 
</VirtualHost> 

<VirtualHost *:80> 
    DocumentRoot "/home/user/site2/" 
    ServerName 192.168.2.10/site2 
</VirtualHost> 

已經將這個在apache2.conf:

ServerName site1 
ServerName site2 

已經加入這個ports.conf:

Listen 192.168.2.10:80 

==============

編輯

現在的工作,我把它放在啓用了site的conf文件中:

​​

我在ports.conf中有這個:

Listen *:80 
Listen *:81 
Listen *:82 

我在apache2有這個。CONF:

ServerName site1 
ServerName site2 

我沒有在我剛剛纔通過試錯的一整天工作的任何指南找到這個,所以我不知道這是一個很好的解決方案。但至少現在我正在努力工作。

+0

在我看來,你需要在NameVirtualHost中指定虛擬主機名。 –

+0

乾杯,我已經嘗試了很多事情,你能更具體地說我應該嘗試放在那裏嗎? – Exbi

+0

經過整整一天的努力才得以發揮作用,我終於偶然發現了答案。我感到精神枯竭,我要去睡覺了。 我懷疑我的狡猾的apache黑客解決方案會對任何人都感興趣,但我會將其編輯到我的問題的最後。 – Exbi

回答

62

你的問題是混合幾個不同的概念。你開始說你想在同一個服務器上使用同一個域名運行站點,但是在不同的文件夾中。這不需要任何特殊的設置。一旦你運行了單個域,你就可以在該文件根目錄下創建文件夾。

根據問題的其餘部分,您真正想要做的是在同一臺服務器上運行各自的站點,並使用自己的域名。

關於該主題,您可以找到的最佳文檔是apache手冊中的virtual host文檔。

有兩種類型的虛擬主機:基於名稱和基於IP。基於名稱允許您使用單個IP地址,而基於IP的每個站點需要不同的IP。根據您的描述,您想使用基於名稱的虛擬主機。

您得到的最初錯誤是由於您使用的端口號不同於NameVirtualHost。如果您確實希望從80以外的端口提供站點,則每個端口都需要有一個NameVirtualHost條目。

假設你從頭開始,這比看起來簡單得多。

您需要做的第一件事就是告訴apache您要使用基於名稱的虛擬主機。

NameVirtualHost *:80 

現在阿帕奇知道你想做什麼,你可以設置你的虛擬主機定義:

<VirtualHost *:80> 
    DocumentRoot "/home/user/site1/" 
    ServerName site1 
</VirtualHost> 

<VirtualHost *:80> 
    DocumentRoot "/home/user/site2/" 
    ServerName site2 
</VirtualHost> 

注意,只要你想在同一端口上,你可以運行儘可能多的網站。 ServerName是不同的就足以告訴Apache使用哪個虛擬主機。此外,ServerName指令始終是域/主機名,不應包含路徑。

如果您決定在80以外的端口上運行網站,則在訪問該網站時,您必須始終在網址中包含端口號。因此,而不是去http://example.com你就必須去http://example.com:81

+0

可否請您詳細說明80以外的端口的情況。我在實現這個過程中真的很困惑。 :) – user79307

+1

@accssharma默認情況下,apache(和所有其他網絡服務器軟件)偵聽端口80上的連接。因此,瀏覽器在請求網站時編碼爲連接到端口80。如果您在80以外的端口上運行網站,則用戶在請求站點時將始終需要包含端口號。我強烈建議不要這樣。 – bradym

+0

@accssharma如果您選擇在80以外的端口上運行站點,則需要指定端口的NameVirtualHost行,然後指定同一端口的VirtualHost容器。基本上,複製我上面的內容,並將80替換爲您打算運行該站點的端口。 – bradym

3

是用虛擬主機,只要你想,你可以有很多並行程序:

打開

的/ etc/httpd的/ conf目錄/ httpd的。CONF

Listen 81 
Listen 82 
Listen 83 

<VirtualHost *:81> 
    ServerAdmin [email protected] 
    DocumentRoot /var/www/site1/html 
    ServerName site1.com 
    ErrorLog logs/site1-error_log 
    CustomLog logs/site1-access_log common 
    ScriptAlias /cgi-bin/ "/var/www/site1/cgi-bin/" 
</VirtualHost> 

<VirtualHost *:82> 
    ServerAdmin [email protected] 
    DocumentRoot /var/www/site2/html 
    ServerName site2.com 
    ErrorLog logs/site2-error_log 
    CustomLog logs/site2-access_log common 
    ScriptAlias /cgi-bin/ "/var/www/site2/cgi-bin/" 
</VirtualHost> 

<VirtualHost *:83> 
    ServerAdmin [email protected] 
    DocumentRoot /var/www/site3/html 
    ServerName site3.com 
    ErrorLog logs/site3-error_log 
    CustomLog logs/site3-access_log common 
    ScriptAlias /cgi-bin/ "/var/www/site3/cgi-bin/" 
</VirtualHost> 

重新啓動Apache

service httpd restart

現在,您可以參考站點1:

http://<ip-address>:81/ 
http://<ip-address>:81/cgi-bin/ 

站點2:

http://<ip-address>:82/ 
http://<ip-address>:82/cgi-bin/ 

Site3:

http://<ip-address>:83/ 
http://<ip-address>:83/cgi-bin/ 

如果路徑中沒有任何腳本硬編碼那麼你的網站應該無縫協作。

相關問題