Create and Deploy a Ruby on Rails 4 App with PostgreSQL, Unicorn and Nginx on Ubuntu 14.04
-
Install the rbenv and Ruby dependencies
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev
-
To install rbenv
git clone git://github.com/sstephenson/rbenv.git .rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(rbenv init -)"' >> ~/.bash_profile git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile source ~/.bash_profile
On Ubuntu Desktop, replace all occurrences .bash_profile in the above code block with .bashrc.
-
Install Ruby
rbenv install -v 2.2.3
-
Install the bundler gem
gem install bundler
-
Install Rails
gem install rails
-
Install Nodejs
sudo apt-get update sudo apt-get install nodejs
-
Install PostgreSQL
sudo apt-get update sudo apt-get install postgresql postgresql-contrib libpq-dev
-
Create a PostgreSQL Super User
sudo apt-get update sudo apt-get install postgresql postgresql-contrib libpq-dev
Set a password
Enter a password and confirm. Press \q to quit.sudo -u postgres psql postgres=# \password pguser
-
Create a new Rails app with PostgreSQL
rails new myapp -d postgresql To use MySQL: rails new myapp -d mysql
-
Configure Database connection Open config/database.yml and set up your configuration.
host: localhost username: pguser password: pguser_password
-
Create Database
RAILS_ENV=production rake db:create
-
Test Configuration
To test that everything is working fine start the rail server and bind it with your public IP
RAILS_ENV=production rails server --binding=server_public_IP
Browse to http://server_public_IP:3000 to see a welcome page.
-
Set up your project
Copy your project files to the server and run bundle install to install the dependencies
If you get an error related to pg gem, install the following files and run bundle install again.
sudo apt-get install postgresql sudo apt-get install python-psycopg2 sudo apt-get install libpq-dev
Run migrations
RAILS_ENV=production rake db:migrate
Compile the assets
RAILS_ENV=production rake assets:precompile