Posts Tagged ‘Ruby on Rails’

Installing Ruby on Rails – Ubuntu Linux

Posted in HowTo, Linux, Ruby on Rails, Ubuntu on January 3rd, 2010 by Doug – Be the first to comment

Over the past few weeks I have been teaching myself ever so slowly Ruby on Rails. While my background is system administration, I have always had an eye for programming logic and a respect for programmers. The first place one needs to start when learning Ruby on Rails is getting up and running Ruby on Rails on the Server or Workstation.

I already have a dedicated Ubuntu Servers so I will leverage what I already have implemented and hook in Ruby on Rails. For my server environment I already have Apache 2, MySQL Server and a few other tools so I will start with that as a baseline. If you don’t have any of this installed already I have created a primer below.

Install Apache Server

sudo apt-get install apache2

After the install, Apache will autostart. As a result you might find an error

* Starting web server apache2
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
   ...done.

While this is annoying, it does not impact Apache’s ability to operate but the fix is simple and resolved by creating a file called fqdn (fully qualified domain name) in /etc/apache2/conf.d/. Mine looks like this:

ServerName localhost

Restarting Apache will now result in a clean startup.

sudo /etc/init.d/apache2 restart

Install Mysql Server

While Ruby runs great with sqlite, I have on my server MySQL as it performs all the needs I have in a shared database environment. Installing is simple and is accomplished with just a few simple commands.

sudo apt-get install mysql-server
sudo apt-get install libmysqlclient16-dev

During the install, MySQL will prompt you to create a root password for the admin user.

To test the installation, log in to MySQL via command line to confirm.

doug@audi:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 34
Server version: 5.0.75-0ubuntu10.2 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>

And to view the databases

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
+--------------------+
2 rows in set (0.00 sec)
mysql>

Install Ruby from Source

While this solution might promote some argument, I believe by far the best way to work with some applications, including Ruby on Rails is to install from source. This method will give you the most control as well as provide stability when performing system upgrades or during the patching process.

Make a directory for all your source code. This will come in handy when you need to re-compile and add in additional functionality for future efforts.

You may already have a /usr/local/src directory but if you don’t, simply create one for yourself.

sudo mkdir /usr/local/src

We will place all source code for Ruby and RubyGems in this directory.

Download Ruby

cd /usr/local/src
sudo wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p174.tar.gz

You need to use sudo with wget in this case because as a normal user, you wont have the necessary permissions to save to this directory.

Uncompress ruby-1.8.7-p174.tar.gz

sudo tar -xzvf ruby-1.8.7-p174.tar.gz
cd ruby-1.8.7-p174
sudo ./configure
sudo make
sudo make install

Ruby is now installed in /usr/local/bin/ruby

While Ruby is installed, there a still 2 pieces that are needed before we install RubyGems.

Install zlib for Ruby

cd /usr/local/src/ruby-1.8.7-p174/ext/zlib
sudo ruby extconf.rb --with-zlib-include=/usr/include --with-zlib-lib=/usr/lib
sudo make
sudo make install

Install openssl for Ruby

cd ../openssl/
sudo ruby extconf.rb
sudo make
sudo make install

Install RubyGems

cd /usr/local/src/
sudo wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
sudo tar -xzvf rubygems-1.3.5.tgz
cd rubygems-1.3.5
sudo ruby setup.rb config
sudo ruby setup.rb setup
sudo ruby setup.rb install

Install RAILS

sudo gem install rails

You will need to enable RAILS to talk to our database and this is done by installing the mysql gem.

sudo gem install mysql

Optionally, you can install Mongrel instead of using WebBrick.

sudo gem instal mongrel

Install Passenger

Passenger basically allows you to proxy RAILS applications via apache to the Ruby server – WebBrick or Mongrel. The installation is rather easy and is installed via a Ruby gem. Once installed you will need to modify your Apache configurations to point to the appropriate RAILS application.

sudo gem install Passenger
sudo passenger-install-apache2-module

After running passenger-install-apache2-module, it said I was missing some dependencies. In my case I needed to install the following via apt-get.

sudo apt-get install build-essential apache2-prefork-dev libapr1-dev libaprutil1-dev

After installing the dependancies, re-run passenger-install-apache2-module.

sudo passenger-install-apache2-module

The passenger configuration requires you to modify your Apache configuration file. In my case I create a file in /etc/apache2/conf.d called passenger.

sudo vi /etc/apache2/conf.d/passenger
LoadModule passenger_module /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.8/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.8/gems/passenger-2.2.8
PassengerRuby /usr/local/bin/ruby

Then you need to create a virtual host to point to your new application. In my case it is setup in this fashion but there are many other ways of accomplishing the same thing.

sudo vi /etc/apache2/site-available/ruby.dougjaworski.com

What my virtual host file looks like.

< VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName ruby.dougjaworski.com
        DocumentRoot /home/doug/rails_apps/test/public
        < Directory /home/doug/rails_apps/test/public>
                Options MultiViews
                AllowOverride All
        < /Directory>
< /VirtualHost>

It is important that you point DocumentRoot to the location of your applications public folder as this is what is exposed to Apache.

It is also important that you have a DNS record or at the very least an entry in hosts host pointing to your Apache virtual host as this is named based resolution.

Now you must enable the virtual host.

sudo a2ensite ruby.dougjaworski.com
[sudo] password for doug:
Enabling site ruby.dougjaworski.com.
Run '/etc/init.d/apache2 reload' to activate new configuration!

Reload the Apache configuration so your changes take effect.

sudo /etc/init.d/apache2 reload

Point your web browser to your new RAILS site!