HowTo Install Redmine in a sub-URI » History » Version 18

David Navarro Solans, 2019-02-08 15:23

1 15 Simon Deziel
{{toc}}
2 15 Simon Deziel
3 3 Jean-Philippe Lang
h1. HowTo Install Redmine in a sub-URI
4 1 Jean-Baptiste Barth
5 2 Jean-Baptiste Barth
This page explains how to run Redmine in a subdirectory of your site, for instance @http://www.mysite.com/redmine/@ ; in such a case, you can feel lost because the classic Redmine install does not work directly, and the links to css or javascript files seem to be broken. In this page, we assume you want to run Redmine under "/redmine/" subdirectory of your site.
6 1 Jean-Baptiste Barth
7 16 Evgeniy Dushistov
h2. Working variant (Sun Jun 26 13:48:50 MSK 2016)
8 16 Evgeniy Dushistov
9 16 Evgeniy Dushistov
Change the following lines at the bottom of your config/environment.rb
10 16 Evgeniy Dushistov
11 16 Evgeniy Dushistov
<pre><code lang="ruby">
12 16 Evgeniy Dushistov
# Initialize the Rails application
13 16 Evgeniy Dushistov
Rails.application.initialize!
14 16 Evgeniy Dushistov
</code></pre>
15 16 Evgeniy Dushistov
16 16 Evgeniy Dushistov
to 
17 16 Evgeniy Dushistov
18 16 Evgeniy Dushistov
<pre><code lang="ruby">
19 16 Evgeniy Dushistov
RedmineApp::Application.routes.default_scope = "/redmine"
20 16 Evgeniy Dushistov
# Initialize the Rails application
21 16 Evgeniy Dushistov
Rails.application.initialize!
22 16 Evgeniy Dushistov
</code></pre>
23 16 Evgeniy Dushistov
24 16 Evgeniy Dushistov
25 6 Luca Pireddu
h2.  Using Redmine::Utils (preferred solution)
26 6 Luca Pireddu
27 6 Luca Pireddu
Add the following line at the bottom of your config/environment.rb
28 6 Luca Pireddu
29 6 Luca Pireddu
<pre>
30 6 Luca Pireddu
Redmine::Utils::relative_url_root = "/redmine"
31 6 Luca Pireddu
</pre>
32 6 Luca Pireddu
33 6 Luca Pireddu
Then restart your application.
34 6 Luca Pireddu
35 6 Luca Pireddu
h2. Using Rails features
36 1 Jean-Baptiste Barth
37 1 Jean-Baptiste Barth
Add the following line at the end of your config/environment.rb :
38 1 Jean-Baptiste Barth
<pre>
39 1 Jean-Baptiste Barth
ActionController::AbstractRequest.relative_url_root = "/redmine"
40 4 Michael Vance
</pre>Rails will then prefix all links with "/redmine". It can be considered as the simplest, cleanest and most flexible solution. Then restart your application. In more recent versions of Rails the class hierarchy has changed slightly and you will need to use
41 4 Michael Vance
<pre>
42 4 Michael Vance
ActionController::Base.relative_url_root = "/redmine"
43 5 Michael Vance
</pre>for the class name.
44 1 Jean-Baptiste Barth
45 1 Jean-Baptiste Barth
h2. Using Mongrel features
46 1 Jean-Baptiste Barth
47 2 Jean-Baptiste Barth
If you run Redmine under Mongrel server, you can alternatively use the "--prefix" option of Mongrel :
48 1 Jean-Baptiste Barth
<pre>
49 2 Jean-Baptiste Barth
mongrel_rails start --prefix=/redmine
50 7 Tiemo Vorschuetz
</pre>
51 7 Tiemo Vorschuetz
_*Mongrel_rails service "--prefix" directive does NOT work with Rails 2.3.x*
52 7 Tiemo Vorschuetz
To fix this issue create a file config/initializers/patch_for_mongrel.rb [name of file can be anything]:_
53 7 Tiemo Vorschuetz
<pre>
54 7 Tiemo Vorschuetz
# Fix for mongrel which still doesn't know about Rails 2.2's changes, 
55 7 Tiemo Vorschuetz
# We provide a backwards compatible wrapper around the new
56 7 Tiemo Vorschuetz
# ActionController::base.relative_url_root,
57 7 Tiemo Vorschuetz
# so it can still be called off of the actually non-existing
58 7 Tiemo Vorschuetz
# AbstractRequest class.
59 7 Tiemo Vorschuetz
60 7 Tiemo Vorschuetz
module ActionController
61 7 Tiemo Vorschuetz
  class AbstractRequest < ActionController::Request
62 7 Tiemo Vorschuetz
    def self.relative_url_root=(path)
63 7 Tiemo Vorschuetz
      ActionController::Base.relative_url_root=(path)
64 7 Tiemo Vorschuetz
    end
65 7 Tiemo Vorschuetz
    def self.relative_url_root
66 7 Tiemo Vorschuetz
      ActionController::Base.relative_url_root
67 7 Tiemo Vorschuetz
    end
68 7 Tiemo Vorschuetz
  end
69 7 Tiemo Vorschuetz
end
70 7 Tiemo Vorschuetz
#
71 7 Tiemo Vorschuetz
# Thanks to http://www.ruby-forum.com/topic/190287
72 7 Tiemo Vorschuetz
</pre>
73 7 Tiemo Vorschuetz
74 7 Tiemo Vorschuetz
You may not run Mongrel on port 80 : if you have an Apache server on the same host, and you run Mongrel on port 8000, you can use the following Apache config to redirect (with Apache's mod_proxy enabled) :
75 1 Jean-Baptiste Barth
<pre>
76 1 Jean-Baptiste Barth
ProxyPass /redmine http://localhost:8000/redmine
77 1 Jean-Baptiste Barth
ProxyPassReverse /redmine http://localhost:8000/redmine
78 1 Jean-Baptiste Barth
</pre>
79 1 Jean-Baptiste Barth
80 1 Jean-Baptiste Barth
h2. Using Passenger (aka mod_rails) features
81 1 Jean-Baptiste Barth
82 9 Zack s
If you run Redmine under Apache web server with Phusion Passenger module, you can follow "this guide":http://www.modrails.com/documentation/Users%20guide%20Apache.html#deploying_rails_to_sub_uri ; please note it won't work correctly on some versions of Passenger or some Linux distributions.
83 1 Jean-Baptiste Barth
84 12 shaun williams
h2. Using Passenger+Nginx features
85 12 shaun williams
86 12 shaun williams
See "official guide":http://modrails.com/documentation/Users%20guide%20Nginx.html#deploying_rails_to_sub_uri (This is the only solution that worked for me - 30 Oct 2012)
87 12 shaun williams
88 13 Yehuda Katz
h2. Using Unicorn+Nginx
89 13 Yehuda Katz
90 13 Yehuda Katz
nginx config:
91 13 Yehuda Katz
<pre>
92 13 Yehuda Katz
location /redmine {
93 13 Yehuda Katz
        alias   <PATH_TO>/redmine/public;
94 13 Yehuda Katz
95 13 Yehuda Katz
        try_files $uri/index.html $uri.html $uri @app;
96 13 Yehuda Katz
     }
97 13 Yehuda Katz
</pre>
98 13 Yehuda Katz
99 13 Yehuda Katz
config/routes.rb
100 13 Yehuda Katz
<pre>
101 13 Yehuda Katz
102 13 Yehuda Katz
Redmine::Utils::relative_url_root = "/redmine"
103 13 Yehuda Katz
104 13 Yehuda Katz
RedmineApp::Application.routes.draw do
105 14 Yehuda Katz
scope Redmine::Utils::relative_url_root do
106 13 Yehuda Katz
  root :to => 'welcome#index', :as => 'home'
107 13 Yehuda Katz
.....
108 13 Yehuda Katz
end
109 13 Yehuda Katz
end
110 13 Yehuda Katz
</pre>
111 13 Yehuda Katz
112 1 Jean-Baptiste Barth
h2. With a reverse proxy
113 1 Jean-Baptiste Barth
114 1 Jean-Baptiste Barth
If you have an Apache webserver in front of it (with mod_proxy enabled), or an Apache reverse proxy on another machine, you can run Redmine on a specific port and use this kind of config so it appears to be running in a subdirectory :
115 1 Jean-Baptiste Barth
<pre>
116 1 Jean-Baptiste Barth
ProxyPass /redmine http://real-redmine-server.localdomain:3000/
117 1 Jean-Baptiste Barth
ProxyPassReverse /redmine http://real-redmine-server.localdomain:3000/
118 2 Jean-Baptiste Barth
</pre>See Apache official documentation to adapt it.
119 1 Jean-Baptiste Barth
120 17 Cyril Jouve
h2. With puma
121 17 Cyril Jouve
122 17 Cyril Jouve
# Define "RAILS_RELATIVE_URL_ROOT environment variable":http://guides.rubyonrails.org/configuring.html#deploy-to-a-subdirectory-relative-url-root :
123 17 Cyril Jouve
<pre><code class="bash>
124 17 Cyril Jouve
export RAILS_RELATIVE_URL_ROOT=/redmine
125 17 Cyril Jouve
</code></pre> or use [[HowTo_Install_Redmine_in_a_sub-URI#Using-RedmineUtils-preferred-solution|RedmineUtils method]]
126 17 Cyril Jouve
This allows rails to generate urls prefixed by RAILS_RELATIVE_URL_ROOT.
127 17 Cyril Jouve
# Then update you config.ru:
128 17 Cyril Jouve
<pre><code class="ruby">
129 17 Cyril Jouve
# This file is used by Rack-based servers to start the application.
130 17 Cyril Jouve
131 17 Cyril Jouve
require ::File.expand_path('../config/environment',  __FILE__)
132 17 Cyril Jouve
map ENV['RAILS_RELATIVE_URL_ROOT'] || '/' do
133 17 Cyril Jouve
  run RedmineApp::Application
134 17 Cyril Jouve
end
135 17 Cyril Jouve
</code></pre> This allows rack to handle urls prefixed with RAILS_RELATIVE_URL_ROOT.
136 17 Cyril Jouve
137 17 Cyril Jouve
Both points are necessary.
138 1 Jean-Baptiste Barth
139 18 David Navarro Solans
h2. If using Docker official image, passenger variant
140 18 David Navarro Solans
141 18 David Navarro Solans
Create a _Dockerfile_ in an empty directory with the following contents, adjusting the tag to the version you want to use:
142 18 David Navarro Solans
143 18 David Navarro Solans
<pre>
144 18 David Navarro Solans
FROM redmine:2.6.10-passenger
145 18 David Navarro Solans
COPY assets/passenger-nginx-config-template.erb /passenger-nginx-config-template.erb
146 18 David Navarro Solans
CMD ["passenger", "start", "--nginx-config-template", "/passenger-nginx-config-template.erb"]
147 18 David Navarro Solans
</pre>
148 18 David Navarro Solans
149 18 David Navarro Solans
Create a _passenger-nginx-config-template.erb_ file in the same directory with the following contents, adjusting the sub-URI you want to use:
150 18 David Navarro Solans
151 18 David Navarro Solans
<pre>
152 18 David Navarro Solans
<%= include_passenger_internal_template('global.erb') %>
153 18 David Navarro Solans
154 18 David Navarro Solans
worker_processes 1;
155 18 David Navarro Solans
events {
156 18 David Navarro Solans
    worker_connections 4096;
157 18 David Navarro Solans
}
158 18 David Navarro Solans
159 18 David Navarro Solans
http {
160 18 David Navarro Solans
    <%= include_passenger_internal_template('http.erb', 4) %>
161 18 David Navarro Solans
162 18 David Navarro Solans
    default_type application/octet-stream;
163 18 David Navarro Solans
    types_hash_max_size 2048;
164 18 David Navarro Solans
    server_names_hash_bucket_size 64;
165 18 David Navarro Solans
    client_max_body_size 1024m;
166 18 David Navarro Solans
    access_log off;
167 18 David Navarro Solans
    keepalive_timeout 60;
168 18 David Navarro Solans
    underscores_in_headers on;
169 18 David Navarro Solans
    gzip on;
170 18 David Navarro Solans
    gzip_comp_level 3;
171 18 David Navarro Solans
    gzip_min_length 150;
172 18 David Navarro Solans
    gzip_proxied any;
173 18 David Navarro Solans
    gzip_types text/plain text/css text/json text/javascript
174 18 David Navarro Solans
        application/javascript application/x-javascript application/json
175 18 David Navarro Solans
        application/rss+xml application/vnd.ms-fontobject application/x-font-ttf
176 18 David Navarro Solans
        application/xml font/opentype image/svg+xml text/xml;
177 18 David Navarro Solans
178 18 David Navarro Solans
    server {
179 18 David Navarro Solans
        server_name _;
180 18 David Navarro Solans
        listen 0.0.0.0:3000;
181 18 David Navarro Solans
        root '/usr/src/redmine/public';
182 18 David Navarro Solans
        passenger_app_env 'production';
183 18 David Navarro Solans
        passenger_spawn_method 'smart';
184 18 David Navarro Solans
        passenger_load_shell_envvars off;
185 18 David Navarro Solans
186 18 David Navarro Solans
        location ~ ^/suburi(/.*|$) {
187 18 David Navarro Solans
            alias /usr/src/redmine/public$1;
188 18 David Navarro Solans
            passenger_base_uri /suburi;
189 18 David Navarro Solans
            passenger_app_root /usr/src/redmine;
190 18 David Navarro Solans
            passenger_document_root /usr/src/redmine/public;
191 18 David Navarro Solans
            passenger_enabled on;
192 18 David Navarro Solans
        }
193 18 David Navarro Solans
    }
194 18 David Navarro Solans
195 18 David Navarro Solans
    passenger_pre_start http://0.0.0.0:3000/;
196 18 David Navarro Solans
}
197 18 David Navarro Solans
</pre>
198 18 David Navarro Solans
199 18 David Navarro Solans
Build the Docker image and launch a container:
200 18 David Navarro Solans
201 18 David Navarro Solans
<pre>
202 18 David Navarro Solans
docker build -t redmine-in-a-sub-uri:2.6.10-passenger .
203 18 David Navarro Solans
docker run -d --name redmine-in-a-sub-uri -p 3000:3000 redmine-in-a-sub-uri:2.6.10-passenger
204 18 David Navarro Solans
</pre>
205 18 David Navarro Solans
206 18 David Navarro Solans
The resultant Redmine can be accessed in http://localhost:3000/suburi
207 18 David Navarro Solans
208 2 Jean-Baptiste Barth
h2. Old versions of Redmine and Rails
209 1 Jean-Baptiste Barth
210 2 Jean-Baptiste Barth
If you run a very old version of Redmine (don't know exactly which ones), maybe your version of Rails' ActionController does not support the "relative_url_root" mentionned above. Then you can look at "this page":https://www.riscosopen.org/wiki/documentation/pages/Running+Rails+applications+from+subdirectories/versions/16 to reproduce the same behaviour, but it is NOT a very good idea in most cases, you should consider upgrading Redmine.
211 2 Jean-Baptiste Barth
212 1 Jean-Baptiste Barth
h2. References
213 1 Jean-Baptiste Barth
214 8 Zack s
If this page did not answered your problems, you can see #2508 or "this thread":http://www.redmine.org/boards/2/topics/2244.
215 10 Zack s
216 10 Zack s
Windows : Configuring Ruby On Rails App in a subdirectory under Apache - http://stackoverflow.com/a/470973/663172
217 11 Zack s
218 11 Zack s
Configuring ruby on rails Action Controller - http://edgeguides.rubyonrails.org/configuring.html#configuring-action-controller