Webserver 102

| Comments

In my last post, I explained how to create a webserver using Dediserve.com. [NB: Dediserve.com is the cloud provider]. This post will cover the second half of the webserver setup, including installing gems, deploying with Capistrano, generating SSH keys on github, installing Passenger, and installing and configuring nginx.

Now that you have a server that you can connect to, it’s time to set it up to run your application on it.

Prepping to Deploy

The first step in this process is install the build-essential package (which installs all the developer tools that your Ubuntu Server does not automatically come with, including gcc and make), as well as a Ruby 1.9, Rubygems and their development packages, and PostgresSQL (our database server).

In order to install all of these, you must run apt-get, which is Ubuntu’s management system. This system also speeds up the download process. Instead of spending days running around to all the necessary FTP servers, and then fetch and compile their packages, apt-get takes mere minutes. To get the correct version of this management system, run this command in your terminal:

sudo apt-get update

Once the update has completed, you’ll want to install the system.

sudo apt-get upgrade

Press enter when you are prompted to confirm.

The next step is to install the build-essential package.

sudo apt-get install build-essential

Followed by the Ruby packages

sudo apt-get install ruby1.9.3 sqlite3 libsqlite3-ruby libsqlite3-dev

This installs Ruby 1.9.3, SQLite3 and all of the libraries associated with SQLite3. Ruby and SQLite3 are necessary in order to get a functioning basic Rails application.

Once all of these are installed, you’ll need to install all of the dependant gems. I used Bundle for my gem management.

sudo gem install bundler

Then the SQLite3 gem.

sudo gem install sqlite3 sinatra

When creating this server, I was using a Sinatra application. If you are trying to use this process to set up your server for a Rails app, you would run sudo gem install sqlite3 rails.

I also used Passenger, which is a rack app server for Ruby and Python using Nginx. It essentially handles all the heavy lifting for managing your apps processes and resources. This means it makes deploying your app as seamless as possible. Nginx is an open-source web server for HTTP, SMTP, POP3 and IMAP protocols, and is quickly approaching Apache as the most popular. Because Nginx takes an event driven approach, they can handle higher loads of requests.

To install Passenger:

sudo gem install passenger


Capistrano is an open source tool which executes commands on multiple remote servers, concurrently, and is primarily used for deploying applications.

gem install capistrano

There are two commands available with Capistrano.
capify: used to prepare your application to be deployed using Capistrano
cap: used to actually deploy your application

You’ll want to make sure you’re in your repository’s directory before you run any Capistrano commands. From there, enter


This command creates a capfile and creates a directory called config. This directory contains a file called deploy.rb. The capfile is where the cap command accesses it’s configurations.

Next, you’ll need to edit the config/deploy.rb file. These edits will enable us deploy our application.

require 'bundler/capistrano' # for bundler support

set :application, "studentbody"
set :repository,  "GITHUB_REPOSITORY"

set :user, 'USERNAME'
set :deploy_to, "/home/#{ user }/#{ application }"
set :use_sudo, false

set :scm, :git

default_run_options[:pty] = true

role :web, ""                          # Your HTTP server, Apache/etc
role :app, ""                          # This may be the same as your `Web` server

# if you want to clean up old releases on each deploy uncomment this:
# after "deploy:restart", "deploy:cleanup"

# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts

# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
 task :start do ; end
 task :stop do ; end
 task :restart, :roles => :app, :except => { :no_release => true } do
   run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"

This file is all written in Ruby, just with Capistrano syntax. In this file, we set these variables:
application: the name of our application.
repository: the URL for the repository on Github. This needs to be replaced with yours.
deploy_to: The location on the remote server where we will deploy the application.
use_sudo: set to false because deploying to a location we own.
user: the user that we created to log into our server.
scm: the source-code-management system we’re using (in my case, Git).
default_run_options[:pty] = true.: need to ensure that Capistrano interacts with our terminal correctly.
role: We define two roles, both the web and app roles- to manage both the database and the application.
Uncomment :deploy because Capistrano is task-oriented. Here we uncommented starting, stopping, and resetting our application via the Passenger interface.
require 'bundler/capistrano: To ensure Bundler support.

You’ll want to make sure that both the capfile and config/deploy.rb are committed and pushed to your github repository.

Generating an SSH Key

If you are deploying your application from a private Github repository, you will need to generate a SSH key to act as your “deploy key”. This gives your server authentication to access your repository, without you manually entering your github name and password.

ssh-keygen -t rsa -b 4096

This create a key with the type of “RSA” (a global standard for encryption) and makes the key size 4096 bits (a veryyyy strong key).

You will be prompted for the location of the file and for a password. DO NOT set a password for this key. A password completely defeats the purpose of the key in the first place.

This key generates two files: ~/.ssh/id_rsa: your private RSA key ~/.ssh/id_rsa.pub: your public RSA key. This key is ok to distribute.

You’ll want to enter your private key into your github settings.

Once there, select the SSH Key option from the menu on the left of the page.

You’ll then want to select “Add SSH Key” and copy and paste your private SSH Key there. You don’t need to title it. Github will automatically generate a title to match your server. Make sure you save!

You’ll then want to add the contents of your public key to your server

cat ~/.ssh/id_rsa.pub

Preparing to Deploy AND Actually Deploying!

Because Capistrano has a full deployment framework built in, the first step is to ensure that it’s file structure is in place to deploy your app. In your terminal from the repository’s directory, run

cap deploy:setup

This command will connect to your server, prompt you for your password, and create the necessary directory structure on your server.

It creates a very simple file structure:


When your application is deployed, your github repository is copied into your releases/ directory. A symbolic link is created from that directory to the directory of your application, and names it current. This allows you to release updates of your code while still tracking older versions.

Once your github repository is up to date, you can deploy in one simple command

cap deploy

Nginx In’s and Out’s

Nginx is the actual webserver. It speaks HTTP and sits between the client (typically a browser) and your application. Passenger interacts with Nginx by starting up several “handlers” for your application. This means that several instances of your application will be running all at once. This allows your server to handle multiple requests to your server per second. This also ensures that each instance of your application is only handling once request at a time. Nginx speeds up and smooths out this process.

Passenger has partly configured Nginx for us, but to finish it off, you’ll want to edit /opt/nginx/conf. using vim. Vim is a text editor that you can load in your terminal.

sudo vim /opt/nginx/conf/nginx.conf

Once this file is open, you’ll want to make the following changes:

#user nobody; You will need to uncomment out this line and change it to read user www-data;.

Then you’ll want to find the block that reads:

location / {
  root   html;
  index  index.html index.htm;

And edit it to read:

root /home/USERNAME/APPNAME/current/public;
passenger_enabled on;

Nginx can be stopped and started by running


But because this command is long, you can create a shorten it to nginx by creating a symlink (symbolic link).

Terminal controlls Unix systems by navigating through paths. A path is a series of directories that denote a specific location. To inspect your current path, run

printenv PATH

You should get a response that looks like this:


We will want to place the symlink to nginx in /usr/local/sbin becuase, by default, /usr/local is the location that administrators should put any programs that aren’t automatically installed by your package managers. To do so,

sudo ln -s /opt/nginx/sbin/nginx /usr/local/sbin/

Now, nginx -h will display all the series of arguments associated with the nginx command.

To start Nginx, enter

sudo nginx

This should automatically return your prompt. Nginx spawns a connection in the background so that you can continue to work directly on your server. Once it’s running, you should be able to access your application by going to


To stop Nginx, enter

sudo nginx -s stop

And that’s it! Your application has been deployed to your very own server! Congratulations!!


Copyright © 2015 - Victoria Friedman - Powered by Octopress