After setting up a Ubuntu server and make it ready for Rails application deployment, i have followed these steps to get my Rails application deployed and running.
Install Ruby
Install Ruby 1.8 package with sqlite3:
sudo aptitude install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl-ruby sqlite3 libsqlite3-ruby1.8 -- or -- sudo aptitude install ruby-full
Add the following symlinks:
sudo ln -s /usr/bin/ruby1.8 /usr/bin/ruby sudo ln -s /usr/bin/ri1.8 /usr/bin/ri sudo ln -s /usr/bin/rdoc1.8 /usr/bin/rdoc sudo ln -s /usr/bin/irb1.8 /usr/bin/irb
Install RubyGems
Install RubyGems from source:
mkdir ~/sources cd ~/sources wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz tar xzvf rubygems-1.3.7.tgz cd rubygems-1.3.7 sudo ruby setup.rb ... sudo ln -s /usr/bin/gem1.8 /usr/bin/gem sudo gem env <-- All about the installed RubyGem
Install Rails
Install Rails gem:
sudo gem install rails sudo gem list <-- List all local gems rails -v <-- List Rails version
Create and Commit to a Repository
su git cd ~git/repository mkdir project.git cd project.git git --bare init exit
It is VERY IMPORTANT to initialize the repository before you push to this git repository location, otherwise, there will be permission issue on workstation like this:
fatal: '~git/repository/project.git': unable to chdir or not a git archive fatal: The remote end hung up unexpectedly
In workstation, initialize the local rails project (I assume you have one already) as a git repository:
cd project git init git add. git commit -m "First commit ever" git remote add origin ssh://git@123.456.789.123:12345/~git/repository/project.git git push origin master
You can try to ‘clone’ the remote repository back now:
rm -rf project git clone ssh://git@123.456.789.123:12345/~git/repository/project.git
Setup and Configure Capistrano
Install Capistrano
sudo gem install capistrano cap -V Capistrano v2.5.19
Capify the local project repo:
cd project capify . [add] writing `./Capfile' [add] writing `./config/deploy.rb' [done] capified!
Commit the change:
git add . git commit -m "The project is capified" git push
Configure Capistrano
Configure ‘project/config/deploy.rb’ with correct variables. My settings look like this:
set :application, "domain.com"
set :user, "lorenz"
set :repository, "ssh://git@202.181.234.41/~git/repository/ivotehk.git"
set :scm, :git
set :port, 30822
set :deploy_to, "/var/www/#{application}"
#set :scm, :subversion
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
role :web, application # Your HTTP server, Apache/etc
role :app, application # This may be the same as your `Web` server
role :db, application, :primary => true # This is where Rails migrations will run
#role :db, "your slave db-server here"
Create the folder before you deploy:
ssh -p 12345 lorenz@123.456.789.123 cd /var/www/ sudo mkdir domain.com sudo chown lorenz:www-data domain.com sudo chmod 775 domain.com
On workstation:
cap deploy:setup
===
Install Nginx and Mongrel
Install Nginx via aptitude:
sudo aptitude install nginx
Remove Apache2 from and add Nginx to auto-start:
sudo update-rc.d /etc/init.d/apache2 remove sudo /etc/init.d/nginx start sudo update-rc.d /etc/init.d/nginx defaults
Install Mongrel:
sudo gem install mongrel
Install MySQL for Ruby on Rails
Install MySQL:
sudo aptitude install mysql-server mysql-client libmysqlclient15-dev sudo aptitude install libmysql-ruby1.8
nano /etc/iptables.up.rules # Allow MySQL connections originating from local server -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d 202.181.234.41 --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT -A OUTPUT -p tcp -s 202.181.234.41 --sport 3306 -d 0/0 --dport 1024:65535 -m state --state ESTABLISHED -j ACCEPT
Create database in MySQL
mysql -u root mysql mysql> CREATE DATABASE rails_app_db;
Load Rails db into MySQL database:
rake db:schema:dump RAILS_ENV=production --trace rake db:migrate RAILS_ENV=production --trace
Deploy Directory and Permiison
Create the folder and grant permissions:
mkdir /var/www/domain.com sudo usermod -a -G www-data lorenz sudo chgrp -R www-data /var/www/domain.com sudo chown -R lorenz /var/www/domain.com
Configure Virtual Host
Create a vhost configuration file:
sudo nano /etc/nginx/sites-available/domain.com
The vhost file for ‘domain.com’ will look like:
dadada
The vhost file for ‘domain.com’ will look like:
sudo ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/domain.com
Start 2 mongrel instances to test the installation:
cd project mongrel_rails start -d -e production -p 8000 -P log/mongrel8000.pid mongrel_rails start -d -e production -p 8001 -P log/mongrel8001.pid mongrel_rails stop -P log/mongrel8001.pid
Install and configure mongrel_cluster
sudo gem install mongrel_cluster cd /var/www/domain.com/current mongrel_rails cluster::configure -e production -p 8000 -N 2 -c /var/www/domain.com/current -a 127.0.0.1
Configure ‘/var/www/domain.com/current/config/mongrel_cluster.yml’:
address: 127.0.0.1 log_file: log/mongrel.log port: "8000" cwd: /var/www/domain.com/current environment: production pid_file: tmp/pids/mongrel.pid servers: 2
mongrel_rails cluster::stop sudo mkdir /etc/mongrel_cluster sudo ln -s /var/www/domain.com/current/config/mongrel_cluster.yml /etc/mongrel_cluster/domain.yml sudo cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster /etc/init.d/ sudo chmod +x /etc/init.d/mongrel_cluster sudo /usr/sbin/update-rc.d -f mongrel_cluster defaults sudo useradd mongrel sudo usermod -a -G www-data mongrel
Configure Capistrano details
Add the following lines to ‘config/deploy.rb’:
set :deploy_via, :copy set :runner, user
Create ‘script/spin’ file:
/home/demo/public_html/domain.com/current/script/process/spawner -p 8000 -i 2 -e production
Make it executable after push:
nano .git/hooks/post-receive Adding this line: chmod +x script/spin
Commit the change and deploy the application
cap deploy:cold
Great! Our Rails application is successfully deployed on Ubuntu server, and will be updated by a single command in future.
Related Posts
- Setting Up Ubuntu Server for Ruby on Rails Deployment
- Setting Up PHP FastCGI and phpMyAdmin with nginx
- Setting Up your own DNS server with bind9

{ 2 trackbacks }
{ 0 comments… add one now }