Chennai-Rails Workshop
21 March 2015
Abhishek Yadav
@h6165
Quick Rundown
Quick Rundown
sudo apt-get install build-essential \
zlib1g-dev \
libssl-dev openssl \
libreadline-dev \
sqlite3 libsqlite3-dev \
libxslt-dev libxml2-dev \
libffi-dev \
curl wget git-core
Install build-tools, libs
git clone git://github.com/sstephenson/ruby-build.git; cd ruby-build; sudo ./install.sh
sudo ruby-build -kv 2.2.0 /usr/local/
sudo gem install bundler
Install Ruby and bundler
mkdir -p /var/www/my_app; cd /var/www/my_app
git clone git@bitbucket.org:my_app
Or setup Capistrano
Copy code to the server
export RAILS_ENV=production
Edit database.yml to setup production config
bin/rake db:create
bin/rake db:schema:load
bin/rake db:seed
bin/rake assets:precompile
bundle install, db:migrate, asset precompile
Setup Unicorn/Puma + Nginx
Webrick only: bin/rails s -p 80
Nginx: sudo apt-get install nginx
Unicorn: Add to Gemfile: gem 'unicorn'
Setup Unicorn + Nginx
Nginx site config: /etc/nginx/sites-available/my-app
upstream my_app {
server 127.0.0.1:8080;
}
server{
listen 80;
server_name example.org;
root /var/www/my_app/current/public;
location / {
proxy_set_header X_FORWARDED_PROTO $scheme;
proxy_set_header Host $http_host;
if (!-f $request_filename) {
proxy_pass http://my_app;
break;
}
}
}
Setup Unicorn + Nginx
Unicorn config: Rails.root/config/unicorn.rb
worker_processes 4
listen "/tmp/.sock", :backlog => 64
listen 8080, :tcp_nopush => true
pid "/var/www/my_app/tmp/pids/unicorn.pid"
stderr_path "/var/www/my_app/log/unicorn.stderr.log"
stdout_path "/var/www/my_app/log/unicorn.stdout.log"
before_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
Setup Unicorn + Nginx
Unicorn start:
bundle exec unicorn -c config/unicorn.rb -E production -D
Nginx start:
sudo service nginx start
Setup Database: mysql
Install
sudo apt-get install mysql-server libclient-dev mysql-client
Edit database.yml
bin/rake db:create
bin/rake db:schema:load
DNS setups
DNS setups: setting up DNS Records
DNS setups: transferring Name Server