Sub URI for multisites at one domain » History » Version 3

Mischa The Evil, 2010-10-14 05:17
Changed TBD-abbreviation to TODO and fixed TOC

1 3 Mischa The Evil
h1. Sub URI for multisites at one domain
2 3 Mischa The Evil
3 1 Jiongliang Zhang
{{>toc}}
4 1 Jiongliang Zhang
5 1 Jiongliang Zhang
h2. Why we need SubURI in one domain for multisites
6 1 Jiongliang Zhang
7 1 Jiongliang Zhang
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.
8 1 Jiongliang Zhang
9 1 Jiongliang Zhang
h2. What it looks like
10 1 Jiongliang Zhang
11 1 Jiongliang Zhang
The multi directories deploy way, call it sub-uri way, is look like this:
12 1 Jiongliang Zhang
* resuming the domain is "pfg.com" (ip address is ok), 
13 1 Jiongliang Zhang
* there are two or more rails applications, app1, app2, ... appN
14 2 Jiongliang Zhang
* ubuntu 8.04 server
15 1 Jiongliang Zhang
* web server is apache2.2
16 1 Jiongliang Zhang
* Ruby 1.8.6
17 2 Jiongliang Zhang
* mongrel-1.1.5 (it's option)
18 2 Jiongliang Zhang
* Rails 2.1.x or Rails 2.2.x (a little difference)
19 1 Jiongliang Zhang
20 1 Jiongliang Zhang
<pre>
21 1 Jiongliang Zhang
http://pfg.com/app1
22 1 Jiongliang Zhang
http://pfg.com/app2
23 1 Jiongliang Zhang
...
24 1 Jiongliang Zhang
http://pfg.com/appN
25 1 Jiongliang Zhang
</pre>
26 1 Jiongliang Zhang
27 1 Jiongliang Zhang
Next, I will tell you how to configure them.
28 1 Jiongliang Zhang
29 1 Jiongliang Zhang
h2. How to configure them in Rails application
30 1 Jiongliang Zhang
31 2 Jiongliang Zhang
Three step we need to configure, one is configure apache2.2, second is rails application, the last one is about mongrel.
32 2 Jiongliang Zhang
33 2 Jiongliang Zhang
h3. Configure apache2.2
34 2 Jiongliang Zhang
35 2 Jiongliang Zhang
At first, I want to the rails applications run in CGI way. 
36 2 Jiongliang Zhang
We add a VirtualHost block, listening all ports
37 2 Jiongliang Zhang
<pre>
38 2 Jiongliang Zhang
NameVirtualHost *
39 2 Jiongliang Zhang
<VirtualHost *>
40 2 Jiongliang Zhang
    ...
41 2 Jiongliang Zhang
</VirtualHost>
42 2 Jiongliang Zhang
</pre>
43 2 Jiongliang Zhang
44 2 Jiongliang Zhang
After this, we need to create a DocumentRoot, and all the symlinks of the apps put in here (don't link to app*/public).
45 2 Jiongliang Zhang
46 2 Jiongliang Zhang
<pre>
47 2 Jiongliang Zhang
<VirtualHost *>
48 2 Jiongliang Zhang
    DocumentRoot "/var/www/"
49 2 Jiongliang Zhang
</VirtualHost>
50 2 Jiongliang Zhang
51 2 Jiongliang Zhang
#ls -l /var/www/
52 2 Jiongliang Zhang
-lrwxrwxrwx ..... app1 -> /home/jean/app1
53 2 Jiongliang Zhang
-lrwxrwxrwx ..... app2 -> /home/jiong/app2
54 2 Jiongliang Zhang
...
55 2 Jiongliang Zhang
-lrwxrwxrwx ..... appN -> /home/eric/appN
56 2 Jiongliang Zhang
</pre>
57 2 Jiongliang Zhang
58 2 Jiongliang Zhang
After this, we have virtualhost and documentroot, the next we need to add Alias directive, for all rails applications.
59 2 Jiongliang Zhang
60 2 Jiongliang Zhang
<pre>
61 2 Jiongliang Zhang
<VirtualHost *>
62 2 Jiongliang Zhang
    DocumentRoot "/var/www/"
63 2 Jiongliang Zhang
    Alias /myapp1 "/var/www/app1/public/"
64 2 Jiongliang Zhang
    Alias /yourapp2 "/var/www/app2/public/"
65 2 Jiongliang Zhang
    Alias /hisappN "/var/www/appN/public"
66 2 Jiongliang Zhang
</VirtualHost>
67 2 Jiongliang Zhang
</pre>
68 2 Jiongliang Zhang
69 2 Jiongliang Zhang
The alias name is the sub-uri. *Attention here: don't make the sub-uri name the same as symlinks' name, it's important*
70 2 Jiongliang Zhang
71 2 Jiongliang Zhang
After this, we need to configure Directory blocks, like this:
72 2 Jiongliang Zhang
73 2 Jiongliang Zhang
<pre>
74 2 Jiongliang Zhang
...
75 2 Jiongliang Zhang
    Alias /myapp1 "/var/www/app1/public/"
76 2 Jiongliang Zhang
    <Directory "/var/www/app1/public/">
77 2 Jiongliang Zhang
         Options FollowSymLinks +ExecCGI
78 2 Jiongliang Zhang
         AllowOverride all
79 2 Jiongliang Zhang
         Order allow,deny
80 2 Jiongliang Zhang
         Allow from all
81 2 Jiongliang Zhang
    </Directory>
82 2 Jiongliang Zhang
...
83 2 Jiongliang Zhang
</pre>
84 2 Jiongliang Zhang
85 2 Jiongliang Zhang
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.
86 2 Jiongliang Zhang
87 2 Jiongliang Zhang
h3. Second, configure rails application
88 2 Jiongliang Zhang
89 2 Jiongliang Zhang
Enter you rails application, do it like this:
90 2 Jiongliang Zhang
<pre>
91 2 Jiongliang Zhang
# cd /home/jiong/app1/config/
92 2 Jiongliang Zhang
# vim environment.rb
93 2 Jiongliang Zhang
=> add code:
94 2 Jiongliang Zhang
Rails 2.1.x: add *ActionController::AbstractRequest.relative_url_root="/myapp1"* At the end of file (must the end of file.)
95 2 Jiongliang Zhang
Rails 2.2.x, two way: 
96 2 Jiongliang Zhang
    one is: add *ActionController::Base.relative_url_root="/myapp1"* At the end of file
97 2 Jiongliang Zhang
    other : add *config.action_controller.relative_url_root="/myapp1"* in the block.
98 2 Jiongliang Zhang
</pre>
99 2 Jiongliang Zhang
100 2 Jiongliang Zhang
no this configure, when access the website, it will show error: routes error: { /myapp1/ error} Get.
101 2 Jiongliang Zhang
102 2 Jiongliang Zhang
Configure dispatch.cgi:
103 2 Jiongliang Zhang
<pre>
104 2 Jiongliang Zhang
# cd /home/jiong/app1/public/
105 2 Jiongliang Zhang
# mv dispatch.cgi.example dispatch.cgi
106 2 Jiongliang Zhang
# chmod a+x dispatch.cgi
107 2 Jiongliang Zhang
# which ruby
108 2 Jiongliang Zhang
/usr/local/bin/ruby => It depend on your system.
109 2 Jiongliang Zhang
# vim dispatch.cgi
110 2 Jiongliang Zhang
=> change the first line to #!/usr/local/bin/ruby
111 2 Jiongliang Zhang
test dispatch.cgi.
112 2 Jiongliang Zhang
# sudo -u www-data ./dispatch.cgi
113 2 Jiongliang Zhang
=> success: will show the home page source code for you.
114 2 Jiongliang Zhang
</pre>
115 2 Jiongliang Zhang
116 2 Jiongliang Zhang
All the other rails applications should do like this again. 
117 2 Jiongliang Zhang
118 2 Jiongliang Zhang
restart your apache2.2 server, and then you can access them: http://pfg.com/myapp1/, http://pfg.com/yourapp2/, http://pfg.com/hisappN.
119 2 Jiongliang Zhang
120 2 Jiongliang Zhang
If you just want to run in CGI way, then it's the end of you.
121 2 Jiongliang Zhang
122 2 Jiongliang Zhang
h3. Third, Add mongrel-1.1.5
123 2 Jiongliang Zhang
124 2 Jiongliang Zhang
Mongrel run on apache2.2, apache2.2 using proxy.
125 2 Jiongliang Zhang
126 2 Jiongliang Zhang
first, configure mongrel, enter to your rails application:
127 2 Jiongliang Zhang
<pre>
128 2 Jiongliang Zhang
# cd /home/jiong/app1/
129 2 Jiongliang Zhang
# mongrel_rails <TBD.>
130 2 Jiongliang Zhang
# ln -s /home/jiong/app1/config/mongrel_cluster.yml /etc/mongrel_cluster/app1.yml
131 2 Jiongliang Zhang
</pre>
132 2 Jiongliang Zhang
133 2 Jiongliang Zhang
you can test the mongrel first, http://127.0.0.1:8000/myapp1/...
134 2 Jiongliang Zhang
135 2 Jiongliang Zhang
then, configure apache2.2 again, add this content:
136 2 Jiongliang Zhang
<pre>
137 2 Jiongliang Zhang
...
138 2 Jiongliang Zhang
    <Directory ...>
139 2 Jiongliang Zhang
       ...
140 2 Jiongliang Zhang
    </Directory>
141 2 Jiongliang Zhang
142 2 Jiongliang Zhang
    <Proxy balancer://app1_cluster>
143 2 Jiongliang Zhang
        Order allow,deny
144 2 Jiongliang Zhang
        Allow from all
145 2 Jiongliang Zhang
        balancer 127.0.0.1:8000
146 2 Jiongliang Zhang
        balancer 127.0.0.1:8001
147 2 Jiongliang Zhang
        balancer 127.0.0.1:8002
148 2 Jiongliang Zhang
    </Proxy>
149 2 Jiongliang Zhang
    RewriteCond <TBD.>
150 2 Jiongliang Zhang
    RewriteRule ^/app1/?(.*)$ balancer://app1_cluster/$1 [QSA,L]
151 2 Jiongliang Zhang
...
152 2 Jiongliang Zhang
</pre>
153 2 Jiongliang Zhang
154 2 Jiongliang Zhang
After this, everything is finished.
155 2 Jiongliang Zhang
156 2 Jiongliang Zhang
h2. Attention
157 1 Jiongliang Zhang
158 3 Mischa The Evil
TODO