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:

Using Google to send mails from Rails

I was using 'sendmail' for sending mails from my web applications. The apps was sending mails for welcome messages, comment notifications, activation mails, send to a friend and all other features depending on sending emails to the application user. But recently, I was deploying a webapp on a server in which sendmail is not configured correctly. I didn't have time and wouldn't like to invest any time on system administration then, so I searched for another solution for my webapps to send mails through Google. I found ssmtp. It is a simple tool doing a simple task, sending mails. However it does not receive mail, expand aliases or manage a queue. It is just forwarding mails to the mailhost. SSMTP requires replacing 'sendmail'.

There is nothing required from Rails app to use ssmtp, actually it was transparent. Just make sure that the ActionMailer is configured to use sendmail.
ActionMailer::Base.delivery_method = :sendmail
SSMTP Installation
In Ubuntu I use apt-get to install the package. I am enabling all the sources on my ubuntu, so you may need to do so. In RPM based distribution like Centos in my case i used this link to download the required RPM.

on Centos
sudo wget ftp://mirror.switch.ch/mirror/epel/5/i386/ssmtp-2.61-11.4.el5.i386.rpm
sudo rpm -ivh ssmtp-2.61-11.4.el5.i386.rpm
on Ubuntu.
sudo apt-get install ssmtp

SSMTP Configuration

SSMTP configuration files lie in /etc/ssmtp so you need to edit the conf file and fill it with the your gmail account.
mailhub=smtp.gmail.com:587
AuthUser=my_gmail_account@gmail.com
AuthPass=my_gmail_password
UseSTARTTLS=YES
There is one feature you will get by using Google as your SMTP server, it's its sent mail folder which archive every mail sent by your application, this feature being a bless or damn is up to your needs.


Related links: