Tips and Resources For Setting Up Website From Ground Up
Sep 14, 2015
18 minute read
This is a summary of the whole procedure of How to build your own website using Rails Framework. It sounds really simple in the Rails Guide, but if really want to go over the whole procedure, there are steps beyond writing a small App that runs on your own computer. It is kind of a summary that reminds me things I need to care about when running a serious App in production environment, so I think it suits people who just learn how to build Rails App while want to know more about how to actually run it in production environment.
This Topic covers all this contents below:
Simple Rails App & Github
Domain, DNS & Server
Deployment
Web Server & App Server
In the future there might have:
Database
Cache & Queue
Security
But for now I’m only focusing on getting through the most basic steps.
1.Simple Rails App & Github
If you already familiar with Rails and Github, just ignore this first step.
Build a Toy Rails App is probably the most easy part of this article, just follow the detailed Getting Started with Rails article here. To follow up, please go over it and I’m assuming you now have a code repository called blog on your own computer, and with command line rails s you can run it and view it in browser with address localhost:3000.
If you consider Rails too fancy for you and you just care about building static website (e.g.: for blog), use Jekyll is also a nice choice to get started, with this guide you can setup a toy blog repository in seconds, and view in with localhost:4000.
To set up production environment, you also need Version Control, nowadays most web developers use Git, you can use git with Github, or you can set your own Gitlab Web App to manage your code, or else you can just simply use git on server solely with command line, its your choice to use which method to apply. Though this article mainly focus on using Git with Github. There is also a very nice written procedure on Github here, after following these guidelines you should have a github repository called blog now.
2.Domain, DNS & Server
Many companies provides their own whole package of App management service, like Amazon AWS, Heroku etc, and to be more advanced, people also invent a container called Docker. They are pretty good if you want to develop/copy a prototype really fast. But to some extent, for beginners, they are too much and might freaks you out with their loads of services. So let’s separate them to several basic pieces first.
Domain
To make your project online, you need a domain, which is simply an address you have on the Internet. You can easily get a third level domain for free if you just want to publish a blog(e.g.: myblog.tumblr.com), but to get your own second level domain, you probably need to rent one. (Let me know if you can find a free one. :) ) Personally I use domain providers like GoDaddy and Gandi, and I prefer Gandi more because their Interface is cleaner. To get a domain, simply go to one of this providers and rent one name you like, it won’t be too expensive for the first year. After the payment you should get a domain name like foobar.com
Server
Most developers don’t use physical servers now, I suggest renting a simple virtual host, it is convenient because guys in charge of the real servers will help you fix security issues and upgrade your hardware without you even notice. Then all you need to care about is on the software side, besides, real servers are really expensive and too fancy to play with. I personally use Digital Ocean, they use SSD hard drives and the price of minimum setting is cheap. Another popular choice is Linode, I think they have a longer history and some little birds of mine said that the setting of Linode server is more comprehensive. After the payment and very easy settings (If you use Digital Ocean also), you should have a Server System installed, let’s assume it is the latest stable version of Ubuntu, and in order to make your App work, you also need set up the environment for your App to run, with sudo permission it shouldn’t be hard. And the most important thing is that, now you get your own server IP address like 123.132.213.231
DNS
DNS, according to Wikipedia, is short for Domain Name System, but to understand it in the most superficial way, it is the thing that connect your domain name and your IP address.
There are two awesome and practical articles that can lead you through the procedure of making this connection.
As you can observe, there are lots of good articles about server configuration on DigitalOcean, another reason I use it. After this step, you should able to ping your server with the domain name like the second article said.
3.Deployment
Most of times I use Capistrano, the thing about Capistrano is that there is some significant difference between Capistrano 2 and Capistrano 3, to keep up
with the pace, I’ll look into details of Capistrano 3 here. FYI, there is also a deploy tool called Mina that is getting popular.
After install Capistrano via the Official Guide, there are three files you need to change. I’ve detailed comment the configuration usage in the code below.
/Capfile
/deploy.rb
/config/deploy/production.rb
Then you’re all set on production deploy configuration.
4.Web Server & App Server
The easiest way is to use Passenger, the typical way is to use Nginx+Unicorn, besides Passenger and Unicorn, Puma is also a popular app server choice. Here I would describe how to set up Nginx+Unicorn on server.
First one need to install Nginx on server and unicorn gem, notice here that unicorn is a gem that should be part of the dependency of your Rails App, while Nginx is a popular web server that can work with lots of frameworks beyond Rails. After installation, there are three files that is important for configuration, I’ve commented the important part in the code below
Some of comments originally from other people’s scripts, the references are listed below. Thanks if any of you see this. :)
There are lots of instructions of the server configuration procedure because servers run in various environments, the code above showcase the most typical configuration from my perspective.
After complete all these configurations, restart nginx server and deploy your code again, then your site should properly running now.