Skip to content

Instantly share code, notes, and snippets.

@the-teacher
Created September 25, 2015 16:45
Show Gist options
  • Save the-teacher/dd27b269e6538648fe8b to your computer and use it in GitHub Desktop.
Save the-teacher/dd27b269e6538648fe8b to your computer and use it in GitHub Desktop.
Vagrant.startup

HOST MACHINE

  brew cask install virtualbox
  brew cask install vagrant
  brew cask install vagrant-manager
  vagrant plugin install vagrant-vbguest
  vagrant plugin install vagrant-librarian-chef-nochef
  git clone git@github.com:the_teacher/PROJECT
  cd PROJECT

Place config files:

  >> PROJECT/config/database.yml
  >> PROJECT/config/settings/development.local.yml
  >> PROJECT/DeployTool/settings/production.yml
  vagrant up
  vagrant ssh

VAGRANT MACHINE

  cd /vagrant
  bundle
  rake db:create && rake db:migrate
  ssh-keygen -t rsa -b 4096 -C "vagrant@open-cook.ru"
  >>
  >>
  >>

  ssh-add ~/.ssh/id_rsa && ssh-add -L

  cat ~/.ssh/id_rsa.pub | ssh deployer@server.com 'cat >> ~/.ssh/authorized_keys'
  >> password
  cd /vagrant/DeployTool/

  bundle

  mkdir -p ~/DUMPS

  cap production db:dump
  >> YES

  mysql -u root -pqwerty project_db_dev -S /run/mysql-vagrant/mysqld.sock < ~/DUMPS/project_production.XXX.mysql.sql

  cap production rsync:files
  >> YES

PROXY ASSETS VIA NGINX

  sudo apt-get install nginx
  sudo nano /etc/nginx/nginx.conf
  ##
  # Logging Settings
  ##

  # access_log /var/log/nginx/access.log;
  # error_log /var/log/nginx/error.log;
  # include /etc/nginx/conf.d/*.conf;
  # include /etc/nginx/sites-enabled/*;
  include /vagrant/config/nginx.development.conf;
  sudo /etc/init.d/nginx restart
  sudo service nginx restart
  nginx -t
  cd /vagrant

  |=> rails s -b 0.0.0.0
  -- or --
  |=> unicorn_rails -c ./config/unicorn.development.rb
  -- or --
  |=> ( sudo /etc/init.d/nginx restart ) && ( unicorn_rails -c ./config/unicorn.development.rb )

@the-teacher
Copy link
Author

config/unicorn.development.rb

timeout 60
preload_app true
worker_processes 4

working_directory '/vagrant'

# listen "0.0.0.0:3000"
pid '/vagrant/tmp/pids/unicorn.dev.pid'

# not `/vagrant` because of Vagrant sockets problem
# for sockets you have to use /tmp folder
listen '/tmp/unicorn.dev.sock', backlog: 1024

# stdout_path '/vagrant/log/unicorn.dev.log'
stderr_path '/vagrant/log/unicorn.dev.error.log'

before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

@the-teacher
Copy link
Author

config/nginx.development.conf

upstream vagrant_project {
  server unix:/tmp/unicorn.dev.sock fail_timeout=60;
}

server{
  listen 80 default_server;
  server_name  localhost;

  error_log  /vagrant/log/nginx.dev.err;
  access_log /vagrant/log/nginx.dev.acc;

  charset utf-8;
  proxy_intercept_errors on;

  root /vagrant/public;

  client_max_body_size 256m;

  error_page 403 /403.html;
  error_page 500 501 502 503 504 /500.html;

  location ~ ^/(uploads|special_posts|images|system|default_images)/ {
    expires max;
    gzip_static on;
    add_header Cache-Control public;
  }

  location / {
    try_files $uri @web_site;
  }

  location @web_site {
    proxy_pass http://vagrant_project;

    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_read_timeout 300;
    proxy_buffer_size 16k;
    proxy_buffers 32 16k;

    proxy_set_header X-Forwarded-Proto $scheme;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment