Monday, February 25, 2008

Rails stack on Centos 5 with Nginx and memcached

This is yet another rails stack installation how to. It is useful for installing a production rails hosting environment using Nginx as the reverse proxy server for a mongrel cluster, using memcached as the caching server and mysql as the database server. Of course I am assuming that all these services will be installed in the same server for the sake of simplicity.

Mysql

Make sure that Mysql is installed correctly and ensure it is running on startup.
sudo yum install mysql-server
sudo yum install mysql
sudo yum install mysql-devel
sudo checkconfig --level 345 mysqld on
Note: In case you run mysql on a dedicated server then you need not to install Mysql in the application servers, but make sure you install at least mysql-devel because it contains header files needed for Mysql gem installation.

Memcached
Memcache server needs livevent package as a dependency, so according to the deployment server system distribution we need to install the correct packages, in case of Centos 5 we are going to select the proper packages from Dag repositories from the following pages;
http://dag.wieers.com/rpm/packages/libevent/
http://dag.wieers.com/rpm/packages/memcached/
The following are the required steps to download and install memcached and libevent for centos 5.
wget http://dag.wieers.com/rpm/packages/memcached/memcached-1.1.13-4.el5.rf.i386.rpm
wget http://dag.wieers.com/rpm/packages/libevent/libevent-1.3b-1.el5.test.i386.rpm
sudo rpm -ivh libevent-1.3b-1.el5.test.i386.rpm
sudo rpm -ivh memcached-1.1.13-4.el5.rf.i386.rpm
sudo /sbin/chkconfig --level 345 memcached on
memcached configuration could be found on /etc/sysconfig/memcached set the CACHESIZE to desired memory cache size you want.

Nginx
Get the latest nginx package tar, unpack it and configure it as follows. We need to install nginx required library first before configuring Nginx though. After compiling we need to copy nginx init script to /etc/init.d/nginx and give it execute permission.

sudo yum install pcre
sudo yum install pcre-devel
sudo yum install zlib
sudo yum install zlib-devel
sudo yum install openssl
sudo yum install openssl-devel
sudo ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-cc-opt="-I /usr/include/pcre" --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --conf-path=/etc/nginx/nginx.conf
make
make install
sudo cp nginx /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
Note: you can use this conf file as a base for nginx configuration

Rubygems
If rubygems is installed but needs update run the following
sudo gem update --system
If not installed get the latest version from here, unpack and install.
ruby setup.rb
Rails, Ferret, Rake
sudo gem install rails -y
sudo gem install rake
sudo gem install ferret

Mongrel
sudo gem install mongrel
Mongrel Cluster
Install Mongrel Cluster gem, and copy the service startup script and make sure it starts on system startup.
sudo gem install mongrel_cluster
sudo cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster /etc/init.d/
sudo cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/default.yml /srv/webapp/config/mongrel_cluster.yml
sudo /sbin/chkconfig --level 345 mongrel_cluster on
You may need or should write your own mongrel_cluster configuration file, to define number of mongrel instances, user, group, port and other options. Here is an example:
---
cwd: /srv/webapp/
port: "8000"
environment: production
address: 127.0.0.1
pid_file: log/mongrel.pid
servers: 8
group: nginx
user: nginx
You may need to add nginx user and group to your server
/usr/sbin/useradd nginx

Mysql Ruby Driver

We have to do a little more than gem install here, cause it seems that Redhat based distros are putting Mysql in a place not expected by the gem installer. So we are going to either explicitly give ‘gem install’ the location of lib headers and include for mysql, or soft linking the /usr/include/mysql and /usr/lib/mysql folders before ‘gem install’. (The soft link solution works well for me in most servers)
sudo gem install mysql -- --with-mysql-include=/usr/include/mysql --with-mysql-lib=/usr/lib/mysql

update: New version of rubygems require double dash before the compiler configuration options.
OR
sudo ln -s /usr/lib/mysql/ /usr/local/lib
sudo ln -s /usr/include/mysql/ /usr/local/include/
sudo gem install mysql
Memcached Ruby Driver
sudo gem install memcache-client -y

Related links:

3 comments:

Anonymous said...

Good to know someone is using Rails on CentOs 5, as I am going to try out CentOs for hosting our rails app - with nginx, mongrel, & mysql.

Thanks for the post.

Vasyl Kondrashov said...

chkconfig is much better than checkconfig ;)

fahim said...

Hi ,

Thanks for your blog.
Can we validate request input data using memcache ? Actually I am facing scenario in which user uploading CSV file which can have hundreds of rows and to validate each row using ActiveRecord taking long time.

Please can you reply me below question:-

1. So can we cache our validation rule in memcache ?
2. If it is possible to store validation rules in memcache but can I able to perform
validation check ?

Finite-State Machines, Part 1: Modeling with Haskell Data Types

Stateful programs often become complex beasts as they grow. Program state incohesively spread across a bunch of variables, spuriously guar...