Nginx and Puma for Ruby on Rails
When a lot of people are working on the same Rails application, than Vagrant could help to set up environment quick and easy. Even Vagrant is not recommended for production, it is very usefull for testing deployment script. For production we can simply copy deployment script and run manually.
We can deploy Ruby on Rails app using a quick way to deploy ruby on rails on vagrant. So clone that project with
git clone https://gist.github.com/8815e439905dbd326a41.git vagrant vagrant init # this generates Vagrant file
Deploy on Digital Ocean
If we deployed to VirtualBox, than we know to deploy to Digital Ocean as well, we just need their API key.
Vagrantfile to add
# Vagrantfile config.vm.hostname = "myapp.example.com" config.vm.provider :digital_ocean do |provider, override| override.ssh.private_key_path = '~/.ssh/id_rsa' override.vm.box = 'digital_ocean' override.vm.box_url = "https://github.com/smdahlen/vagrant-digitalocean/raw/master/box/digital_ocean.box" override.vm.provision :file, source: '~/.my_app_staging.env', destination: '/vagrant/.secrets.env' override.vm.provision :shell, path: 'vagrant/bootstrap_ruby_on_rails.sh', keep_color: true # we dont need those folders on production override.vm.synced_folder ".", "/vagrant", type: "rsync", rsync__exclude: [".git/", "tmp/", "log/", "lib/", "docs/", "public/"] provider.token = ENV["DIGITAL_OCEAN_TOKEN"] provider.image = 'ubuntu-14-04-x64' provider.region = 'nyc2' provider.size = '1gb' provider.ssh_key_name = `hostname`.strip # this key name is from https://cloud.digitalocean.com/settings/security # and is used for vagrant ssh. default is Vagrant and it will be created if public key is not already there end
To boot machine on digital ocean, you need to register DIGITAL_OCEAN_TOKEN
API key. If you already added ssh key to your account
than set it's name to
provider.ssh_key_name = name param.
Before provisioning it, you need to install plugin
vagrant plugin install [vagrant-digitalocean](https://github.com/smdahlen/vagrant-digitalocean).
To see all images (regions, sizes) available:
vagrant digitalocean-list images $DIGITAL_OCEAN_TOKEN.
At the end run:
vagrant up --provider=digital_ocean. If you want to resize its memory, you can simply change
We will use
~/.my_staging_secrets.env file to define all secrets that we use
Virtualbox and Digital ocean provision scripts are running as root user.
Please note that
vagrant ssh on VirtualBox use
vagrant user, but on Digital ocean its
Since root access is not recomended on production its better to stick with
Some other advices
for production deployment.
To ssh using deployer user on VirtalBox run
ssh -p 2222 email@example.com
Usually it is good practice to keep all configuration files on git repository so you can
revert back if needed. We will use
config/server folder and place several configuration files
and deployment script there.
Some advices are taken from Digital Ocean
Files: puma.rb, puma-manager.conf, puma.conf and puma-project-list.conf, ngix.conf
sudo start puma-manager # to start all listed projects sudo start/stop/restart puma app=/home/deploy/appname # for managing particular project