blog's Introduction
blog's People
blog's Issues
Cấu hình deploy một dự án Rails với capistrano, unicorn và nginx
Deploy
là quá trình đưa source code bạn lên server sau khi bạn đã phát triển xong và chuẩn bị giao cho khách hàng. Đây là một công đoạn có phải nói các developer nào cũng phát biết trong con đường sự ngiệp của mình.
Tuy nhiên công việc deploy không phải lúc nào cũng diễn ra xuôi sẻ và không phải lúc nào cũng giống nhau. Nó cũng phụ thuộc vào nhiều yếu tố khác nhau
Bài viết này mình sẽ hưỡng dẫn cấu hình để deploy một dự án Rails dùng capistrano, unicorn và nginx lên server Ubuntu 14.04.
Trước khi bắt đầu, hãy chắc chắn là server của bạn đã cài đặt một số thứ như sau:
Ruby
Git
Nginx
Một dự án ROR ở local
Chiến thôi !!
1.Add thêm một số gem
vào Gemfile và chạy bundle íntall
#Gemfile
gem 'capistrano'
gem 'capistrano-rails'
gem 'capistrano-bundler'
gem 'capistrano-unicorn-nginx'
gem 'unicorn'
gem 'capistrano-rvm'
2.Sau khi đã cài một số gem
xong tiếp tục tạo ra những file config capistrano chạy lệnh sau:
$ bundle exec cap install
Lệnh trên sẽ tạo cấu trúc file như sau.
├── Capfile
├── config
│ ├── deploy
│ │ ├── production.rb
│ │ └── staging.rb
│ └── deploy.rb
└── lib
└── capistrano
└── tasks
3. Require thêm một số gói vào Capfile
và Capfile
của bạn sẽ trông như thế này.
#Capfile
# Load DSL and set up stages
require 'capistrano/setup'
# Include default deployment tasks
require 'capistrano/deploy'
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/unicorn_nginx'
# Load custom tasks from `lib/capistrano/tasks' if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
4.Tiếp theo cấu hình cho deploy.rb
.
#config/deploy.rb
#Tên ứng dụng của bạn ở đây mình đặt tên Appdeploy
set :application, 'Appdeploy'
#Repository của bạn
set :repo_url, 'https://github.com/Nguyenanh/Blog.git'
#Đường dãn chứa source trên server sau khi deploy
set :deploy_to, '/home/anhn/Appdeploy'
set :scm, :git
5.Vậy là ta thiết lập những cấu hình chung để deploy
. Giờ tùy vào từng môi trường ta sẽ thiết lập riêng. Bài viết này mình sẽ thiết lập trên môi trường production
. Mở file production.rb
.
#config/deploy/production.rb
#Set user và và server_name của bạn
set :user, 'anhn'
set :server_name, '1.2.3.4'
#Set branch trên Git mà bạn muốn deploy
set :branch, 'master'
#Set môi trường, ở đây mình thiết lâp cho môi trường production
set :rails_env, 'production'
set :bundle_flags, "--no-deployment"
role :app, ["#{fetch(deploy_user)}@#{fetch(server_name)}"]
role :web, ["#{fetch(deploy_user)}@#{fetch(server_name)}"]
role :db, ["#{fetch(deploy_user)}@#{fetch(server_name)}"]
server fetch(server_name), user: fetch(deploy_user), roles: %w{web app db}, primary: true
Trường hợp server của bạn bắt authentication bằng ssh
thì bạn thêm tùy chọn ssh_option
#config/deploy/production.rb
#home/anhn/.ssh/appdeploy.pem là đường dẫn mà mình dùng `.pem` để xác thực
set :ssh_options, {
keys: %w(/home/anhn/.ssh/appdeploy.pem),
forward_agent: false,
}
6.Vậy là xong quá trình config cho quá trình deploy
. Việc cuối cùng là kêu thằng capistrano đưa source lên server.
$ bundle exec cap setup
Lệnh này sẽ bảo cap
khỏi tạo cấu trúc đường dẫn cũng như file config nginx
cho quá trình deploy.
$ bundle exec cap production deploy
Tiếp theo kêu cap deploy code lên server với môi trường production. Việc còn lại là ngồi rung đùi. Khi quay trở lại quá trình deploy hoàn tất.
Kết luận
Bài viết này xin nhấn mạnh thiết lập các cấu hình để deploy một dự án Rails lên server. Và đây chỉ là single server. Ở những bài viết sau mình sẽ hướng dẫn deploy trên HDH CentOS hoặc deploy với cấu trúc server NAT.
Lý do không tạo được record khi khai báo belongs_to trong Rails 5
Trong Rails 5. Bất cứ khi nào chúng ta định nghĩa một quan hệ belongs_to
. Thì nó bắt buộc chúng ta cung cấp một đối tượng quan hệ với nó.
Rails sẽ tiến hành validation lỗi nếu đối tượng quan hệ không tồn tại
class User < ApplicationRecord
end
class Post < ApplicationRecord
belongs_to :user
end
post = Post.create(title: 'Hi')
=> <Post id: nil, title: "Hi", user_id: nil, created_at: nil, updated_at: nil>
post.errors.full_messages.to_sentence
=> "User must exist"
Như chúng ta có thể thấy, chúng ta không thể tạo bất kỳ một post
nào nếu không có một quan hệ user
nào
Làm thế nào để giải quyết được điều này trong Rails 5
Trong Rails 4.x để thêm validation cho belongs_to
chúng ta chỉ cần thêm tùy chọn required: true
.
class User < ApplicationRecord
end
class Post < ApplicationRecord
belongs_to :user, required: true
end
post = Post.create(title: 'Hi')
=> <Post id: nil, title: "Hi", user_id: nil, created_at: nil, updated_at: nil>
post.errors.full_messages.to_sentence
=> "User must exist"
Mặc định trong Rails 4.x tùy chọn required
là false
.
Để bỏ qua validation belongs_to
trong Rails 5. Chúng ta chỉ cần thêm tùy chọn optional: true
.
class Post < ApplicationRecord
belongs_to :user, optional: true
end
post = Post.create(title: 'Hi')
=> <Post id: 2, title: "Hi", user_id: nil>
Với thêm tùy chọn ở trên thì validation sẽ bỏ qua với class Post
và tất cả model còn lại sẽ vẫn bị validation.
Bỏ qua validation cho toàn bộ model trong Rails 5.
Mặc định Rails 5 setting validation cho toàn bộ model
Rails.application.config.active_record.belongs_to_required_by_default = true.
Để bỏ validation chúng ta cần seting lại giá trị sang false
.
Rails.application.config.active_record.belongs_to_required_by_default = false
class Post < ApplicationRecord
belongs_to :user
end
post = Post.create(title: 'Hi')
=> <Post id: 3, title: "Hi", user_id: nil, created_at: "2016-02-11 12:36:05", updated_at: "2016-02-11 12:36:05">
Nguồn: http://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.