專案

一般

設定檔

動作

單一網域多網站的子 URI

為什麼我們需要在一個網域中使用子 URI 來建立多個網站

當您手邊有許多網站時,該如何部署?它們之間的關係應該是什麼?如果您很有錢,從來不擔心錢的問題,那麼您可以為每個網站部署一台伺服器。但是,如您所知,我們程式設計師並沒有那麼富有,所以…我們可能會考慮在一台伺服器上部署多個網站,而且我們可能只想將它們部署在只有一張網路介面卡的伺服器上,這意味著只有一個 IP 位址。一個 IP 位址,也有兩種解析方式(如果有錯誤,請告訴我),一種是虛擬主機(需要配置 DNS 伺服器的 A 記錄),另一種是多目錄。在這裡,我想談談第二種。

它看起來像什麼

多目錄部署方式,稱為子 URI 方式,看起來像這樣
  • 假設網域是「pfg.com」(IP 位址也可以)
  • 有兩個或多個 rails 應用程式,app1、app2、…appN
  • ubuntu 8.04 伺服器
  • 網路伺服器是 apache2.2
  • Ruby 1.8.6
  • mongrel-1.1.5(可選)
  • Rails 2.1.x 或 Rails 2.2.x(略有不同)
http://pfg.com/app1
http://pfg.com/app2
...
http://pfg.com/appN

接下來,我會告訴您如何配置它們。

如何在 Rails 應用程式中配置它們

我們需要配置三個步驟,一是配置 apache2.2,二是配置 rails 應用程式,最後是配置 mongrel。

配置 apache2.2

首先,我希望 rails 應用程式以 CGI 方式運行。
我們新增一個 VirtualHost 區塊,監聽所有埠

NameVirtualHost *
<VirtualHost *>
    ...
</VirtualHost>

之後,我們需要建立一個 DocumentRoot,並將所有應用程式的符號連結放在這裡(不要連結到 app*/public)。

<VirtualHost *>
    DocumentRoot "/var/www/" 
</VirtualHost>

#ls -l /var/www/
-lrwxrwxrwx ..... app1 -> /home/jean/app1
-lrwxrwxrwx ..... app2 -> /home/jiong/app2
...
-lrwxrwxrwx ..... appN -> /home/eric/appN

完成後,我們就有了虛擬主機和文件根目錄,接下來我們需要為所有 rails 應用程式新增 Alias 指令。

<VirtualHost *>
    DocumentRoot "/var/www/" 
    Alias /myapp1 "/var/www/app1/public/" 
    Alias /yourapp2 "/var/www/app2/public/" 
    Alias /hisappN "/var/www/appN/public" 
</VirtualHost>

別名是子 URI。請注意:不要讓子 URI 名稱與符號連結的名稱相同,這一點很重要

之後,我們需要配置 Directory 區塊,如下所示

...
    Alias /myapp1 "/var/www/app1/public/" 
    <Directory "/var/www/app1/public/">
         Options FollowSymLinks +ExecCGI
         AllowOverride all
         Order allow,deny
         Allow from all
    </Directory>
...

完成後,apache2.2 的配置就完成了。要以 CGI 方式運行,我們也需要配置 rails 應用程式。所以,請閱讀下一節。

第二,配置 rails 應用程式

進入您的 rails 應用程式,像這樣做

# cd /home/jiong/app1/config/
# vim environment.rb
=> add code:
Rails 2.1.x: add *ActionController::AbstractRequest.relative_url_root="/myapp1"* At the end of file (must the end of file.)
Rails 2.2.x, two way: 
    one is: add *ActionController::Base.relative_url_root="/myapp1"* At the end of file
    other : add *config.action_controller.relative_url_root="/myapp1"* in the block.

如果沒有這個配置,當訪問網站時,會顯示錯誤:路由錯誤:{ /myapp1/ 錯誤} Get。

配置 dispatch.cgi

# cd /home/jiong/app1/public/
# mv dispatch.cgi.example dispatch.cgi
# chmod a+x dispatch.cgi
# which ruby
/usr/local/bin/ruby => It depend on your system.
# vim dispatch.cgi
=> change the first line to #!/usr/local/bin/ruby
test dispatch.cgi.
# sudo -u www-data ./dispatch.cgi
=> success: will show the home page source code for you.

所有其他的 Rails 應用程式都應該這樣做。

重新啟動您的 apache2.2 伺服器,然後您就可以訪問它們:http://pfg.com/myapp1/http://pfg.com/yourapp2/http://pfg.com/hisappN

如果您只想以 CGI 方式運行,那麼就到此為止了。

第三,新增 mongrel-1.1.5

Mongrel 在 apache2.2 上運行,apache2.2 使用代理。

首先,配置 mongrel,進入您的 Rails 應用程式

# cd /home/jiong/app1/
# mongrel_rails <TBD.>
# ln -s /home/jiong/app1/config/mongrel_cluster.yml /etc/mongrel_cluster/app1.yml

您可以先測試 mongrel,http://127.0.0.1:8000/myapp1/...

然後,再次配置 apache2.2,新增以下內容

...
    <Directory ...>
       ...
    </Directory>

    <Proxy balancer://app1_cluster>
        Order allow,deny
        Allow from all
        balancer 127.0.0.1:8000
        balancer 127.0.0.1:8001
        balancer 127.0.0.1:8002
    </Proxy>
    RewriteCond <TBD.>
    RewriteRule ^/app1/?(.*)$ balancer://app1_cluster/$1 [QSA,L]
...

完成後,一切都完成了。

注意

待辦事項

Mischa The Evil將近 14 年 前更新 · 3 個版本