Skip to content

Instantly share code, notes, and snippets.

@joshuaswilcox
Forked from brobertsaz/serversetup.md
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuaswilcox/a833b07bd519185fb53e to your computer and use it in GitHub Desktop.
Save joshuaswilcox/a833b07bd519185fb53e to your computer and use it in GitHub Desktop.

Ubuntu 14.04, RoR, Nginx, Puma, Postgresql and Capistrano deploy#

Server Setup

  • Create new server
  • Login to new server
    • ssh root@IPaddress
    • accept the RSA key
    • use the password that was provided when you set up the new server:
      • change the password:
passwrd
  • Create new user for deployment
sudo adduser "username"
  • accept the defaults
  • add new user to staff and sudo groups:
usermod -a -G staff "username"
usermod -a -G sudo "username"
  • switch user
su "username"
  • Update and Install dependencies:
sudo apt-get -y update
 
sudo apt-get -y install build-essential zlib1g-dev libssl-dev libreadline-dev libyaml-dev libcurl4-openssl-dev curl git-core python-software-properties libxslt1-dev libxml2-dev libmysqlclient-dev libsqlite3-dev nodejs
  • To make life easier, lets go ahead and add our ssh keys to the new server so that we do not have to sign in every time. (THis is done from your local terminal and not on the new server):

Add ssh keys

mkdir ~/.ssh
cat ~/.ssh/id_rsa.pub | ssh root@IPaddress "cat >> ~/.ssh/authorized_keys"

Install Ruby with RVM

Install your Ruby (obviously you use the version that you want. At this point many people use a Ruby version management like RVM or RBENV. As these servers are normally setup for just on application we will use the system version of Ruby):

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

\curl -sSL https://get.rvm.io | bash -s stable --ruby=2.2.0
 
echo "gem: --no-ri --no-rdoc" >> ~/.gemrc
 
gem install bundler

Database Setup

Setup mySQL:

sudo apt-get install postgresql postgresql-contrib libpq-dev
 
   * If you need to add your user to the database:
 
sudo -u postgres psql
 
postgres=# create user USERNAME createdb createuser password 'PASSWORD';

postgres=# create database DATABASE_NAME owner USERNAME;

Rails Deployment

Run the setup for capistrano

cap deploy:setup

and add any ssh-keys from your server to your git respository

cat ~/.ssh/id_rsa.pub

Nginx

Install Nginx:

sudo apt-get install nginx

Nginx will create several files for you. Setting these files up properly seems to be the biggest issue in the entire server setup. If you spand enough time you will find a multitude of different ways to configure these files. The following files are how I configured them and they work for me.

The nginx.conf file that is created required no editing for my setup to work:

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /usr/local/nginx/conf/mime.types;
    default_type  application/octet-stream;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;

    server_names_hash_bucket_size 128;
    
    client_max_body_size 4M; 
    client_body_buffer_size 128k;
    
    include /usr/local/nginx/conf/conf.d/*.conf;
    include /usr/local/nginx/conf/sites-enabled/*;
}

Nginx works with several sites on a server. These sites are listed in the sites available directory for Nginx (etc/nginx/sites-available/) and includes a default on install.

Add a new file in your repo called nginx.conf:

upstream app_name {
  server unix:///tmp/app_name.sock;
}

server {
  listen 80;

  server_name www.app_name.com app_name.com 104.236.22.101;
  root /home/rails_user/apps/app_name/current/public; # change to match your rails app public folder

  location / {
    proxy_pass http://app_name; # match the name of upstream directive which is defined in line 1
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
  }

  location ~* ^/assets/ {
    # Per RFC2616 - 1 year maximum expiry
    expires 1y;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }
}

and in the deploy.rb file add the following to the setup area:

sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment