Choose any Linux distribution you feel comfortable with. I’ll choose Ubuntu because I’m familiar with it (just don’t choose Windows unless you know what you’re doing, Windows is a real pain for this sort of things). In step 2, choose the instance type. I chose t2.micro as it’s just enough for me. The great thing about cloud computing is that you can always obtain more resources with little effort. In step 3, leave all the defaults but Auto-assign Public IP, which should be enabled if you want your site to be accessible from anywhere. Set up everything according to your preferences or just use the defaults until step 6: Configure security group. Here we need to create a new security group and make sure inbound SSH and HTTP connections are allowed. With SSH, just allow SSH inbound traffic from your particular IP and nobody else’s (or else someone might be able to sneak into your instance!). HTTP (TCP port 80) should be reachable from anywhere, which translates to the address 0.0.0.0.
Once you’re happy with the initial configuration, proceed to create your instance by pressing the Launch button from the Review screen. It will take a while to set up. You can check the status of your new instance from the EC2 Dashboard (you can get to it from anywhere in the AWS Console via the Services tab at the top). Once your instance is ready, we’ll need to SSH into it using the default username and IP provided by AWS a well as the PEM file containing the public key we need to authenticate ourselves to the instance. Right click on the instance and select ‘Connect’. AWS will probably prompt you to generate this unique PEM file. After obtaining it, make sure you keep it somewhere safe, as you’ll need it handy every time you want to connect to your server. Your SSH command will look something like this
$ ssh -i pemfile.pem email@example.com
Now we’ll install everything we need to create our site, starting with RVM.
RVM is a command-line tool which allows you to easily install, manage, and work with multiple ruby environments from interpreters to sets of gems.
In essence, RVM lets you have a completely segregated environment for every Ruby application you develop, avoiding versioning conflicts with gems and many other issues. Install RVM
$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 \curl -sSL https://get.rvm.io | bash
This will import RVM’s GnuPG public key and install it using a script obtained from their website. Make sure you change your .bashrc/.zshrc to source RVM when opening a shell session by adding this line to it while also ensuring your $PATH starts with
$ source ~/.rvm/script/rvm
The RVM installation script should have done this for you anyway, but it’s always good practice to check yourself to avoid any headaches later. Now install Ruby (at the time of this writing, 2.2.3 was a current stable release, but yours might be a later one)
$ rvm install 2.2.3
Set this ruby as the system default
$ rvm use 2.2.3 --default $ gem install rails --version 4.2.4
At this point, we have everything laid out to make our site come to life. All that’s left is a content management system to help us through the last configuration steps. The first thing I did was research what’s out there. Since I’m all about minimalism, I didn’t do an awful lot of that. Any CMS would probably cater for my unambitious requirements so I went for the most popular option first: Radiant. Mature, stable and really powerful, but alas the timing was not ideal, as the project was in an unstable status at the time. Its Github page indicated it was not even building and its dependencies were broken. That just seemed to corroborate my own results when installing it (it just didn’t install). So I decided to settle for RefineryCMS, an increasingly popular RoR CMS. Lightweight, dead easy to install and very configurable. It doesn’t have lots of fancy built-in options but, on the flip side, it gives you a great deal of room for customisation and tweaks and it’s very easy to get up and running with minimum adjustment.
One of the neat things about RefineryCMS is that it doesn’t require installation. You can install it as a gem, sure, but you don’t have to. And I found it less headache-prone to not do so. The alternative that I went for is to create a new normal RoR application and then pass to the
rails generate command the URL of a rake script that will set everything up for you and will give you a RefineryCMS site ready to use.
RefineryCMS’s Getting Started Guide instructs you to use this other URL with a fixed version number
$ rails new rickrockstar -m http://refinerycms.com/t/2.1.0
I had issues trying to create my application using this particular script, so instead I used the latest version
$ rails new segfault -m http://refinerycms.com/t/edge
Your site is ready to go. Now, we need to start WEBrick, RoR’s default web server. Navigate to your site’s directory, e.g.
cd segfault. If you were just testing your site locally, you could just invoke the server straight away with
rails server but if you want your site to go public, we’ll need to provide a few extra flags.
$ rails server --binding=0.0.0.0 -p 80
This starts up a WEBrick server instance ready to serve your site, accepting any requests made on HTTP default port 80… well, not really. Turns out you need root permissions to bind a WEBrick server instance socket to TCP port 80. But fret not, as RVM comes to our rescue.
$ rvmsudo rails server --binding=0.0.0.0 -p 80
Now this will work.
rvmsudo will execute with root privilege the ensuing command, in this case spinning a server instance.
Finally! You should now be able to enter the address of your instance. At the time of this writing, I don’t have an AWS Route 53 purchased domain yet, so that’s why I’m using my instance’s public IP.
The first time you start Refinery, you’ll be greeted by the User Registration screen so you can create an account to edit the site. Once you’ve done this, just go to the Pages section on the left and create a new child of the Home folder. You’ll land on the page editor. You can insert links, pictures and use raw HTML to write your page, all the basic tools you would expect. I’m not going to explain any of this in further detail as Refinery is fairly straightforward and well documented. You’re more than encouraged to consult the documentation, as this tutorial is just meant to get you started.
Once you’re happy with the contents of your first post, click on the Save button below. Your page will be posted now and you’ll be able to view it if you click on the eye icon next to its name in the Pages section we talked about earlier.
But doesn’t our page look a bit too… empty? What you’re currently seeing is your blog post being skinned by Refinery’ default view template. Refinery encourages people to extend or reimplement altogether the page templates they provide, so everyone can come up with their own style.
RefineryCMS makes use of rake tasks to override page views.
$ rake refinery:override view=refinery/pages/show
Edit the contents of the file
app/views/refinery/pages/show.html.erb. Mine looks something like this.
Now that we have laid down the structure of a page, it’s time to style it. RoR 4 places stylesheets (CSS) in
app/assets/stylesheets. If you want to style the pages which belong to the Home folder, create a new file, home.scss, and stick your CSS rules in it.
And that’s about it! From now on, you’ll be able to post articles on your Ruby on Rails blog running on an inexpensive server hosted in the cloud. If you want to give your site some basic blogging functionality like categories, comments and post moderation, check out this Refinery extension. It’s fairly simple to install. Happy blogging!
DISCLAIMER: Just wanted to point out that I am aware that there are far better ways to get a RoR application out of the door. Amazon offers services for every layer of the cloud service domain. EC2 lies at the lowest level of their cloud stack, also known as Infrastructure as a Service or IaaS; if you’re looking for something more ready to go, but not quite as much as WordPress or Blogspot, check out Amazon Elastic Beanstalk or Heroku, which is a very popular option.