Saturday, December 8, 2018

Installing ruby on rails with Apache on Ubuntu 16.04 LTS for production

Recently I decided to start with ROR and move away from PHP.
So I decided to first setup production like environment for rails.
And to my surprise it wasnt the easy task to do, at least for me.
hence decided to blog about same and share.

Have taken heavy reference from two sites, and those are as given below.
  1. https://gorails.com/setup/ubuntu/16.04
  2. https://stackoverflow.com/questions/33573972/run-rails-application-with-apache-and-passenger-on-ubuntu/44920822#44920822
Was planning to go with latest rails but after lots and lots of try, was not able to resolve few of the permission issues. That is when one of my colleague suggested me to start with some old version.

Below are the version details:
Operating system: Ubuntu 16.04.5 LTS
Ruby: ruby 2.5.3
Rails: Rails 5.0.7.1
Passenger: Phusion Passenger 5.3.7

Note: first try below procedure on your dev, UAT or day-1 environment, do not follow it directly on production.

Let's start with installation process.


  • Make sure you have everything up to date on your Ubuntu box, use below command to do same.
apt update && apt upgrade -y && apt autoremove -y
  • Installing dependencies:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

sudo apt update
sudo apt install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn
  • we will be using rbenv to install ruby 2.5.3 
cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

rbenv install 2.5.3
rbenv global 2.5.3
ruby -v
  • Installing bundler 
gem install bundler
rbenv rehash
  • Installing node.js and rails
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
gem install rails -v 5.0.7
rbenv rehash
rails -v
  • Installing apache2
apt-get install apache2
  • Installing passenger
gem install passenger -v 5.3.7
passenger-install-apache2-module
For second command in passenger block may ask you to install additional packages, do follow as its prompted by program. Program will also suggest changes you need to make in Apache configuration.
once you update apache config, make sure you restart apache service.
  • Apache configuration
check for your apache config file, which is responsible for port 80 VirtualHost, make sure your config looks like below. and restart apache service again.
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/public/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SetEnv SECRET_KEY_BASE "0123456789abcdef"
        SetEnv DEPOT_DATABASE_PASSWORD "some-password"

        <Directory /var/www/html/public/>
            AllowOverride all
            Options -MultiViews
            Require all granted
        </Directory>
</VirtualHost>


We are done, Hope this helps!



No comments:

Post a Comment

Installing ruby on rails with Apache on Ubuntu 16.04 LTS for production

Recently I decided to start with ROR and move away from PHP. So I decided to first setup production like environment for rails. And to my...