HowTo configure Apache to run Redmine » History » Version 16

Lu Pa, 2010-06-01 23:02
Comment about Ubuntu version

1 1 Cyber Sprocket
h1. HowTo configure Apache to run Redmine
2 1 Cyber Sprocket
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 1 Cyber Sprocket
h2. For CentOS 5
8 1 Cyber Sprocket
9 1 Cyber Sprocket
h3. Assumptions
10 1 Cyber Sprocket
11 1 Cyber Sprocket
* OS is CentOS 5
12 1 Cyber Sprocket
* Web server is Apache 2
13 1 Cyber Sprocket
** mod_cgi is enabled
14 1 Cyber Sprocket
** 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 4 Cyber Sprocket
19 4 Cyber Sprocket
* You do not need to run mod_fcgid
20 4 Cyber Sprocket
* You do not need to run mod_fastcgi
21 8 William Baum
 
22 8 William Baum
bq. -This section needs work. I can't tell if these are the myths or the corrections to the myths.
23 8 William Baum
_(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 14 Cyber Sprocket
29 1 Cyber Sprocket
h3. Basic Steps
30 1 Cyber Sprocket
31 11 Patrick OMalley
* Install Redmine per the [[RedmineInstall| installation instructions]] and get it running with webrick.
32 1 Cyber Sprocket
33 1 Cyber Sprocket
* Kill the webrick session
34 1 Cyber Sprocket
35 1 Cyber Sprocket
* Copy the public/dispatch.cgi.example to public/dispatch.cgi
36 1 Cyber Sprocket
37 9 Patrick OMalley
* Edit public/dispatch.cgi to fix the shell script invocation to point to your ruby install location like:
38 1 Cyber Sprocket
  @#!/usr/local/bin/ruby@
39 9 Patrick OMalley
  or 
40 9 Patrick OMalley
  @#!/usr/bin/ruby@
41 1 Cyber Sprocket
42 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
43 10 Patrick OMalley
  @require "/usr/lib/ruby/gems/1.8/gems/rails-2.1.2/lib/dispatcher.rb"@
44 10 Patrick OMalley
45 1 Cyber Sprocket
* Make sure public/dispatch.cgi has execute permissions via:
46 1 Cyber Sprocket
  @# chmod 755 public/dispatch.cgi@
47 1 Cyber Sprocket
48 1 Cyber Sprocket
* Update the config/environment.rb file to force the rails environment to production, simply uncomment this line at the start of the file:
49 1 Cyber Sprocket
  @ENV['RAILS_ENV'] ||= 'production'@
50 1 Cyber Sprocket
51 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!_
52 3 Cyber Sprocket
53 1 Cyber Sprocket
<pre>
54 1 Cyber Sprocket
    <VirtualHost *:80>
55 1 Cyber Sprocket
        ServerName redmine.<YOUR-DOMAIN>.com
56 1 Cyber Sprocket
        ServerAdmin webmaster@<YOUR-DOMAIN>.com
57 1 Cyber Sprocket
        DocumentRoot /live/redmine/public/
58 1 Cyber Sprocket
        ErrorLog logs/redmine_error_log
59 1 Cyber Sprocket
60 1 Cyber Sprocket
        <Directory "/live/redmine/public/">
61 1 Cyber Sprocket
                Options Indexes ExecCGI FollowSymLinks
62 1 Cyber Sprocket
                Order allow,deny
63 1 Cyber Sprocket
                Allow from all
64 1 Cyber Sprocket
                AllowOverride all
65 1 Cyber Sprocket
        </Directory>
66 1 Cyber Sprocket
    </VirtualHost>
67 1 Cyber Sprocket
</pre>
68 1 Cyber Sprocket
69 1 Cyber Sprocket
* 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:
70 1 Cyber Sprocket
  @# chown -R apache:apache files log tmp vendor@
71 1 Cyber Sprocket
72 1 Cyber Sprocket
73 2 Cyber Sprocket
h3. Error Messages and Resolutions
74 1 Cyber Sprocket
75 2 Cyber Sprocket
  * @Rails requires RubyGems >= 0.9.4. Please install RubyGems@
76 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.
77 1 Cyber Sprocket
78 2 Cyber Sprocket
  * @Premature script headers@
79 2 Cyber Sprocket
    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.
80 2 Cyber Sprocket
81 8 William Baum
h3. Helpful Commands
82 2 Cyber Sprocket
83 2 Cyber Sprocket
 * @# which ruby@
84 2 Cyber Sprocket
   tells you which ruby binary is being run when the fully-qualified-filename has not been specified.
85 2 Cyber Sprocket
86 2 Cyber Sprocket
 * @# find / -name ruby@
87 2 Cyber Sprocket
   searches your entire system for any file named ruby, warning: can take a while on large filesystems.
88 2 Cyber Sprocket
 
89 2 Cyber Sprocket
 * @# ruby -v@
90 1 Cyber Sprocket
   tell you what version of ruby you are running by default
91 1 Cyber Sprocket
92 1 Cyber Sprocket
 * @#public/dispatch.cgi@
93 1 Cyber Sprocket
   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.
94 8 William Baum
95 8 William Baum
96 8 William Baum
h2. mod_fastcgi
97 8 William Baum
98 8 William Baum
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_fcid.
99 8 William Baum
100 8 William Baum
We'll start with "mod_fastcgi":http://www.fastcgi.com/.  
101 8 William Baum
102 8 William Baum
Install prerequisites:
103 8 William Baum
104 8 William Baum
<pre>
105 8 William Baum
yum install libtool httpd-devel apr-devel apr
106 8 William Baum
</pre>
107 8 William Baum
108 8 William Baum
h3. Download and Install mod_fastcgi
109 8 William Baum
110 8 William Baum
<pre>
111 8 William Baum
cd /usr/local/src/
112 8 William Baum
wget http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz
113 8 William Baum
114 8 William Baum
tar -zxvf mod_fastcgi-current.tar.gz
115 8 William Baum
cd mod_fastcgi-2.4.6/
116 8 William Baum
cp Makefile.AP2 Makefile 
117 8 William Baum
make top_dir=/usr/lib/httpd
118 8 William Baum
make install top_dir=/usr/lib/httpd
119 8 William Baum
</pre>
120 8 William Baum
121 8 William Baum
Create or edit @/etc/httpd/conf.d/mod_fastcgi.conf@
122 8 William Baum
<pre>
123 8 William Baum
LoadModule fastcgi_module modules/mod_fastcgi.so
124 8 William Baum
<IfModule mod_fastcgi.c>
125 8 William Baum
FastCgiIpcDir /tmp/fcgi_ipc/
126 8 William Baum
</IfModule>
127 8 William Baum
</pre>
128 8 William Baum
129 8 William Baum
The @/tmp/fcgi_ipc/@ directory needs to be writable to the apache user:
130 8 William Baum
<pre>
131 8 William Baum
chown -R apache.apache /tmp/fcgi_ipc/
132 8 William Baum
chmod -R 777 /tmp/fcgi_ipc/
133 8 William Baum
</pre>
134 8 William Baum
135 8 William Baum
> Note:  I had to do this more than once.. It created directories which it then didn't own.. ??
136 8 William Baum
137 8 William Baum
h3. Download and install @fcgi@ (for fcgi gem)
138 8 William Baum
139 8 William Baum
<pre>
140 8 William Baum
wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
141 8 William Baum
tar -zxvf fcgi-2.4.0.tar.gz
142 8 William Baum
cd fcgi-2.4.0
143 8 William Baum
./configure
144 8 William Baum
make
145 8 William Baum
make install
146 8 William Baum
</pre>
147 8 William Baum
148 8 William Baum
h3. Install fcgi gem:
149 8 William Baum
150 8 William Baum
<pre>
151 8 William Baum
gem install fcgi
152 8 William Baum
</pre>
153 8 William Baum
154 8 William Baum
h2. Configuring redmine to use fastcgi:
155 8 William Baum
156 8 William Baum
In your redmine/public/ directory, copy @dispatch.fcgi.example@ to @dispatch.fcgi@
157 8 William Baum
> 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.
158 8 William Baum
159 8 William Baum
@./public/.htaccess@
160 8 William Baum
<pre>
161 8 William Baum
#<IfModule mod_fastcgi.c>
162 8 William Baum
#       RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
163 8 William Baum
#</IfModule>
164 8 William Baum
#<IfModule mod_fcgid.c>
165 8 William Baum
#       RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
166 8 William Baum
#</IfModule>
167 8 William Baum
#<IfModule mod_cgi.c>
168 8 William Baum
#       RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
169 8 William Baum
#</IfModule>
170 8 William Baum
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
171 8 William Baum
</pre>
172 8 William Baum
173 8 William Baum
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>
174 8 William Baum
175 8 William Baum
Give it a whirl:
176 8 William Baum
<pre>
177 8 William Baum
/etc/init.d/httpd configtest
178 8 William Baum
/etc/init.d/httpd restart
179 8 William Baum
</pre>
180 8 William Baum
181 8 William Baum
h2. Additional Apache Configuration
182 8 William Baum
183 8 William Baum
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. 
184 8 William Baum
185 8 William Baum
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.
186 8 William Baum
187 8 William Baum
If you're having issues with apache virtual hosts, this can be very helpful:
188 8 William Baum
<pre>
189 8 William Baum
/usr/sbin/httpd -t -D DUMP_VHOSTS
190 8 William Baum
</pre>
191 8 William Baum
192 8 William Baum
h2. mod_fcgid
193 1 Cyber Sprocket
194 14 Cyber Sprocket
h3. fcgid from Apache
195 14 Cyber Sprocket
196 14 Cyber Sprocket
"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.
197 14 Cyber Sprocket
198 14 Cyber Sprocket
<pre>
199 14 Cyber Sprocket
cd /usr/local/src/
200 14 Cyber Sprocket
wget http://apache.mirrors.hoobly.com/httpd/mod_fcgid/mod_fcgid-2.3.5.tar.gz
201 14 Cyber Sprocket
tar zxvf mod_fcgid.2.3.5.tgz
202 14 Cyber Sprocket
cd mod_fcgid.2.3.5
203 14 Cyber Sprocket
</pre>
204 14 Cyber Sprocket
205 14 Cyber Sprocket
Configure and Install
206 14 Cyber Sprocket
<pre>
207 14 Cyber Sprocket
./configure.apxs
208 14 Cyber Sprocket
make
209 14 Cyber Sprocket
make install
210 14 Cyber Sprocket
service httpd restart
211 14 Cyber Sprocket
</pre>
212 14 Cyber Sprocket
213 14 Cyber Sprocket
214 14 Cyber Sprocket
215 14 Cyber Sprocket
h3. fcgid from China Coremail service
216 14 Cyber Sprocket
217 8 William Baum
"mod_fcgid":http://fastcgi.coremail.cn/ seems newer and preferable to mod_fastcgi.
218 1 Cyber Sprocket
219 14 Cyber Sprocket
220 1 Cyber Sprocket
<pre>
221 8 William Baum
cd /usr/local/src/
222 14 Cyber Sprocket
wget http://apache.mirrors.hoobly.com/httpd/mod_fcgid/mod_fcgid-2.2.tar.gz
223 13 William Baum
tar zxvf mod_fcgid.2.2.tgz
224 13 William Baum
cd mod_fcgid.2.2
225 8 William Baum
</pre>
226 8 William Baum
227 8 William Baum
Edit Makefile
228 8 William Baum
<pre>
229 8 William Baum
#top_dir      = /usr/local/apache2
230 1 Cyber Sprocket
top_dir      = /usr/lib/httpd
231 13 William Baum
</pre>
232 13 William Baum
233 13 William Baum
Build it..
234 13 William Baum
<pre>
235 13 William Baum
make
236 13 William Baum
make install
237 8 William Baum
</pre>
238 8 William Baum
239 8 William Baum
Edit/create @/etc/httpd/conf.d/mod_fcgid.conf@
240 8 William Baum
<pre>
241 8 William Baum
LoadModule fcgid_module /usr/lib/httpd/modules/mod_fcgid.so
242 8 William Baum
243 8 William Baum
<IfModule mod_fcgid.c>
244 8 William Baum
    SocketPath /tmp/fcgid_sock/
245 8 William Baum
    AddHandler fcgid-script .fcgi
246 8 William Baum
</IfModule>
247 8 William Baum
</pre>
248 8 William Baum
249 8 William Baum
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/@
250 8 William Baum
<pre>
251 8 William Baum
cd /etc/httpd/conf.d/
252 8 William Baum
mv mod_fastcgi.conf mod_fastcgi.conf.not
253 1 Cyber Sprocket
/etc/init.d/httpd restart
254 1 Cyber Sprocket
</pre>
255 14 Cyber Sprocket
256 14 Cyber Sprocket
257 14 Cyber Sprocket
h3. Either Version, Permissions
258 14 Cyber Sprocket
259 14 Cyber Sprocket
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.
260 14 Cyber Sprocket
261 14 Cyber Sprocket
<pre>
262 14 Cyber Sprocket
chmod 755 /var/log/httpd
263 14 Cyber Sprocket
service httpd restart
264 14 Cyber Sprocket
</pre>
265 14 Cyber Sprocket
266 8 William Baum
267 8 William Baum
h2. Installation Sources
268 8 William Baum
269 8 William Baum
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.
270 12 Fred Eisele
271 16 Lu Pa
h2. Ubuntu Server (Version ?) (This not don't work for 8.04 LTS)
272 12 Fred Eisele
273 12 Fred Eisele
Install passenger
274 12 Fred Eisele
<pre>
275 12 Fred Eisele
sudo aptitude install libapache2-mod-passenger
276 12 Fred Eisele
</pre>
277 12 Fred Eisele
This did switch the type of apache server to worker from prefork but in my case that was acceptable.
278 12 Fred Eisele
279 12 Fred Eisele
I had installed the redmine tarball into /opt/redmine as redmine-0.8.5 and made a symlink named current.
280 12 Fred Eisele
Then make a link to the redmine public directory from the apache DocumentRoot (see /etc/apache2/config.d/*).
281 12 Fred Eisele
<pre>
282 12 Fred Eisele
ln -s /opt/redmine/current/public /var/www/redmine
283 12 Fred Eisele
</pre>
284 12 Fred Eisele
285 12 Fred Eisele
Updated /etc/apache/config.d/redmine (or wherever your virtualhost is defined) with the following:
286 12 Fred Eisele
<pre>
287 12 Fred Eisele
RailsEnv production
288 12 Fred Eisele
RailsBaseURI /redmine
289 12 Fred Eisele
</pre>
290 12 Fred Eisele
291 12 Fred Eisele
Restart apache.
292 15 Martin Cyr
293 15 Martin Cyr
h2. For FreeBSD (and possibly others)
294 15 Martin Cyr
295 15 Martin Cyr
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.
296 15 Martin Cyr
297 15 Martin Cyr
Make sure 'Apache 2.2', 'Ruby', 'fastcgi' (including the gem) are installed.
298 15 Martin Cyr
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.
299 15 Martin Cyr
So here it is, my httpd.conf :
300 15 Martin Cyr
<pre>
301 15 Martin Cyr
LoadModule fastcgi_module     libexec/apache22/mod_fastcgi.so
302 15 Martin Cyr
303 15 Martin Cyr
DocumentRoot "/usr/local/www/redmine/public"
304 15 Martin Cyr
305 15 Martin Cyr
FastCgiServer /usr/local/www/redmine/public/dispatch.fcgi -idle-timeout 120 -initial-env RAILS_ENV=production -initial-env PATH=/usr/local/bin -processes 2
306 15 Martin Cyr
307 15 Martin Cyr
<Directory /usr/local/www/redmine/public>
308 15 Martin Cyr
   AddHandler fastcgi-script fcgi
309 15 Martin Cyr
   Order allow,deny
310 15 Martin Cyr
   Allow from all
311 15 Martin Cyr
   AllowOverride all
312 15 Martin Cyr
   Options +FollowSymLinks +ExecCGI
313 15 Martin Cyr
   RewriteEngine On
314 15 Martin Cyr
   RewriteCond %{REQUEST_FILENAME} !-f
315 15 Martin Cyr
   RewriteRule ^(.*)$ dispatch.fcgi
316 15 Martin Cyr
</Directory>
317 15 Martin Cyr
</pre>
318 15 Martin Cyr
319 15 Martin Cyr
Now let's explain what happens and why:
320 15 Martin Cyr
# _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).
321 15 Martin Cyr
# _Options +FollowSymLinks +ExecCGI_: tells apache that within this directory, CGI-ish script execution is allowed
322 15 Martin Cyr
# _RewriteCond %{REQUEST_FILENAME} !-f_: only rewrite if the file doesn't exist
323 15 Martin Cyr
# _RewriteRule ^(.*)$ dispatch.fcgi_: everything goes to dispatch.fcgi
324 15 Martin Cyr
# _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'.
325 15 Martin Cyr
326 15 Martin Cyr
Restart apache and you should be set.