diff --git a/data/authors.yml b/data/authors.yml index 35da57b6..85cc3148 100644 --- a/data/authors.yml +++ b/data/authors.yml @@ -3,6 +3,7 @@ authors: toziserikan: Tayfun Öziş ERİKAN baygunm: Murat Kemal BAYGÜN leylakapi: Leyla KAPİ + isoakbudak: İsmail AKBUDAK marjinal1st: Ahmet Sezgin DURAN safiyesepetci: Safiye SEPETCİ hamitturkukaya: Hamit Türkü KAYA diff --git a/source/assets/images/articles/2015-12-21-ruby-on-rails-digital-ocean.png b/source/assets/images/articles/2015-12-21-ruby-on-rails-digital-ocean.png new file mode 100644 index 00000000..22f6685b Binary files /dev/null and b/source/assets/images/articles/2015-12-21-ruby-on-rails-digital-ocean.png differ diff --git a/source/posts/2015-12-21-deploy-ruby-on-rails-application-swiftly-with-capistrano.md b/source/posts/2015-12-21-deploy-ruby-on-rails-application-swiftly-with-capistrano.md new file mode 100644 index 00000000..32f52660 --- /dev/null +++ b/source/posts/2015-12-21-deploy-ruby-on-rails-application-swiftly-with-capistrano.md @@ -0,0 +1,149 @@ +--- +title: Deploy Ruby on Rails Application Swiftly With Capistrano 3 +date: 2015-12-21 +author: isoakbudak +tags: ruby, rails, server, client, cap, capistrano, ssh, bash, script, ruby on rails, capistrano 3, web, cybele, deploy, shell, ubuntu, vps, rbenv, en +--- + +Hi everybody, + +In this post, I will talk about the Capistrano deploy tool on Ruby On Rails and I will share some code blocks that is a bash script for to start the server from zero level. I am using Ubuntu-14.04 server. Capistrano version is 3.4. Ruby version on my server is 2.2.3. Capistrano is a open source project, if you want to look the source code of it you can visit the github page. I am using DigitalOcean for server. The servers that are price $10 are enough to run Ruby On Rails application. You can prepare your server within 2-3 hours with the following bash scripts. Those bash scripts prepare basic environment for ruby libraries and create deploy user for to use on deployment process. Also one of them prepares ruby environment under the deploy(default deploy user name you can change it before to run scripts) user home folder. + +![Digital Ocean](../assets/images/articles/2015-12-21-ruby-on-rails-digital-ocean.png) + +  + +Base installation bash script for ruby environment is as follows. You must run this script as a root user. + + Deploy user creating script is as follows. You must run this script as a root user too. + +  + +This script is preparing ruby environment for deploy user. You should run this script as a deploy user. + + + +  + +Your server is ready to run Ruby On Rails application with those scripts. + +Now I will talk about deployment process for simple application. +Ruby On Rails as you know written with ruby programming language, developing with MVC(Model-View-Controller) open source web framework and it includes principles like DRY(Don't Repeat Yourself), CoC(Convetion over configuration) . + +If you want to create rails application with swiftly, you should look the Cybele ruby gem. This gem provides useful gem list and creates same pages for using in every application like user register, user login, update login and profile info, admin login. Thus you don't repeat yourself on every new project. You can look the template Gemfile of Cybele gem from github account. Some useful deploy gems are on this file. +If you create project rails new project_name command or if you want to deploy project that is already initialized, my deploy commands is in this recipes_matic gem, you should look that. + +I will show deploy steps using with cybele gem. +
    +
  1. Create project $ cybele project_name
  2. +
  3. Edit deploy repo on this file /config/deploy.rb +
     set :repo_url, 'git@github.com:your_username/your_repo_name.git'
    +
  4. +
  5. Edit production deploy settings on this file /config/deploy/production.rb +
    +server "example.com", user: "#{fetch(:local_user)}", roles: %w{app db web}, primary: true, port: 22
    +set :rails_env, 'production'
    +set :branch, 'master'
    +set :project_domain, “example.com”
    +
    +
  6. +
  7. Edit staging deploy settings on this file /config/deploy/staging.rb +
    +server "staging.example.com", user: "#{fetch(:local_user)}", roles: %w{app db web}, primary: true, port: 22
    +set :rails_env, 'staging'
    +set :branch, 'develop'
    +set :project_domain, “staging.example.com”
    +
    +
  8. +
  9. Edit your_email address for to get info about the occurred errors +
    config/environments/production.rb
    config/environments/staging.rb +
    config.middleware.use ExceptionNotification::Rack,
    +:email => {
    +  :email_prefix => "[project_name]",
    +  :sender_address => %{"Notifier" <notifier@project_name.com>},
    +  :exception_recipients => %w{your_email@address.com}
    +}
    +
    +
  10. +
  11. Edit yout SMTP settings on those files
    config/settings/staging.yml
    config/settings/production.yml
  12. +
  13. If you have private repo on github, run this commands in order for to access repo from server
    $ eval `ssh-agent -s`
    $ ssh-add
  14. +
  15. Let's deploy our application with capistrano, +Check production server is ready to deployment +$ bundle exec cap production deploy:check +Setup nginx, postgresql, unicorn, backup(backup gem for to get database backup before deploy) for production server. +$ bundle exec cap production deploy:prepare +Do deploy to production server. +$ bundle exec cap production deploy
  16. +
+  + +Example Capfile file under project root directory + +```rb +# Load DSL and set up stages +require 'capistrano/setup' +# Include default deployment tasks +require 'capistrano/deploy' +require 'capistrano/rails' +require 'capistrano/bundler' +require 'sshkit/sudo' +require 'capistrano/maintenance' +# Include tasks from other gems included in your Gemfile +# For documentation on these, see for example: +# https://github.com/capistrano/rvm +# https://github.com/capistrano/rbenv +# https://github.com/capistrano/chruby +# https://github.com/capistrano/bundler +# https://github.com/capistrano/rails +# https://github.com/capistrano/passenger +# require 'capistrano/rvm' +# require 'capistrano/rbenv' +# require 'capistrano/chruby' +# require 'capistrano/bundler' +# require 'capistrano/rails/assets' +# require 'capistrano/rails/migrations' +# require 'capistrano/passenger' +# Load custom tasks from `lib/capistrano/tasks` if you have any defined +Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r } +``` + + +Example deploy.rb file under project config directory + +```rb +# config valid only for current version of Capistrano +lock '3.4.0' +set :application, 'appname' +set :local_user, 'deploy' +set :stages, %w(staging production) +set :default_stage, 'production' +set :repo_url, "git@github.com:username/#{fetch(:application)}.git" +# Default branch is :master +# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp +# Default deploy_to directory is /var/www/blog2 +set :deploy_to, "/home/#{fetch(:local_user)}/apps/#{fetch(:application)}" +# Default value for :scm is :git +set :scm, :git +# Default value for :format is :pretty +# set :format, :pretty +# Default value for :log_level is :debug +# set :log_level, :debug +# Default value for :pty is false +set :pty, true +# Default value for :linked_files is [] +set :linked_files, fetch(:linked_files, []).push('config/database.yml') +# Default value for linked_dirs is [] +set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/upload', 'public/images', 'public/seat_images') +# Default value for default_env is {} +# set :default_env, { path: "/opt/ruby/bin:$PATH" } +set :default_env, { path: '$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH' } +# Default value for keep_releases is 5 +# set :keep_releases, 5 +# Look our recipes +# https://github.com/lab2023/recipes_matic +load 'config/deploy/recipes/base.rb' +``` + +I hope it is a useful post for you. + +See you next post.. \ No newline at end of file diff --git "a/source/posts/2015-12-21-ruby-on-rails-uygulamasinin-capistrano-3-ile-kolayca-yay\304\261nlanmasi.md" "b/source/posts/2015-12-21-ruby-on-rails-uygulamasinin-capistrano-3-ile-kolayca-yay\304\261nlanmasi.md" new file mode 100644 index 00000000..0f82974b --- /dev/null +++ "b/source/posts/2015-12-21-ruby-on-rails-uygulamasinin-capistrano-3-ile-kolayca-yay\304\261nlanmasi.md" @@ -0,0 +1,149 @@ +--- +title: Ruby on Rails Uygulamasının Capistrano 3 ile Kolayca Yayınlanması +date: 2015-12-21 +author: isoakbudak +tags: ruby, rails, sunucu, server, client, cap, capistrano, ssh, bash, script, ruby on rails, capistrano 3, Web, cybele, shell, ubuntu, vps, rbenv, tr +--- + +Merhaba arkadaşlar, +Sizlere Ubuntu-14.04 sunucusunu sıfırdan ayağa kaldırıp, kendi rails uygulamalarınızı sunucuya hızlı bir şekilde aktarabileceğiniz Capistrano uygulamasından bahsedeceğim ve bazı kaynak kodlar paylaşacağım. Kullandığım capistrano'nun versiyonu 3.4'tür. Sunucu üzerinde kullandığım ruby versiyonu ise 2.2.3'tür. Capistrano'nun kaynak kodlarına github adresinden erişip göz atabilirsiniz. Ben kendi uygulamalarımda sunucu olarak DigitalOcean kullanıyorum. DigitalOcean'dan alacağınız 10 $'lık bir sunucuyu aşşağıda vereceğim bash scriptleri ile 2-3 saat içinde ayağa kaldırıp Nginx, Postgresql, Unicorn ayarlarını yaparak çalışır hale getirebilirsiniz. + +Şimdi izliyeceğimiz adımlar gelecek olursak, öncelikle DigitalOcean'dan yeni bir Droplet oluşturuyoruz ve dağıtım olarak Ubuntu 14.04 seçiyoruz. + +![Digital Ocean](../assets/images/articles/2015-12-21-ruby-on-rails-digital-ocean.png) + +  + +Sunucuda ruby ortamı için temel kurulum scripti aşağıdaki gibidir. Bu script root kullanıcısı olarak bağlanıp çalıştırmalısınız. + + Sunucuda deploy kullanıcısı için kurulum scripti aşağıdaki gibidir. Bu script'ide root kullanıcısı olarak bağlanıp çalıştırmalısınız. + +  + +Sunucuda deploy kullanıcısı için ruby ortamını hazırlama scripti aşağıdaki gibidir. Bu script deploy kullanıcısı olarak bağlanıp çalıştırmalısınız. + + + +  + +Bu scriptler ile sunucunuzu bir rails uygulamasını çalıştıracak duruma getirmiş olursunuz. + +Şimdi bir rails uygulaması oluşturup, uygulamayı bir sunucuya aktarma işlemlerini anlatacağım. +Ruby On Rails bildiğiniz ruby dili ile, MVC(Model-View-Controller) mimari deseni ile geliştirilmiş ve bünyesinde DRY(Don't Repeat Yourself), CoC(Convetion over configuration) gibi yazılım felsefelerini barındıran açık kaynak bir uygulama çatısıdır. + +Hızlı bir Ruby On Rails uygulaması çıkarmak istiyorsanız size Cybele ruby gem'ini öneririm. Bu gem bir uygulamada olması gereken kullanıcı giriş, bilgi güncelleme, yönetici tarafına giriş işlemleri gibi her projede kullanacağınız kısımlar hazır bir taslak olarak geliyor. Bu sayede önceden yazdığınız kodları tekrar yazmak zorunda kalmıyorsunuz. Cybele geminin taslak olarak getirdiği Gemfile'ı github adresinden inceleyebilirsiniz. Sunucuya kolay bir şekilde uygulamayı taşımak için kullancağımız gemler bu Gemfile'da yer almaktadır. +Eğer rails new project_name şeklinde sıfırdan bir proje oluşturursanız veya halihazırda bulunan bir projenizi sunucuya taşımak isterseniz, kullanacağım komutlar dizisinin yer aldığı recipes_matic gemini incelemenizi tavsiye ederim. + +Ben cybele ile oluşturduğumuz bir proje için bu adımları anlataçağım. +
    +
  1. Proje oluştur $ cybele project_name
  2. +
  3. Deploy repo bilgilerini düzenle /config/deploy.rb +
     set :repo_url, 'git@github.com:your_username/your_repo_name.git'
    +
  4. +
  5. Production deploy bilgilerini düzenle /config/deploy/production.rb +
    +server "example.com", user: "#{fetch(:local_user)}", roles: %w{app db web}, primary: true, port: 22
    +set :rails_env, 'production'
    +set :branch, 'master'
    +set :project_domain, “example.com”
    +
    +
  6. +
  7. Staging deploy bilgilerini düzenle /config/deploy/staging.rb +
    +server "staging.example.com", user: "#{fetch(:local_user)}", roles: %w{app db web}, primary: true, port: 22
    +set :rails_env, 'staging'
    +set :branch, 'develop'
    +set :project_domain, “staging.example.com”
    +
    +
  8. +
  9. Hata bildirimleri için email adresini düzenle +
    config/environments/production.rb
    config/environments/staging.rb +
    config.middleware.use ExceptionNotification::Rack,
    +:email => {
    +  :email_prefix => "[project_name]",
    +  :sender_address => %{"Notifier" <notifier@project_name.com>},
    +  :exception_recipients => %w{your_email@address.com}
    +}
    +
    +
  10. +
  11. Aşağıdaki dosyalarda SMTP ayarlarını ayarla
    config/settings/staging.yml
    config/settings/production.yml
  12. +
  13. Github da bulunan private repolara lokaldeki gibi erismek icin şu komutları çalıştır +
    $ eval `ssh-agent -s`
    $ ssh-add
  14. +
  15. Gelelim terminalden çalıştırmamız gereken komutlar dizisine,Production sunucusunda ki deploy ortamının hazır olup olmadığını kontrol eder. +$ bundle exec cap production deploy:check +Production sunucusunda nginx, postgresql, unicorn, backup(veritabanı yedek alma gemi) gibi ayarları yapar. +$ bundle exec cap production deploy:prepare +Production sunucusuna deploy işlemini gerçekleştirir. +$ bundle exec cap production deploy
  16. +
+  + +Örnek dosyalarımızda şu şekilde olmalıdır. +Proje anadizininde bulunan Capfile örneği: + +```rb +# Load DSL and set up stages +require 'capistrano/setup' +# Include default deployment tasks +require 'capistrano/deploy' +require 'capistrano/rails' +require 'capistrano/bundler' +require 'sshkit/sudo' +require 'capistrano/maintenance' +# Include tasks from other gems included in your Gemfile +# For documentation on these, see for example: +# https://github.com/capistrano/rvm +# https://github.com/capistrano/rbenv +# https://github.com/capistrano/chruby +# https://github.com/capistrano/bundler +# https://github.com/capistrano/rails +# https://github.com/capistrano/passenger +# require 'capistrano/rvm' +# require 'capistrano/rbenv' +# require 'capistrano/chruby' +# require 'capistrano/bundler' +# require 'capistrano/rails/assets' +# require 'capistrano/rails/migrations' +# require 'capistrano/passenger' +# Load custom tasks from `lib/capistrano/tasks` if you have any defined +Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r } +``` + +Proje config dizininde bulunan deploy.rb örneği: + +```rb +# config valid only for current version of Capistrano +lock '3.4.0' +set :application, 'appname' +set :local_user, 'deploy' +set :stages, %w(staging production) +set :default_stage, 'production' +set :repo_url, "git@github.com:username/#{fetch(:application)}.git" +# Default branch is :master +# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp +# Default deploy_to directory is /var/www/blog2 +set :deploy_to, "/home/#{fetch(:local_user)}/apps/#{fetch(:application)}" +# Default value for :scm is :git +set :scm, :git +# Default value for :format is :pretty +# set :format, :pretty +# Default value for :log_level is :debug +# set :log_level, :debug +# Default value for :pty is false +set :pty, true +# Default value for :linked_files is [] +set :linked_files, fetch(:linked_files, []).push('config/database.yml') +# Default value for linked_dirs is [] +set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/upload', 'public/images', 'public/seat_images') +# Default value for default_env is {} +# set :default_env, { path: "/opt/ruby/bin:$PATH" } +set :default_env, { path: '$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH' } +# Default value for keep_releases is 5 +# set :keep_releases, 5 +# Look our recipes +# https://github.com/lab2023/recipes_matic +load 'config/deploy/recipes/base.rb' +``` + +Umarım faydalı bir yazı olmuştur. +Kolay gelsin.. \ No newline at end of file