Skip to content

First Time Deploy

Joe Cridge edited this page Apr 25, 2016 · 1 revision

Process for deploying an app for the first time.

On the development machine

  1. Add a public SSH key to your GitHub account:
    This is only necessary when the repository is private. Copy…

    $ cat ~/.ssh/id_rsa.pub | pbcopy
    

    …and paste here: https://github.com/settings/ssh.

  2. Add the private key to the forwarding agent on your computer:

    $ ssh-add ~/.ssh/id_rsa
    

    You need to use the same key as the one specified in config/deploy/production.rb.

  3. Let Capistrano do its thing…
    From the application root directory:

    $ cap production deploy
    

    You'll see a few red warnings the first time. Ignore them.

On the server

  1. Source the app secrets:

    $ source ~/.rails/secrets
    
  2. Install gems:

    $ cd /apps/my_app/current && bundle install
    

    You may need to enter your user password.

  3. Create and migrate the database:

    $ RAILS_ENV=production rake db:create
    $ RAILS_ENV=production rake db:migrate
    
  4. Precompile assets:

    $ RAILS_ENV=production rake assets:precompile
    
  5. Configure nginx:

    $ sudo mv /etc/nginx/sites-available/{rails,my_app}
    $ sudo rm /etc/nginx/sites-enabled/rails
    $ sudo ln -s /etc/nginx/sites-{available,enabled}/my_app
    

    Then, in /etc/nginx/sites-available/my_app, change

        root /home/rails/rails_project/public;
    

    to

        root /apps/my_app/current/public;
    

    and load the changes:

    $ sudo service nginx reload
    
  6. Configure Unicorn:
    Change /etc/unicorn.conf to look like this:

 listen "unix:/var/run/unicorn.sock"
 worker_processes 2
 user "webdev"
 working_directory "/apps/my_app/current"
 pid "/var/run/unicorn.pid"
 stderr_path "/var/log/unicorn/unicorn.log"
 stdout_path "/var/log/unicorn/unicorn.log"

and set the app root in /etc/default/unicorn:

APP_ROOT=/apps/my_app/current

Lastly, replace the lines

# Generate by running `rake -f /home/rails/Rakefile secret`
export SECRET_KEY_BASE=XXX…XXX
export APP_DATABASE_PASSWORD=xx…xx

with

# Source app secrets
. /home/webdev/.rails/secrets

and reload:

$ sudo service unicorn reload