Setting up a production server with ruby on rails (Deployment included) Part 2

Welcome to part 2 of setting up a production server for your ruby on rails application. In part 1 we setup rvm, ruby, and rails. In Part 2 of this tutorial we will setup a mysql, postgresql, and finally install apache & passenger.

Installing Mysql

Since we have rails and ruby setup lets install mysql. If you would like to use postgresql skip this part in go down below.

sudo apt-get install mysql-server mysql-client libmysqlclient-dev`

Now install the mysql2 gem for rails

gem install mysql2

Installing Postgresql

Now that we have ruby and rails installed we can install postgresql. First lets install the python software properties.

sudo apt-get install python-software-properties

Next well add the backports and repo and update apt

    sudo add-apt-repository ppa:pitti/postgresql
    sudo apt-get update

Next we’ll install postgresql

sudo apt-get install postgresql-9.1 libpq-dev`

Apache & Passenger

Now that you have your database setup we can finally install Apache and passenger which is what we’ll be using to power our server.

    sudo apt-get install apache2 libcurl4-openssl-dev apache2-prefork-dev \
                     libapr1-dev libaprutil1-dev

Next we will install the passenger gem

gem install passenger

Now we’re ready to install the Passenger Apache2 module which will setup our server files etc.

passenger-install-apache2-module`

After the installation is complete you will need to add in the three lines of code from the installation into your passenger.load file. After you have pasted the code into the file press cmd + x and Y to save the file.

sudo vim /etc/apache2/mods-available/passenger.load

Here is the 3 lines of code. Do not copy below, just for example

  LoadModule passenger_module /home/yourusername/.rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.5/ext/apache2/mod_passenger.so
  PassengerRoot /home/yourusername/.rvm/gems/ruby-1.9.2-p180/gems/passenger-3.0.5
  PassengerRuby /home/yourusername/.rvm/wrappers/ruby-1.9.2-p180/ruby

Now lets enable the passenger module

sudo a2enmod passenger

Next we will setup a virtual host for our website.

sudo vim /etc/apache2/sites-available/appname

Then well add our server and site information into the file.

      ServerName xx.xx.xx.xx
      DocumentRoot /home/yourusername/appname/public

        AllowOverride all
        Options -MultiViews

Now lets disable the default Apache page and enable our website

    sudo a2dissite default
    sudo a2ensite appname

Now reload your web server and go to your ip and you should see a blank white screen.

sudo /etc/init.d/apache2 reload

In part 3 of this tutorial we will deploy our rails application with capistrano.