This article will guide you through the process of installation of the Amazon EC2 instance and elastic block including: Ubuntu, Mysql, Ruby on Rails, Fussion Passenger, Git, Gems
First you need to pick on of the AMI to for your instance. I have chosen ami-5059be39. Using Amazon Management Console assign the instance elastic IP Address and create elastic block.
Then instance is place for you server software such are Ubuntu and elastic block acts as your hard-rive.
You need to be able to SSH into your instance to be able to install additional software, your app and so on. You can do this by executing the following command from your machine.
ssh -i your-ssh-key.pem ubuntu@your-elastic-ip-address
Mount your elastic block into your instance running these commands while ssh connected to your instance.
yes | mkfs -t ext3 /dev/sdf
mkdir /mnt/data
mount /dev/sdf /mnt/data
Now we will need to install all the additional software which will make our life easier. This process will install apache, ruby, mysql server, and necessary libraries, subversion and git, ruby gems, Passenger Phusion.
The best way to do this is to create file called setup.sh in the folder /home/ubuntu and insert inside all this code. (you can read more details about this process on the Design by Engineers site)
# Setup Ruby on Rails, MySql, Apache + Passenger
# on Amazon Ubuntu instance (ami-5059be39 – ubuntu-intrepid)
# 10/09/09
echo Updating system...
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install build-essential
sudo apt-get install curl libcurl4-openssl-dev
sudo apt-get install zlib1g-dev libssl-dev libexpat1-dev
echo Installing tools...
sudo apt-get install apache2 apache2-threaded-dev
sudo apt-get install ruby ri rdoc ruby1.8-dev irb libreadline-ruby1.8
sudo apt-get install libruby1.8 libopenssl-ruby libopenssl-ruby1.8
sudo apt-get install mysql-server libmysqlclient15off mysql-client-5.0
sudo apt-get install mysql-common mysql-server-5.0 libmysqlclient-dev
sudo apt-get install libmysql-ruby libmysql-ruby1.8 psmisc
sudo apt-get install subversion
sudo apt-get install git-core gitweb
echo Installing ruby gems...
cd /usr/local/src
sudo wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
sudo tar -zvxf rubygems-1.3.5.tgz
cd rubygems-1.3.5/
sudo ruby setup.rb
echo Symlinking...
sudo ln -s /usr/bin/gem1.8 /usr/local/bin/gem
sudo ln -s /usr/bin/ruby1.8 /usr/local/bin/ruby
sudo ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdoc
sudo ln -s /usr/bin/ri1.8 /usr/local/bin/ri
sudo ln -s /usr/bin/irb1.8 /usr/local/bin/irb
echo Installing Gems...
sudo gem install sys-proctable --no-rdoc --no-ri
sudo gem install rails --no-rdoc --no-ri
sudo gem install mysql --no-rdoc --no-ri
sudo gem install capistrano rspec rdoc --no-rdoc --no-ri
sudo gem install passenger --no-rdoc --no-ri
sudo gem install haml --no-rdoc --no-ri
sudo gem install hpricot --no-rdoc --no-ri
sudo gem install json --no-rdoc --no-ri
sudo gem install newrelic_rpm --no-rdoc --no-ri
sudo gem install sqlite3-ruby --no-rdoc --no-ri
sudo gem install will_paginate --no-rdoc --no-ri
sudo gem install authlogic --no-rdoc --no-ri
sudo gem install cached_model --no-rdoc --no-ri
echo Installing Phusion Passenger
sudo passenger-install-apache2-module
Now run this command to execute this file…
sudo bash ./setup.sh
You will be asked to create mysql password during the installation process. Write it down, you will need it for the configuration of your application. You will also need to confirm couple of steps during the installation.
We now need to configure apache so it runs Passenger for our rails application. You need to add this few lines at the end of your apache configuration file which you will find in /etc/apache/apache2.conf
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.11/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.11
PassengerRuby /usr/bin/ruby1.8
You will also need to add these few lines to tell Passenger where is your application sitting.
Replace “your-elastic-ip-address” with your elastic IP address
Replace “your-path” with the real path where your app will sit. You will need to create the folder structure before you set this path otherwise the Apache will complain that the path does not exist.
<VirtualHost *:80>
ServerName your-elastic-ip-address
DocumentRoot /mnt/data/your-path
<Directory /mnt/data/your-path>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
The same virtual host configuration need to be added to the file /etc/apache2/sites-available/default
Restart your server…
sudo /etc/init.d/apache2 restart
At this point I have deployed my application with Capistrano. There will be a separate post how to set Capistrano to deploy on EC2 server.
In case you want to double check free space on your hard-drive you can execute this command
df -h
The most difficult part for me was to setup mysql.
You might need to check the system errors during this process so you know what is going wrong. You can see the errors by executing
sudo tail -50 /var/log/syslog
Create new folder for your msq files called mysql_data inside /mnt/data/ folder (your elastic block) then assign the right privileges to that folder for mysql user and group. Stop the server, move the original mysql files, delete the original folder and create symlink to your new directory. Restart mysql
mkdir /mnt/data/mysql_data
chown mysql:mysql /mnt/data/mysql_data
/etc/init.d/mysql stop
mv /var/lib/mysql/* /mnt/data/mysql_data
rmdir /var/lib/mysql
ln -s /mnt/data/mysql_data mysql
/etc/init.d/mysql start
You will need to adjust configurations for mysql apparmor file by executing
vi /etc/apparmor.d/usr.sbin.mysqld
adding these few lines…
As you can see these are my paths /mtn/data/mysql_data you can put your files in different folder structure inside mnt folder, change the paths accordingly. You will find more details about this process on Article my-addr
/mnt/data/mysql_data/ r,
/mnt/data/mysql_data/** rwk,
/mnt/data/mysql_data/ r,
/mnt/data/mysql_data/** rw,
Restart apparmor
/etc/init.d/apparmor restart
This was all what was necessary to setup mysql for most of the users but not in my case, I got errors and mysql was not running. I found that these few lines has fixed my problem.
mysql_install_db --user=mysql --ldata=/new-data-location
mysqld_safe --datadir=/new-data-location --user=mysql
I hope it could help someone because I have spent long time configuring the EC2 server. There are still some tasks you will need to do after your server is up and running. I will talk about these in later posts. I mean things like backing up your instance, backing up your elastic block and setting up cron job to backup your database regularly.