Skip to content

Instantly share code, notes, and snippets.

@imuchene
Last active August 29, 2015 13:57
Show Gist options
  • Save imuchene/9526932 to your computer and use it in GitHub Desktop.
Save imuchene/9526932 to your computer and use it in GitHub Desktop.
This is a recipe for deploying a Rails 4 app in Ubuntu (I used 12.04), with Nginx as the web server, Unicorn as the application server and Postgresql as the database server.
# Install vim-nox
sudo apt-get install vim-nox
# Install rbenv
sudo apt-get install git-core build-essential python-software-properties nodejs
curl https://raw.github.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | bash
# Resolve rbenv openssl error
sudo apt-get install openssl libssl-dev ruby1.9.1-dev
# Edit .bashrc and reload bash
vi .bashrc
source .bashrc
# Install ruby 2.0
rbenv install 2.0.0-p353
rbenv rehash
# Install postgresql 9.2
sudo aptitude search postgresql # Search for the lastest postgresql from the repository
sudo apt-get purge postgresql-9.1 # uninstall postgresql 9.1 if it's already installed
sudo apt-get -f install
sudo apt-add-repository ppa:pitti/postgresql
sudo apt-get update
sudo apt-get install postgresql-9.2
# Resolve the bundler error Can't install RMagick 2.13.2. Can't find Magick-config
sudo apt-get install libmagickwand-dev
# Resolve the bundler error You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
sudo apt-get install libpq-dev
# Install admin pack postgres extensions
sudo apt-get install postgresql-contrib
# Login to postgresql with the postgres role
sudo -u postgres psql postgres
# Run initial commands
\password postgres
CREATE EXTENSION adminpack;
create role jumpstartrails with createdb;
alter role jumpstartrails with password 'jumpstart';
alter role jumpstartrails with login;
# run db migrations
rake db:create
rake db:migrate
# Install nginx
sudo apt-add-repository ppa:nginx/stable
sudo apt-get update
sudo apt-get install nginx
# Start nginx
sudo service nginx status
sudo service nginx start
# To edit the main nginx server configurations
sudo vi /etc/nginx/nginx.conf
# To edit nginx default enabled site
sudo vi /etc/nginx/sites-available/default
# If you receive an ipv6 failed (98: Address already in use) error comment out the line
listen [::]:80 default_server ipv6only=on;
from /etc/nginx/sites-available/default
# Check if nginx is successfully installed
> ip addr
> copy returned ip to the web browser
NB: The default web root nginx reads from is /usr/share/nginx/html/index.html
# Install unicorn
uncomment the gem 'unicorn' line in your gem file
run bundle
> create a unicorn.rb file in the config folder and add the line:
working_directory File.expand_path("../..", __FILE__)
listen “/tmp/unicorn.sock”
worker_processes 3
timeout 30
pid "/tmp/unicorn_jumpstartrails.pid"
stdout_path "/home/vagrant/jumpstartrails/log/unicorn.log"
stderr_path "/home/vagrant/jumpstartrails/log/unicorn.log"
# run unicorn
rbenv rehash
bundle exec unicorn -c config/unicorn.rb
You should be able to view the app on the browser: http://<servername>:8080
# Update nginx to interact with unicorn
sudo vi /etc/nginx/sites-enabled/default
Edit the file to appear like so:
upstream unicorn
{
server unix:/tmp/unicorn.sock fail_timeout=0;
}
server
{
listen 80 default_server;
# Application's web root
root /vagrant/jumpstartrails/public/;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location /
{
try_files $uri/index.html $uri @unicorn;
}
location @unicorn
{
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
}
# Restart nginx
sudo service nginx restart
# Make sure the unicorn app server is running, adding the -D flag daemonises the process
bundle exec unicorn -c config/unicorn.rb -E production
# Migrate the database
RAILS_ENV=production bundle exec rake db:migrate
# If necessary pre-compile the assets
rake assets:precompile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment