Project

General

Profile

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