Sub URI for multisites at one domain

Why we need SubURI in one domain for multisites

When there are many web sites on your hands, then how to deploy? what their relationship should be? If you are richer, and never care about money, then you can deploy them one server machine one site. But, as you know, we programmers, not so rich as we are, so... we may consider deploy one server machine multisites, and maybe we just want to deploy them at the server machine just has one network interface card, it means there is just one ip address. One ip address, there are also two resolving ways (If there's error, just tell me), one is virtual host (it needs to configure DNS server A record.), the other is multi directories. In there, I want to tell the second one.

What it looks like

The multi directories deploy way, call it sub-uri way, is look like this:
  • resuming the domain is "pfg.com" (ip address is ok),
  • there are two or more rails applications, app1, app2, ... appN
  • ubuntu 8.04 server
  • web server is apache2.2
  • Ruby 1.8.6
  • mongrel-1.1.5 (it's option)
  • Rails 2.1.x or Rails 2.2.x (a little difference)
http://pfg.com/app1
http://pfg.com/app2
...
http://pfg.com/appN

Next, I will tell you how to configure them.

How to configure them in Rails application

Three step we need to configure, one is configure apache2.2, second is rails application, the last one is about mongrel.

Configure apache2.2

At first, I want to the rails applications run in CGI way.
We add a VirtualHost block, listening all ports

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

After this, we need to create a DocumentRoot, and all the symlinks of the apps put in here (don't link to 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

After this, we have virtualhost and documentroot, the next we need to add Alias directive, for all rails applications.

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

The alias name is the sub-uri. Attention here: don't make the sub-uri name the same as symlinks' name, it's important

After this, we need to configure Directory blocks, like this:

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

after this, the apache2.2 configuration is finished. Want to run in CGI way, we need to configure the rails application too. So, read the next.

Second, configure rails application

Enter you rails application, do it like this:

# 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.

no this configure, when access the website, it will show error: routes error: { /myapp1/ error} Get.

Configure 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.

All the other rails applications should do like this again.

restart your apache2.2 server, and then you can access them: http://pfg.com/myapp1/, http://pfg.com/yourapp2/, http://pfg.com/hisappN.

If you just want to run in CGI way, then it's the end of you.

Third, Add mongrel-1.1.5

Mongrel run on apache2.2, apache2.2 using proxy.

first, configure mongrel, enter to your rails application:

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

you can test the mongrel first, http://127.0.0.1:8000/myapp1/...

then, configure apache2.2 again, add this content:

...
    <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]
...

After this, everything is finished.

Attention

TODO