One of the most popular development languages and application runtimes is Ruby on Rails. From Wikipedia:
Ruby on Rails, often simply referred to as Rails, is an open source web application framework which runs via the Ruby programming language. It is a full-stack framework: it allows creating pages and applications that gather information from the web server, talk to or query the database, and render templates out of the box. As a result, Rails features a routing system that is independent of the web server.
Ruby on Rails emphasizes the use of well-known software engineering patterns and principles, such as active record pattern, convention over configuration (CoC), don't repeat yourself (DRY), and model–view–controller (MVC).
It is estimated that over 600,000 web sites are running Ruby on Rails. We are going to install the latest version of the Ruby language (version 2.1.2) on the latest version of Rails (version 4.1.2) on top of Ubuntu Trusty (version 14.04). With this setup you will be one of the cool kids on the cutting edge of development technology!
Create a CenturyLink Cloud server and update to Ubuntu Trusty
We need to get the CenturyLink Cloud server and OS created and set up. Follow the directions here to do so. Note: In addition to the standard ports (when adding the public IP address) you should specify port 3000 as well. This is the Rails default port.
Reboot your server and ensure that you have a terminal open and are logged into your server as root.
While you can do initial development with the built-in SQLite database you will not want to go to production, or even serious testing, without a more industrial strength database. The standard choices are either MySQL or PostgreSQL, today we are going to install and use PostgreSQL.
First, let's update our apt information and then get the packages we need:
$ sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list" $ wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add - apt-get update $ apt-get dist-upgrade $ apt-get install postgresql postgresql-contrib pgadmin3 postgresql-common libpq-dev
When PostgreSQL was installed it set up a standard default user postgres. For ease of use we are going to remove the database password from this account.
$ sudo -u postgres psql postgres=# ALTER USER postgres PASSWORD '';
What the above commands do is:
- Change the logged in user to postgres
- Run the PostgreSQL shell psql
- Change the password on the user postgres to "no password". You will note that this is done by issuing SQL commands through the database shell. Make sure you end the SQL line with a semi-colon, and enter "q" to exit psql and get back to the root user context.
In order to ensure we can connect locally to the database we need to alter the _pghba.conf file.
Currently this is located in the /etc/postgresql/9.3/main/ directory. Find the following section and update "peer" and "md5" to "trust".
Lastly, we need to ensure that PostgreSQL is listening correctly for localhost connections. To do this we need to edit the file postgresql.conf, located in the same directory as the pg_hba.conf file. Find the line _listenaddresses and ensure it looks like: _listenaddresses = 'localhost' # what IP address(es) to listen on; (you should be able to just uncomment this line).
Restart the database:
$ /etc/init.d/postgresql restart
The PostgreSQL database should now be running and you should be able to connect and interact with it locally (just as our Rails applications will). To test enter the following:
$ sudo -u postgres psql postgres=# l postgres=# du
The first command will show you all the tables currently defined. The second command shows the currently defined users on the system. Remember to enter "q" to exit.
Install Ruby on Rails
Now that we have the operating system and database set up, let's complete the exercise by installing the Ruby language and Rails runtime. We will not be doing this as root so first let's create a login (user and password).
$ adduser centurylinkdev
When prompted add centurylinkdev as the password on the account, then take the rest of the defaults. In order to give centurylinkdev the correct privileges we must add the user to the sudoers file.
# Edit the sudoers file to add the new user $ visudo
Add new user in the ‘User privilege specification’ section.
# User privilege specification root ALL=(ALL:ALL) ALL _centurylinkdev_ ALL=(ALL:ALL) ALL
Once the file has been successfully edited press CTRL-x to exit the file; and Y to save the changes. Logout as the root user and then login as centurylinkdev. We now have a normal user with sufficient privileges to install and run Ruby on Rails.
Install Ruby 2.1.2
To start with, we are going to make sure the apt repos are up to date and then install some dependencies.
$ sudo apt-get update $ 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
There are a few ways to install Ruby, we are going to use rbenv as it one of the most popular install systems. Of course, first we have to install rbenv and then we will install Ruby.
$ cd $ git clone git://github.com/sstephenson/rbenv.git .rbenv $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc $ echo 'eval "$(rbenv init -)"' >> ~/.bashrc $ exec $SHELL $ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build $ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc $ exec $SHELL $ rbenv install 2.1.2 $ rbenv global 2.1.2 $ ruby -v [/preformatted-text]
Note the version of Ruby installed on the system. Also, let's not have all the Rubygems packages documentation copied locally:
$ echo "gem: --no-ri --no-rdoc" > ~/.gemrc
$ sudo apt-get install software-properties-common $ sudo add-apt-repository ppa:chris-lea/node.js $ sudo apt-get update $ sudo apt-get install nodejs
And, finally, we get to install Rails:
$ gem install rails $ rbenv rehash
Run the following command to ensure that Rails is properly installed and at the correct version:
$ rails -v
Getting ready to ride the Ruby on Rails
We are very close to being done...We have to set up a PostgreSQL user, hook up the database, and then create our initial Ruby on Rails application.
Create a PostgreSQL user that has permissions to create databases, or expediency sakes name it the same as the current login user (centurylinkdev). If you decide to create a different PostgreSQL user you will need to update config/database.yml to add the new user to the credentials for the databases you create.
$ sudo -u postgres createuser centurylinkdev -s
And the grand finale... Let's create the initial Rails application:
$ rails new myapp -d postgresql $ cd myapp $ rake db:create
At this point you should logout and re-log into the centurylinkdev account. Run the following:
$ rails server
And, viola! You have a Rails application server running at PUBLIC_IP:3000. Point a browser here and you should see:
And if you hit the "About..." link you will see that Ruby is running version 2.1.2 and Rails is running version 4.1.2.
Now that you have the Rails framework up and running, with all components at the newest levels, you are ready to start writing the next big application in Ruby on the CenturyLink Cloud!