Rails 3 Stack: Ruby 1.9.2 + Nginx + Passenger + MySQL on CentOS AWS micro
Note: This was tested on AWS using a micro instance and ami-6b608c02 which seemed to be the most barebones community CentOS i386 EBS AMI I could find. It works as a Micro instance out of the box without issues.
Prepare the Server
Update the system and install required libraries
- yum update -y
- yum groupinstall "Development Tools" -y
- yum install zlib-devel wget openssl-devel pcre pcre-devel readline-devel sudo nano -y
Configure Time Settings
- yum install tzdata system-config-date -y
- yum install ntp -y
Contact NTP server and update time:
Configure the hostname
Write loopback IP and your hostname in your /etc/hosts file:
Write your hostname to /etc/hostname
Installing the Application Stack
Install MySQL
- yum install mysql mysql-devel
Secure your MySQL install:
- mysql_secure_installation
Install Ruby
Ignore ri and rdoc for this server
Add these two lines:
- install: --no-ri --no-rdoc
- update: --no-ri --no-rdoc
Download Ruby from source and install:
Check that Ruby and RubyGems all were installed correctly:
Clean up the Ruby source files:
Install Git (Good idea for Capistrano deployments)
Install Rails and Passenger gems
If you need a specific version of Rails:
- gem install rails -v=2.3.8
- gem install passenger
Install modified Nginx with Passenger built-in
- passenger-install-nginx-module
Option 1
Create init scripts for Nginx
Verify the script can start and stop Nginx:
- sudo /etc/init.d/nginx start
* Starting Nginx Server...
...done.
- sudo /etc/init.d/nginx status
nginx found running with processes: 11511 11510
- sudo /etc/init.d/nginx stop
* Stopping Nginx Server...
...done.
Add Nginx to startup:
- sudo /sbin/chkconfig nginx on
Check that it starts and stops with the server by:
Install ImageMagick and RMagick (Optional)
- yum remove imagemagick
- yum install tcl-devel libpng-devel libjpeg-devel ghostscript-devel bzip2-devel freetype-devel libtiff-devel
- yum install libjpeg-devel libpng-devel glib2-devel fontconfig-devel zlib-devel libwmf-devel freetype-devel
Download the ImageMagick Source
- wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
- tar xvfz ImageMagick.tar.gz
- cd ImageMagick-6*
- ./configure --prefix=/usr --with-bzlib=yes --with-fontconfig=yes --with-freetype=yes --with-gslib=yes --with-gvc=yes --with-jpeg=yes --with-jp2=yes --with-png=yes --with-tiff=yes
- make && make install
Install RMagick
Create a ‘deployer’ user (Recommended)
Add the user
- useradd deployer
- passwd deployer
Give deployer sudo permission
Add this near the bottom of the file:
Test the account
End your SSH session
Lock the root account
Test a Rails 3 app
Create the testapp
- rails new testapp -d mysql
- cd testapp
- bundle install
Enter your database password in your database.yml real quick
- nano config/database.yml
- rake db:create:all
- rails generate scaffold post title:string body:text
- rake db:migrate
Check really quick that WeBrick can start the rails app before we involve Passenger/Nginx
Change the user Nginx runs as
- sudo nano /opt/nginx/conf/nginx.conf
At the top of the config file, uncomment or change “user nobody;” to:
Add a virtual host to Nginx
- sudo nano /opt/nginx/conf/nginx.conf
Find the server { } block in the default file and remove it. Add this:
server {
listen 80;
root /home/deployer/testapp/public;
passenger_enabled on;
}
Restart Nginx:
- sudo /etc/init.d/nginx restart
Try accessing your testapp
In your web browser:
Tweaking Nginx/Passenger
Enable gzip
- sudo nano /opt/nginx/conf/nginx.conf
Find the existing commented “ #gzip on; ” and remove it and replace it with the following:
gzip on;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6]\.";
gzip_proxied any;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
Longer expiration tags on content served by Nginx
Place the following in your virtual host’s server { } block:
location ~* \.(ico|css|js|gif|jp?g|png)(\?[0-9]+)?$ {
expires max;
break;
}
Reduce Passenger spin-up time
Place the following in your virtual host’s server { } block:
#Passenger options
rails_spawn_method smart-lv2; # This can cause problems with some apps
rails_app_spawner_idle_time 0;
rails_framework_spawner_idle_time 0;