Project

General

Profile

HowTo configure Apache to run Redmine » History » Version 22

Anton Kovalenko, 2011-09-24 11:50
have forgotten to mention help.* as an exception for rewrite rule

1 1 Cyber Sprocket
h1. HowTo configure Apache to run Redmine
2
3 7 Mischa The Evil
{{>toc}}
4 1 Cyber Sprocket
5 2 Cyber Sprocket
These notes assume you already have Redmine running via the webrick server and are looking to get Redmine running via your existing Apache installation.   Most of the commands assume you are in the root installation directory of redmine, so be sure to change directory there before starting.
6 1 Cyber Sprocket
7
h2. For CentOS 5
8
9
h3. Assumptions
10
11
* OS is CentOS 5
12
* Web server is Apache 2
13
** mod_cgi is enabled
14
** name based virtual servers are being used
15 2 Cyber Sprocket
** the web server runs under the user apache, group apache
16 1 Cyber Sprocket
17 4 Cyber Sprocket
h3. Myths
18
19
* You do not need to run mod_fcgid
20
* You do not need to run mod_fastcgi
21 8 William Baum
 
22
bq. -This section needs work. I can't tell if these are the myths or the corrections to the myths.
23
_(note: if someone were so generous to post working, known good, HOWTO steps here for enabling FastCGI, that would be highly valuable to the entire community.- It is reported that www.redmine.org itself runs FastCGI, so it is obviously a valid, worthwhile, stable, performing way to run this great great web app'.)_
24 5 Brad Mace
25 14 Cyber Sprocket
bq. I have added sections detailing the installation and configuration of mod_fastcgi and mod_fcgid below. --wmbaum, (2009-08-30)
26 1 Cyber Sprocket
27 14 Cyber Sprocket
bq. We had problems with getting Rails 2.3.5 working with cgi and fastcgi.  fcgid on Apache2 worked great for 9.3. We tweaked some of the notes based on the recent fcgid updates. -- "cybersprocket":http://www.cybersprocket.com, (2010-04-25)
28
29 1 Cyber Sprocket
h3. Basic Steps
30
31 11 Patrick OMalley
* Install Redmine per the [[RedmineInstall| installation instructions]] and get it running with webrick.
32 1 Cyber Sprocket
33
* Kill the webrick session
34
35
* Copy the public/dispatch.cgi.example to public/dispatch.cgi
36
37 18 Matt Brown
* Edit public/dispatch.cgi to fix the shell script invocation to point to the ruby executable:
38 1 Cyber Sprocket
  @#!/usr/local/bin/ruby@
39 9 Patrick OMalley
  or 
40 1 Cyber Sprocket
  @#!/usr/bin/ruby@
41 18 Matt Brown
  or
42
  @#!c:/ruby_root/bin/ruby.exe@
43 1 Cyber Sprocket
44 10 Patrick OMalley
* Also in public/dispatch.cgi, you may need to change the require line to an absolute path as stated in the comment to something like
45
  @require "/usr/lib/ruby/gems/1.8/gems/rails-2.1.2/lib/dispatcher.rb"@
46 19 Matt Brown
  or
47
  @require "c:/ruby_root/lib/ruby/gems/1.8/gems/rails-2.3.11/lib/dispatcher.rb"@
48 10 Patrick OMalley
49 1 Cyber Sprocket
* Make sure public/dispatch.cgi has execute permissions via:
50
  @# chmod 755 public/dispatch.cgi@
51
52
* Update the config/environment.rb file to force the rails environment to production, simply uncomment this line at the start of the file:
53
  @ENV['RAILS_ENV'] ||= 'production'@
54
55 3 Cyber Sprocket
* Add your virtual host entry to the apache configuration file (/etc/httpd/conf/httpd.conf).  We installed redmine into the /live/redmine folder on our server. _Note: be sure to point your DocumentRoot to the public sub-folder!_
56
57 1 Cyber Sprocket
<pre>
58
    <VirtualHost *:80>
59
        ServerName redmine.<YOUR-DOMAIN>.com
60
        ServerAdmin webmaster@<YOUR-DOMAIN>.com
61
        DocumentRoot /live/redmine/public/
62
        ErrorLog logs/redmine_error_log
63
64 21 Anton Kovalenko
        #If you are using mod_fcgid and are going to upload files larger than
65
        #131072 bytes you should consider adding the following line
66
        #that allows to upload files up to 20 mb
67
        MaxRequestLen 20971520
68
69 1 Cyber Sprocket
        <Directory "/live/redmine/public/">
70
                Options Indexes ExecCGI FollowSymLinks
71
                Order allow,deny
72
                Allow from all
73
                AllowOverride all
74
        </Directory>
75
    </VirtualHost>
76
</pre>
77
78
* Make sure your files, log, tmp, and vendor directories are all accessible (read/write) by user apache, group apache. We did that via a change of ownership:
79
  @# chown -R apache:apache files log tmp vendor@
80
81
82 2 Cyber Sprocket
h3. Error Messages and Resolutions
83 1 Cyber Sprocket
84 2 Cyber Sprocket
  * @Rails requires RubyGems >= 0.9.4. Please install RubyGems@
85 1 Cyber Sprocket
    Look for rogue versions of ruby binaries.  We had an older version in /usr/bin/ruby as well as /usr/local/bin/ruby.
86
87 2 Cyber Sprocket
  * @Premature script headers@
88
    This is the generic "got something before the Content-Type: header in a CGI script" error from Apache.  Run dispatch.cgi (see below) and see what comes out BEFORE the Content-Type: directive.
89
90 8 William Baum
h3. Helpful Commands
91 2 Cyber Sprocket
92
 * @# which ruby@
93
   tells you which ruby binary is being run when the fully-qualified-filename has not been specified.
94
95
 * @# find / -name ruby@
96
   searches your entire system for any file named ruby, warning: can take a while on large filesystems.
97
 
98
 * @# ruby -v@
99 1 Cyber Sprocket
   tell you what version of ruby you are running by default
100
101
 * @#public/dispatch.cgi@
102
   runs the dispatch CGI script.   It should spit out HTML that start with @Content-Type: text/html; charset=utf-8@, if ANYTHING precedes the Content-Type text you will get a "premature script headers" error in the Apache log files.
103 8 William Baum
104
105
h2. mod_fastcgi
106
107 17 André Jonsson
I suggest getting redmine running with mod_cgi above, not only to verify your basic redmine and apache configuration, but also so you can appreciate the perfomance gains you'll get from mod_fastcgi or mod_fcgid.
108 8 William Baum
109
We'll start with "mod_fastcgi":http://www.fastcgi.com/.  
110
111
Install prerequisites:
112
113
<pre>
114
yum install libtool httpd-devel apr-devel apr
115
</pre>
116
117
h3. Download and Install mod_fastcgi
118
119
<pre>
120
cd /usr/local/src/
121
wget http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz
122
123
tar -zxvf mod_fastcgi-current.tar.gz
124
cd mod_fastcgi-2.4.6/
125
cp Makefile.AP2 Makefile 
126
make top_dir=/usr/lib/httpd
127
make install top_dir=/usr/lib/httpd
128
</pre>
129
130
Create or edit @/etc/httpd/conf.d/mod_fastcgi.conf@
131
<pre>
132
LoadModule fastcgi_module modules/mod_fastcgi.so
133
<IfModule mod_fastcgi.c>
134
FastCgiIpcDir /tmp/fcgi_ipc/
135
</IfModule>
136
</pre>
137
138
The @/tmp/fcgi_ipc/@ directory needs to be writable to the apache user:
139
<pre>
140
chown -R apache.apache /tmp/fcgi_ipc/
141
chmod -R 777 /tmp/fcgi_ipc/
142
</pre>
143
144
> Note:  I had to do this more than once.. It created directories which it then didn't own.. ??
145
146
h3. Download and install @fcgi@ (for fcgi gem)
147
148
<pre>
149
wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
150
tar -zxvf fcgi-2.4.0.tar.gz
151
cd fcgi-2.4.0
152
./configure
153
make
154
make install
155
</pre>
156
157
h3. Install fcgi gem:
158
159
<pre>
160
gem install fcgi
161
</pre>
162
163
h2. Configuring redmine to use fastcgi:
164
165
In your redmine/public/ directory, copy @dispatch.fcgi.example@ to @dispatch.fcgi@
166
> Note: Mine was shebanged to "#!/usr/bin/env ruby", which is fine.  I found a reference or two that seemed to indicate the 'env' bit is preferable to calling ruby directly.  If this doesn't work, then you'll need to change it to wherever your ruby is as above.
167
168
@./public/.htaccess@
169
<pre>
170 22 Anton Kovalenko
#RewriteRule ^(stylesheets.*|images.*|favicon.*|javascripts.*|plugin_assets.*|themes.*|help.*)$ $1 [L]
171 8 William Baum
#<IfModule mod_fastcgi.c>
172
#       RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
173
#</IfModule>
174
#<IfModule mod_fcgid.c>
175
#       RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
176
#</IfModule>
177
#<IfModule mod_cgi.c>
178
#       RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
179
#</IfModule>
180
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
181
</pre>
182
183
The default .htaccess will use cgi if it's available, so we need to force fcgi.  You could perhaps rearrange the directives to prefer fcgi -- I just commented out the others and forced it with <code>RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]</code>  You can see which one is actually in use with <code>ps gaux</code>
184
185
Give it a whirl:
186
<pre>
187
/etc/init.d/httpd configtest
188
/etc/init.d/httpd restart
189
</pre>
190
191
h2. Additional Apache Configuration
192
193
When I first fired up apache with redmine, apache started very slowly and sucked up a significantly larger chunk of RAM than normal.  Further investigation revealed that it had fired off *8* @ruby .../redmine/public/dispatch.fcgi@ processes! No wonder it was slow. 
194
195
I was running redmin under the apache default VirtualHost, and the default StartServers, MinSpareServers, etc. applied.  You can adjust the defaults in @/etc/httpd/conf/httpd.conf@ or even better is to run redmine under a @NameVirtualHost@ or a different @VirtualHost@.  This prevents apache from firing off a bunch of extraneous processes, and @NameVirtualHost@ should allow you to configure multiple redmine environments on the same IP without wasting a bunch of resources.
196
197
If you're having issues with apache virtual hosts, this can be very helpful:
198
<pre>
199
/usr/sbin/httpd -t -D DUMP_VHOSTS
200
</pre>
201
202
h2. mod_fcgid
203 1 Cyber Sprocket
204 14 Cyber Sprocket
h3. fcgid from Apache
205
206
"Official Apache mod_fcgid":http://httpd.apache.org/mod_fcgid/ this is the Apache version, seems newer and we had more luck with this than the Coremail hosted version below.
207
208
<pre>
209
cd /usr/local/src/
210
wget http://apache.mirrors.hoobly.com/httpd/mod_fcgid/mod_fcgid-2.3.5.tar.gz
211
tar zxvf mod_fcgid.2.3.5.tgz
212
cd mod_fcgid.2.3.5
213
</pre>
214
215
Configure and Install
216
<pre>
217
./configure.apxs
218
make
219
make install
220
service httpd restart
221
</pre>
222
223
224
225
h3. fcgid from China Coremail service
226
227 8 William Baum
"mod_fcgid":http://fastcgi.coremail.cn/ seems newer and preferable to mod_fastcgi.
228 1 Cyber Sprocket
229 14 Cyber Sprocket
230 1 Cyber Sprocket
<pre>
231 8 William Baum
cd /usr/local/src/
232 14 Cyber Sprocket
wget http://apache.mirrors.hoobly.com/httpd/mod_fcgid/mod_fcgid-2.2.tar.gz
233 13 William Baum
tar zxvf mod_fcgid.2.2.tgz
234
cd mod_fcgid.2.2
235 8 William Baum
</pre>
236
237
Edit Makefile
238
<pre>
239
#top_dir      = /usr/local/apache2
240 1 Cyber Sprocket
top_dir      = /usr/lib/httpd
241 13 William Baum
</pre>
242
243
Build it..
244
<pre>
245
make
246
make install
247 8 William Baum
</pre>
248
249
Edit/create @/etc/httpd/conf.d/mod_fcgid.conf@
250
<pre>
251
LoadModule fcgid_module /usr/lib/httpd/modules/mod_fcgid.so
252
253
<IfModule mod_fcgid.c>
254
    SocketPath /tmp/fcgid_sock/
255
    AddHandler fcgid-script .fcgi
256
</IfModule>
257
</pre>
258
259
Now you should be able to switch between mod_fastcgi and mod_fcgid by renaming one of them to other than *.conf in @/etc/httpd/conf.d/@
260
<pre>
261
cd /etc/httpd/conf.d/
262
mv mod_fastcgi.conf mod_fastcgi.conf.not
263 1 Cyber Sprocket
/etc/init.d/httpd restart
264
</pre>
265 14 Cyber Sprocket
266
267
h3. Either Version, Permissions
268
269
With either fcgid version you may run into problems with the fcgid service starting.   Make sure the socks parent directory (typically /var/log/httpd) has proper permissions.   The default directory permissions for /var/log/httpd is for a standard non-fcgid install of Apache.  You need to add execute permissions for Apache to read/write to the directory properly while it is running fcgid.
270
271
<pre>
272
chmod 755 /var/log/httpd
273
service httpd restart
274
</pre>
275
276 8 William Baum
277
h2. Installation Sources
278
279
In the above steps, I installed from sources only where I didn't find any RPM's in common repo's.  I'm rather surprised that one can't simply @yum install@ mod_fastcgi, mod_fcgid, fcgi, etc., but there we are.  If you find better methods or sources for any of the above, please feel free to update.
280 12 Fred Eisele
281 16 Lu Pon
h2. Ubuntu Server (Version ?) (This not don't work for 8.04 LTS)
282 12 Fred Eisele
283
Install passenger
284
<pre>
285
sudo aptitude install libapache2-mod-passenger
286
</pre>
287
This did switch the type of apache server to worker from prefork but in my case that was acceptable.
288
289
I had installed the redmine tarball into /opt/redmine as redmine-0.8.5 and made a symlink named current.
290
Then make a link to the redmine public directory from the apache DocumentRoot (see /etc/apache2/config.d/*).
291
<pre>
292
ln -s /opt/redmine/current/public /var/www/redmine
293
</pre>
294
295
Updated /etc/apache/config.d/redmine (or wherever your virtualhost is defined) with the following:
296
<pre>
297
RailsEnv production
298
RailsBaseURI /redmine
299
</pre>
300
301
Restart apache.
302 15 Martin Cyr
303
h2. For FreeBSD (and possibly others)
304
305
I found the current instructions to be lacking on some way. With the information above, the scripts and css might not appear correctly, the link get broken and such. Also, some hacks seems to have been lost to history (the #!/bin/bash/ruby hack, wich is not required with this technique). Here is how I got it going.
306
307
Make sure 'Apache 2.2', 'Ruby', 'fastcgi' (including the gem) are installed.
308
First, putting redmine in a sub-directory requires more work and is left for the reader as an exercise to get properly (hint Rewrite is your friend), so from now on, I will assume that redmine will be the DocumentRoot.
309
So here it is, my httpd.conf :
310
<pre>
311
LoadModule fastcgi_module     libexec/apache22/mod_fastcgi.so
312
313
DocumentRoot "/usr/local/www/redmine/public"
314
315
FastCgiServer /usr/local/www/redmine/public/dispatch.fcgi -idle-timeout 120 -initial-env RAILS_ENV=production -initial-env PATH=/usr/local/bin -processes 2
316
317
<Directory /usr/local/www/redmine/public>
318
   AddHandler fastcgi-script fcgi
319
   Order allow,deny
320
   Allow from all
321
   AllowOverride all
322
   Options +FollowSymLinks +ExecCGI
323
   RewriteEngine On
324
   RewriteCond %{REQUEST_FILENAME} !-f
325
   RewriteRule ^(.*)$ dispatch.fcgi
326
</Directory>
327
</pre>
328
329
Now let's explain what happens and why:
330
# _AddHandler fastcgi-script fcgi_: defines that when apache identifies a file with extension fcgi, if should be handled buy the fastcgi-script handler. This definition of a handler is created by the mod_fastcgi.so module (loaded through the LoadModule directive of couse).
331
# _Options +FollowSymLinks +ExecCGI_: tells apache that within this directory, CGI-ish script execution is allowed
332
# _RewriteCond %{REQUEST_FILENAME} !-f_: only rewrite if the file doesn't exist
333
# _RewriteRule ^(.*)$ dispatch.fcgi_: everything goes to dispatch.fcgi
334
# _FastCgiServer /usr/local/www/redmine/public/dispatch.fcgi -idle-timeout 120 -initial-env RAILS_ENV=production -initial-env PATH=/usr/local/bin -processes 2_: This is the main thing. As Scott Laird explains in this "blog":http://scottstuff.net/blog/2005/07/20/apache-tuning-for-rails-and-fastcgi, running FastCGI can run in three modes (see post). This tells FastCGI how to handle our script, with the important part being the initial-env to define what variables should be set in the FastCGI context. As FastCGI starts, it will normally start with a blank (*emtpy*) environment variables, so yeah, your $PATH will be empty, and no /usr/bin/env *will NOT* find the ruby executable in the path and thus failing with the error _env: ruby: No such file or directory_. Also, ruby will not know that he is expected to start in production env. With this line, you don't have to fix your dispatch.fcgi shabang (the #!/usr/bin/env ruby line) nor will you have to change the config/environment.rb file with a ENV['RAILS_ENV'] ||= 'production'.
335
336
Restart apache and you should be set.