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

Jiongliang Zhang, 2010-01-22 14:17
T.B.D.

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