Project

General

Profile

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