Installing Ruby on Rails on CentOS 4.5 provided by GoDaddy.com is definitely not for the weak of heart.
First of all, Apache HTTP Server. Sure there are alternatives available which are faster, easier, and maybe better, but none are as popular as Apache. The version I have installed is 2.2.8.
Next, Ruby on Rails, version 2.0. One of my new favorites. Not ideal for all your programming projects but a lot better than most web frameworks out there.
And third, MySQL. Installed on a separate machine. You should never install your database on your web server machine.
Last, some packages necessary for some cool RoR gems and plug-ins. This is up to you, but is not really necessary for standard RoR except maybe the mysql gem.
1. Mysql gem
2. RMagick, this one was a pain in the derrière.
3. ar_mailer
A. Starting with Apache HTTP Server 2.0.x
Uninstall any default installed versions: yum remove httpd
Download the latest version of Apache HTTP Server and compile with the following settings:
install apache 2 with : ./configure --enable-deflate --enable-proxy --enable-proxy-html --enable-proxy-balancer --enable-rewrite --enable-cache --enable-mem-cache --enable-ssl --enable-headers
Create a user apache and group apache. Add
User apacheto the httpd.conf file. The default installation directory is /usr/local/apache2. The httpd.conf file can be found under the conf subdirectory.
Group apache
Create a new conf.d subdirectory under /usr/local/apache2, or in the install directory on your machine if you changed the default install path.
Add the following in your httpd.conf file:
Include conf.d/*.confUnder this new directory create the following files.
I like to create first a .common file which contains all the common settings for your web applications.
e.g. mywebserver.common
RewriteEngine On
# Uncomment for rewrite debugging
#RewriteLog logs/myapp_rewrite_log
#RewriteLogLevel 9
# Check for maintenance file and redirect all requests
# ( this is for use with Capistrano's disable_web task )
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]
# Rewrite index to check for static
RewriteRule ^/$ /index.html [QSA]
# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]
# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
# Deflate
AddOutputFilterByType DEFLATE text/html text/plain text/css
# ... text/xml application/xml application/xhtml+xml text/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Uncomment for deflate debugging
#DeflateFilterNote Input input_info
#DeflateFilterNote Output output_info
#DeflateFilterNote Ratio ratio_info
#LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
#CustomLog logs/myapp_deflate_log deflate
# this not only blocks access to .svn directories, but makes it appear
# as though they aren't even there, not just that they are forbidden
<DirectoryMatch "^/.*/\.svn/">
ErrorDocument 403 /404.html
Order allow,deny
Deny from all
Satisfy All
<DirectoryMatch>
And next create a .conf file for your app. This file also contains the settings for the Mongrel cluster.
e.g. myapp.conf
As a bonus, this is my start and stop script for Apache HTTP server. If you did not use the default install directory, it will need some tweaking.><VirtualHost www.myapp.com:80>
ServerName www.myapp.com
DocumentRoot /var/www/myapp/current/public
<Directory "/var/www/myapp/current/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</DirectoryInclude /usr/local/apache2/conf.d/mywebserver.common
ErrorLog logs/myapp_errors_log
CustomLog logs/myapp_log combined
# Proxy balancer
<Proxy balancer://mongrel_cluster>
BalancerMember http://127.0.0.1:8000
BalancerMember http://127.0.0.1:8001
BalancerMember http://127.0.0.1:8002
</Proxy>
</VirtualHost>
#!/bin/bash
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# config: /usr/local/apache2/conf/httpd.conf
# config: /usr/local/apache2/httpd
# pidfile: /var/run/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /usr/local/apache2/ ]; then
. /usr/local/apache2/
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache2/bin/apachectl
httpd=${HTTPD-/usr/local/apache2/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
# check for 1.3 configuration
check13 () {
CONFFILE=/usr/local/apache2/conf/httpd.conf
GONE="(ServerType|BindAddress|Port|AddModule|ClearModuleList|"
GONE="${GONE}AgentLog|RefererLog|RefererIgnore|FancyIndexing|"
GONE="${GONE}AccessConfig|ResourceConfig)"
if LANG=C grep -Eiq "^[[:space:]]*($GONE)" $CONFFILE; then
echo
echo 1>&2 " Apache 1.3 configuration directives found"
echo 1>&2 " please read /usr/share/doc/httpd-2.0.52/migration.html"
failure "Apache 1.3 config directives test"
echo
exit 1
fi
}
# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure. So we just do it the way init scripts
# are expected to behave here.
start() {
echo -n $"Starting $prog: "
check13 || exit 1
LANG=$HTTPD_LANG daemon $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
# When stopping httpd a delay of >10 second is required before SIGKILLing the
# httpd parent; this gives enough time for the httpd parent to SIGKILL any
# errant children.
stop() {
echo -n $"Stopping $prog: "
killproc -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
killproc $httpd -HUP
RETVAL=$?
fi
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit $RETVAL
B. Installing Ruby on Rails
First of all I assume you know where to find all the packages and how to compile a program in linux.
Download and install readline from source, this package a prerequisite for RoR. Install default in
/usr/localDownload Ruby's source. Again remove any pre-installed version:
yum remove rubyConfigure and compile:
./configure --enable-pthread --with-readline-dir=/usr/localDownload and install ruby gems from source:
ruby setup.rb
And
sudo gem install rails
C. MySQL
Because your database server should not be installed on the same machine as your web server.
You should only install the client and development packages from MySQL on this machine. I used yum for this.
yum install mysql
yum install mysql-devel
Installed Packages
mysql.i386 5.0.48-1.el4.centos installed
Installed Packages
mysql-devel.i386 5.0.48-1.el4.centos installed
D. And last, all those nice gems.
1. Mysql gem
gem install mysql -- --with-mysql-config=/usr/bin/mysql_config2. RMagick
First, we need all the ImageMagick dependencies
yum install ImageMagickyum remove ImageMagickThere is probably a better way to get the dependencies.
Second, download and install ghostscript fonts:
ftp://ftp.imagemagick.org/pub/ImageMagick/delegates/ghostscript-fonts-std-8.11.tar.gz
Install:
sudo tar xvf ghostscript-fonts-std-8.11.tar.gz -c /usr/local/shareNext. download and compile ghostcript:
http://pages.cs.wisc.edu/~ghost/
And, download ImageMagick and compile with following settings:
./configure --disable-static --with-modules --without-perl --without-magick-plus-plus --with-quantum-depth=8 --with-gs-font-dir=/usr/local/share/fonts
Last, install the gem:
gem install rmagick3. ar_mailer:
You will need this if you want to send bulk emails through must SMTP servers.
gem install ar_mailer
create a cronjob, this one runs every 3 minutes:
*/3 * * * * /usr/local/bin/ar_sendmail -o -e production -c /var/www/myapp/current/ > /home/user/ar_mailsender.log 2>&1
E. Final note.
I assumed your Ruby on Rails app will be installed under /var/www/myapp.
This post did not describe anything about the mongrel_cluster, a good link for information on how to install the mongrel cluster, the article is about Mac OS X, but will also work for CentOS 4.5: http://blog.gwikzone.org/articles/2006/10/02/easy-setup-a-mongrel-cluster-with-apache-2-2-on-mac-os-x
Labels: CentOS 4.5, ImageMagick, Ruby on Rails