Create the digital ocean droplet with the ssh keys you want to be able to ssh as
adduser deployer
And set password for sudo permissions
gpasswd -a deployer sudo
Make a /var/www/ directory and allow the deployer user to own it
mkdir -p "/var/www/ptpal.io" && chown -R deployer "/var/www/ptpal.io"
Copy the ~/.ssh/authorized_keys from the root user to the deployer users ~/.ssh/authorized_keys file
As the root user
cat ~/.ssh/authorized_keys
copy the output
Switch to deployer user
mkdir ~/.ssh
touch ~/.ssh/authorized_keys
vi ~/.ssh/authorized_keys and paste in what was in the root users
Add Nginx while you are the root user too
sudo apt-get install nginx
sudo apt-get -y install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev nodejs
cd
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
Install ruby, wait 5 hours, then set the global ruby version
rbenv install 2.4.1
rbenv global 2.4.1
Verify ruby version with ruby -v
Dont install documentation for ruby gems by default
echo "gem: --no-document" > ~/.gemrc
Install Bundler and Rails
gem install bundler
gem install rails
- Make sure you are in the right rbenv ruby
Set up git with new public key and make sure you can authenticate
ssh-keygen -t rsa -b 4096 -C "samuel.parry@bigpond.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
Now you need to add the public key to Github
cat ~/.ssh/id_rsa.pub
and then copy the output and add it to your github profile under settings > add gpg or whatever the shit
Check that it works by running:
ssh -T git@github.com
sudo fallocate -l 4G /swapfile
ls -lh /swapfile
sudo chmod 600 /swapfile
ls -lh /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon -s
Make the swap file persist
sudo vi /etc/fstab
Add the following to the file
/swapfile none swap sw 0 0
Change the swapiness value and cache pressure value
sudo vi /etc/sysctl.conf
Add the following to the bottom of the file:
vm.swappiness=10
vm.vfs_cache_pressure = 50
Should now be right to set up a mina script with the user deployer deploying to /var/www/sitename.com
Skip to the section about setting up mina once you have got a database up and running
Set up postgres box (https://www.digitalocean.com/community/tutorials/scaling-ruby-on-rails-setting-up-a-dedicated-postgresql-server-part-3)
ssh into the postgres box as root
aptitude update
aptitude -y upgrade
aptitude install -y build-essential
aptitude install -y git-core
Need to add the postgres repo to the apt-get source lists
vim /etc/apt/sources.list.d/pgdg.list
And paste in:
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main
Then update and upgrate the system
aptitude update
aptitude -y upgrade
Install Postgres:
aptitude install -y postgresql-9.3 pgadmin3
Create roles for postgres:
Switch to postgres user and enter postgres:
sudo -u postgres psql postgres
Set a password for the postgres user
\password postgres
Exit postgres after you have set your password:
\q
Creating roles:
sudo -u postgres psql
CREATE USER ptpal_admin WITH PASSWORD 'pwd';
CREATE DATABASE ptpal_production OWNER ptpal_admin;
Enabling remote connections
vim /etc/postgresql/9.3/main/postgresql.conf
Change Line:
#listen_addresses = 'localhost' -> listen_addresses = '*'
vim /etc/postgresql/9.3/main/pg_hba.conf
Insert:
# TYPE DATABASE USER ADDRESS METHOD
host all all 0.0.0.0/0 md5
under the section:
# Put your actual configuration here
# ..
Restart Postgres:
service postgresql restart
Install MySQL
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
sudo mysql_install_db
sudo mysql_secure_installation
It is also probably worth creating a mysql user instead of using the root user
mysql -uroot -p[PASSWORD_YOU_SET]
CREATE USER 'payd'@'localhost' IDENTIFIED BY '[A_PASSWORD]';
^^^ store that somewhere
GRANT ALL PRIVILEGES ON * . * TO 'payd'@'localhost';
FLUSH PRIVILEGES;
Create the database while you are here too
CREATE DATABASE onboarder_production
Update your production config/database.yml
Create add a secret key base to the rails app
Run locally:
rake secret
Copy output and add it to /var/www/site.com/shared/secrets.yml like:
production:
secret_key_base: <your-key>
Add this to the /var/www/site.com/shared/database.yml
production:
adapter: postgresql
encoding: utf8
database: ptpal_production
username: ptpal_admin
password: pwd
host: 128.199.215.39
port: 5432
pool: 10
Using Mina:
gem install mina
mina init
ssh deployer@yoururlorip
mkdir /var/www/payd.io
chown -R deployer /var/www/payd.io
exit out of ssh tunnel
mina setup
mina deploy
Fix whats broken
- MAKE SURE TO SET TERM_MODE TO NIL (this just saves you headaches)
set :term_mode, nil
- Be sure to set the correct user to ssh as
- Uncomment the
mina/rbenv - Uncomment the
:'rbenv:load'in the:environmenttask - Ensure that
bundleris installed - Ensure that the database user is set up and has correct permissions
- Ensure the database exists
- Ensure the user is correct (deployer)
- Ensure the github repo url is in the git://github.com:smlparry format
Do this through mina cause its awesome
be sure to install both the mina gems
gem 'mina-nginx', :require => false
gem 'mina-unicorn', :require => false
Make sure you do require: false it doesn't work without it
Also require them in your config/deploy.rb
require 'mina/nginx'
require 'mina/unicorn'
Oh and make sure to bundle ey
The mina-nginx and mina-unicorn gems require a couple of variables to be present so be sure to set that in config/deploy.rb
set :application, 'ptpal'
set :app_path, "#{deploy_to}/#{current_path}"
set :rails_env, 'production'
Now we want to get an Nginx config on the server
vi /var/www/payd.io/shared/config/nginx.conf
Paste in below (changing ptpal to whatever your app is called)
upstream unicorn {
server unix:/tmp/unicorn.ptpal.sock fail_timeout=0;
}
server {
server_name www.ptpal.io;
return 301 $scheme://ptpal.io$request_uri;
}
server {
listen 80 default deferred;
server_name ptpal.io;
root /var/www/ptpal.io/current/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Now we start using Mina
First cab off the rank
mina nginx:link
Assuming your config/deploy.rb is set up properly this should create a symlink from shared/config/nginx.conf to /etc/nginx/sites-enabled/#{application}.conf. Check that this is right.
Then you will need to run (i think)
mina nginx:restart
You may have to remove /etc/nginx/sites-enabled/default and /etc/nginx/sites-available/default for your site to show up when you hit the ip
I think that is all for nginx (check that it is running by visiting the IP, you should see nginx)
Create a config/unicorn.rb locally
Make sure that is deployed
It should also look something like (change app name again):
root = "/var/www/ptpal.io/current"
working_directory root
pid "/var/www/ptpal.io/shared/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"
listen "/tmp/unicorn.ptpal.sock"
worker_processes 2
timeout 30
config/unicorn.rb needs to be commited and pushed for unicorn to work
You will also have to create the directories /var/www/payd.io/shared/tmp/pids and add tmp/pids to the shared paths in confg/deploy :
set :shared_paths, ['config/database.yml', 'config/secrets.yml', 'log', 'tmp/pids']
You will need to manually create the shared pids folder too
mkdir /var/www/ptpal.io/shared/pids
Also I added some unicorn code to the config/deploy.rb which probs overwrites the mina-unicorn gem stuff but i dont care it works and looks good. It goes a little something like this:
namespace :unicorn do
set :unicorn_pid, "#{app_path}/tmp/pids/unicorn.pid"
set :start_unicorn, %{
cd #{app_path}
bundle exec unicorn -c #{app_path}/config/unicorn/#{rails_env}.rb -E #{rails_env} -D
}
desc "Start unicorn"
task :start => :environment do
queue 'echo "-----> Start Unicorn"'
queue! start_unicorn
end
desc "Stop unicorn"
task :stop do
queue 'echo "-----> Stop Unicorn"'
queue! %{
test -s "#{unicorn_pid}" && kill -QUIT `cat "#{unicorn_pid}"` && echo "Stop Ok" && exit 0
echo >&2 "Not running"
}
end
desc "Restart unicorn using 'upgrade'"
task :restart => :environment do
invoke 'unicorn:stop'
invoke 'unicorn:start'
end
end
Ooh and add
invoke :'unicorn:restart'
to the
to :launch do
...
end
block in the config/deploy.rb script
Also if you have the same config as I you may need to create a log dir in your rails app on the server so unicorn can log. Also i think starting unicorn doesnt work if you dont have that
Then I think you should be good to run
mina unicorn:start
and all those other cool commands