Ruby on Rails Server build

This guide will follow the journey that is setting up a rails production server from scratch. I hope it makes it easy for you. Expect this to take at least 2 hours. Budget for 4 or more if it is your first time.

Base guide that I followed. Reference

One of the guides it says to go through is this. Reference

Ubuntu add and delete users. Reference

WARNING: Signing up for Digitalocean costs money. It is very inexpencive starting at $5 per month or $0.007 per hour. See their site (DigitalOcean) for more details. I am not affiliated with them and recieve no money for this, but am a user and fan. If you chose to use a different solution then go for it, most of this will be the same.

What you will end up with:

  • a vps from digitalocean.com
  • ruby
  • rails
  • postgresql
  • a live production website
  • various other tools that will allow you to work effeciently on the server

Insert your username and project name. The tutorial will fill them in to make it easy for you to copy and paste. This information will not be kept, used or saved in any way other then what is stated here.

PUTTYgen Download

We will start out by creating an SSH key so that we have a very secure way to sign in to our new server.

To do this go download this really oldschool tool from this really oldschool site.

PUTTYgen

Click on the link that says...

puttygen.exe

Download it and put it on your Desktop, or anywhere else you normaly use for files. Don't put it in C:/program_files or any other folder close to that name. Then open PUTTYgen.

Generate an SSH key

Click generate and then litteraly move your mouse around in the box at the top. This is used to generate randomness for the gigantic math problem that they are about to solve. After it gets enough of your randomness it will take a few seconds to generate the key.

Fill in a password for the private key and then save the private key.

What to do with the public key

Copy the text that replaced the mouse dancefloor of randomness and paste it in 2 different places. The first is a text file (use notepad) and save it somewhere you will remember where it is. The second is past it into our Digitalocean account to save it for server creation, oh right we havent gotten there yet, sorry. This is why we are saving it in a file, but you can just leave PUTTYgen open until we need it.

Save your public key.

Create a Digitalocean Droplet

So let's do this. If you havent already, set up an account with digital ocean, they will need a form of payment, and stuff. Once signed in click on create a droplet.

Name your droplet.

Chose size.

Chose your region, preferably the one closest to where your users will be.

Select your image, in this case we will be using ubuntu 14.04x64. There are tones more options here, I highly encourage you to explore this at some point and start googling the stuff you dont understand.

Click add ssh key and paste in the text from PUTTYgen.

Add the comment so you remember what it is for.

Click create droplet and wait 60 seconds. (This has rarely taken longer.)

Setup PUTTY

Next we are gonna go back to the PUTTY site and get...

PUTTY.exe.

Put in the same folder where you put PUTTYgen and open it.

Type in the IP address fom your Digitalocean server when it is done building. It will look like this: 56.143.15.64 (This is a random one, don't use)

Click on Connection > Data. in the "Auto-login username" type:

root

Expand ssh and click auth.

At the bottom where it says "Private key file for authentication" borwse to your ssh key .ppk that you saved.

Go back to

session

Where is says "Saved Sessions", type in a name for your session and click save. Then click open.

It will ask you for your password, type it in, you will not see it typing, just hit enter when you are done.

BONUS: You can put this app and your SSH keys on a USB drive and run it on any computer.

Create a New User

you are now logged into your server. The first thing we are going to so is to create a new user. This is done for security reasons. I Recoment not doing anything as the root user other then create your first new user.

adduser username

then give that user sudo privelages

gpasswd -a username sudo

then we are going to switch to that user

su username

NOTE: Next time you login to this server you will have to switch users unless you follow the last step.

Update apt-get

Cool, now we need to set up some basics that you should be familiar with, if you are not, google them, they are good to know. I will bring you through the basics throughout this toutorial.

To install software on ubuntu you can use apt-get.

This first one updates the list of software available.

sudo apt-get update

Install VIM

Next we will install VIM. It is a command line text editor. If you don't know how to use it, don't worry. This guide will walk you through using it.

sudo apt-get install vim

There is a awesome tutorial that takes about 30 minutes (Optional)

vimtutor

Install Git

Next we install Git.

sudo apt-get install git

If you dont know how to set up git, use this DigitalOcean tutorial.

Install fail2ban

Install a firewall. This will help secure your server from brute force attacks

sudo apt-get install fail2ban

I recomend that you google this and learn about it. It can be very usefull, but its not necessary right away.

Now to Install Ruby!

First we have to install a bunch of stuff that ruby and rails needs.

sudo apt-get install build-essential libssl-dev libyaml-dev libreadline-dev openssl curl git-core zlib1g-dev bison libxml2-dev libxslt1-dev libcurl4-openssl-dev libsqlite3-dev sqlite3

mkdir ~/ruby

cd ~/ruby

wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.2.tar.gz

tar -xzf ruby-2.2.2.tar.gz

cd ruby-2.2.2

These next 3 steps will each take a long time.

./configure

make

sudo make install

That is it. Check to see the Ruby version.

ruby -v

It should return this or something similar:

ruby 2.2.2p265 (2014-10-27 revision 48166) [x86_64-linux]

Lastly, some clean up.

cd ~

rm -rf ~/ruby

Install Apache2

Now on to the loooooong journey of installing apache2.

sudo apt-get install apache2

OK, neeext...

Install Passanger

First we need to add the repository to Ubuntu's list.

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7

Now open this file with Vim.

sudo vim /etc/apt/sources.list.d/passenger.list

add the following to the file. To do this, press o and then right click to paste

deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main

hit esc then : then type wq then hit enter

Congrats, you just used Vim!

sudo chown root: /etc/apt/sources.list.d/passenger.list

sudo chmod 600 /etc/apt/sources.list.d/passenger.list

sudo apt-get update

sudo apt-get install libapache2-mod-passenger

sudo a2enmod passenger

sudo service apache2 restart

sudo rm /usr/bin/ruby

sudo ln -s /usr/local/bin/ruby /usr/bin/ruby

Install Passenger Gem

Reference second answer.

sudo gem install passenger

Go through the following setup. At the end of the setup you will be told do some more things. They are listed below but check and make sure that they are the same.

Make sure and chose ruby and nodejs!

sudo passenger-install-apache2-module

These are the things you have to do after.

sudo apt-get install apache2-threaded-dev

sudo apt-get install libapr1-dev

sudo apt-get install libaprutil1-dev

That is done.

Paste the link below into your browser for Documentation.

/usr/local/lib/ruby/gems/2.2.0/gems/passenger-5.0.10/doc/Users guide Apache.html

Reference

Install Nodejs

I know it sounds wierd to be installing this but it is required for rails.

sudo apt-get install nodejs

Yup, thats it, and now on to postgresql.

Install Postgresql

Great how to on digital ocean Postgres Install. Everything you need from and more is below.

sudo apt-get install postgresql postgresql-contrib

must install the this library for the pg gem to work pg gem requirements.

sudo apt-get install libpq-dev

Log into postgres user.

sudo -i -u postgres

Create a user with the interactive tool.

createuser --interactive

Enter your username.

username

Then chose yes for superuser.

y

Open the postgres terminal.

psql

ALTER USER username WITH PASSWORD 'password';

Type \q and enter to exit.

\q

Now type exit to exit.

exit

and you are done!

If you ever need to restart postgres look here. Reference

Checking postgres for roles and databases. Reference both answers

This lists the databases

postgres=> \l

This lists the users

postgres=> \du

Install Bundler and Rails

Time to finaly install Rails, but first lets install Bundler.

sudo gem install bundler

sudo gem install rails

If you have any other programs that your rails project will need now is the time to do that.

Clone Project

Finaly! We are at the point of cloning our rails project. I prefer to do this to /home/cool_user/my_project. You might also hear you should put it in /var/www, if you want to do that you can but in the past I have run into issues with permissions.

cd ~

git clone https://github.com/your_user_name/project.git

Now bundle install and setup your database.

bundle install

RAILS_ENV=production rake db:create

RAILS_ENV=production rake db:migrate

Seed if you have to.

Setup Rails Production

Put secret_key_base into ENV Reference.

RAILS_ENV=production rake secret

It will generate a string of charicters that looks like the line below. This one is not real, I just hit a bunch of random keys on the board.

45jkjh4pz576ghyt37n4oycd80vc54hm18fkf6h6ti45tj45emdh6c76843mfjv74j96jmd90fyc6

Now we have to put that into the enviroment file. There are 3 files below. You only need to update one. If you have more then one then chose the one that is higher on the list.

vim ~/.bash_profile

vim ~/.bash_login

vim ~/.profile

Add this code to the bottom of the file.

export SECRET_KEY_BASE=45jkjh4pz576ghyt37n4oycd80vc54hm18fkf6h6ti45tj45emdh6c76843mfjv74j96jmd90fyc6

Reload that file.

source ~/.profile

Check if its working.

echo $SECRET_KEY_BASE

Compile Assets

Complile the assets for production. Reference, first answer.

In /config/environments/production.rb I had to add this:

config.assets.compile = true

In /config/initializers/assets.rb I had to add this:

Rails.application.config.assets.precompile = ['*.js', '*.css', '*.css.erb']

Then we need to compile the assets.

RAILS_ENV=production bundle exec rake assets:precompile

Last thing to do on the server is to activate the site in apache.

cd /etc/apache2/sites-available/

Rename the default conf file. If you are hosting more then one site on this server, you need one of these for each site.

sudo mv 000-default.conf my_site_name.conf

Now we need to edit this file and enter the proper settings.

sudo vim my_site_name.conf

The file should look like the one below but colorful.

<VirtualHost *:80>
	# a bunch of comments
	ServerName www.my_site.com
	ServerAlias my_site.com
	ServerAdmin webmaster@localhost
	DocumentRoot /home/my_user_name/my_site/public
	RailsEnv production
	# a bunch of comments
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
	<Directory "/home/my_user_name/my_site/public">
		Options FollowSymLinks
		Require all granted
	</Directory>
	# a bunch of comments
</VirtualHost>

When you are done press esc then type the following...

:qw

and press enter.

Last thing you have to do is to set up the DNS server in your registrar and in your Digitalocean account.

Follow this tutorial from digital ocean.

To point your domain to digital ocean, google the following...

point (my_registrar) to digitalocean

This should come up with an answer for your registrar. If it doesn't, feel free to contact me.

You are done congradulations!

Cheers!

Debugging stuff

Invariably you will have issues and or bugs. Check these 3 files to start. Once you open them in vim press G to get to the end of the file.

vim /home/username/projectname/log/production.log

sudo vim /var/log/apache2/error.log

sudo vim /var/log/apache2/access.log

press shift + g to jump to the last line.

Happy Debugging!

Rails Console

To open rails c, you need to chown folder for temp a file. Reference, first answer.

chown -R cool-user: /run/spring/0source ~/.bash_profile

To get into the rails console using the production database. Reference

RAILS_ENV=production rails console

Try one these in order.

rails console production

bundle exec rails console production

Postgresql help

If you find yourself not able to connect to postgres, here is how to close those connections.Reference