惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
L
LINUX DO - 热门话题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
V
V2EX
GbyAI
GbyAI
量子位
Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
B
Blog
Microsoft Security Blog
Microsoft Security Blog
S
SegmentFault 最新的问题
O
OpenAI News
N
News and Events Feed by Topic
博客园 - Franky
爱范儿
爱范儿
Forbes - Security
Forbes - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V2EX - 技术
V2EX - 技术
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Schneier on Security
Schneier on Security
Cloudbric
Cloudbric
Security Archives - TechRepublic
Security Archives - TechRepublic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Recent Commits to openclaw:main
Recent Commits to openclaw:main
人人都是产品经理
人人都是产品经理
P
Privacy International News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
阮一峰的网络日志
阮一峰的网络日志
D
DataBreaches.Net
Last Week in AI
Last Week in AI
罗磊的独立博客
Spread Privacy
Spread Privacy
Recent Announcements
Recent Announcements
The Cloudflare Blog
Google DeepMind News
Google DeepMind News
AWS News Blog
AWS News Blog
The Register - Security
The Register - Security
Y
Y Combinator Blog
J
Java Code Geeks
I
Intezer

Ruby on Rails

以后发软件库是不是只用发 spec? - V2EX OpenCode + GLM-4.7 还是弟弟, Antigravity + Gemini 3 Pro 还是强不少,不写一行代码转译了一个 Gem: rb-edge-tts - V2EX Ruby on Rails 用着太舒服了 - V2EX 手把手教您在 Baklib 上搭建一个模板网站 - V2EX 我回来了, Ruby on Rails - V2EX Ruby on Rails 国内使用情况 - V2EX 推荐一个 Admin Panel - V2EX 当我更换 mac m2 芯片 无法使用使用 mysql2 报错 symbol not found in flat namespace (_mysql_affected_rows) - V2EX 后端收到两次重复 api 请求,如何判断第二次是重复的? - V2EX Rails 7 发布 - V2EX 大家写 ruby on rails applications 的时候一般怎么写前端页面? - V2EX 如何动态显示和保存一系列输入框中的内容 - V2EX ruby on rails: no implicit conversion of nil into String - V2EX Rails 现在国内是不是有点凉了? - V2EX Rails 6.1 升级记录 - V2EX 使用 Vue 前端、Rails 后端实现图片上传的功能 - V2EX 写了个在线约会的日历,已经部署在公有云上了,正文有访问链接 - V2EX Ruby, Rails 不值一学 - V2EX Rails new 命令一直转圈怎么办? 大家都用什么来部署 ROR 的? - V2EX ruby on rails 做 web 开发,效率真是杠杠的啊 - V2EX 如果有一天 DHH 没有再维护 ror 了怎么办? - V2EX Ruby on Rails 怎么样? - V2EX 世界无人帆船锦标赛,招募两名 Ruby 志愿者的参与 正在看 restful rail development 这本书,按照书上写的测试报错了,大佬帮我看看 - V2EX spree 电商框架怎么样 - V2EX 送几本《Rails 5 敏捷开发》 - V2EX Rails Seed Migration 问题 - V2EX 心好累 ruby 发一个 OAuth1.0 的请求怎么这么难 - V2EX Ruby on Rails 的工程怎么做 heap dump? - V2EX 为什么是 Ruby on Rails?而不是 Rails on Ruby? - V2EX Turbolinks 会导致微信网页在 iOS 上点击链接时出现 invalid signature 错误, Android 和开发者工具则正常 - V2EX ruby on rails 如何上传 swf 文件 - V2EX 球写一段及其简单的 Ruby 代码 - V2EX 初学者请教一个 ruby 使用 leveldb 的问题 - V2EX Rails 本地开发,访问网站巨慢,你们都是怎么解决的? - V2EX RESTful API 设计问题(很基础) - V2EX ruby 中使用类变量是 bad code style 吗?有没有替代品? - V2EX jenkins 执行 ruby -v 和在 slave 上执行 ruby -v 版本不一致 - V2EX Tower,Worktile,Teambition 在低层存储上的差别是什么? - V2EX 良心之作,全站工程师的独白 [视频] - V2EX 有没有人用个人电脑+花生壳+RoR,在家搭建一个自己的服务器? - V2EX 之前已经可以写一些简单的东西了,现在发现都忘完了,有没有类似经历的同学?怎么破? - V2EX 视频推荐:使用 Rails 5 开发一款 Ruby on Rails app - V2EX Ruby on Rails 教程(原书第 4 版)发布 - V2EX 一个美女程序员开发的 Rails App - V2EX 需要长期运营的项目,千万别用 rails - V2EX Rails 出 5.0 了 - V2EX ruby 如何做 race conditions 测试? - V2EX
Rails 布署最佳实践
rina · 2017-12-28 · via Ruby on Rails

准备工作

  • 购买域名:http://name.com/
  • 购买一台云服务器, 提供商: 阿里云,AWS
  • 域名绑定 IP 为服务器 IP 地址

本文介绍的内容为:服务器 ubuntu16.04, Rails 项目,mina 发布

  • 使用 root 账号登录服务器,创建一个没有 root 权限的账号。
adduser newuser

设置新用户密码

配置 ssh

在本地生成 SSH KEY (如果已经有了,可以忽略这步。)

  • 打开终端

  • 用你自己的邮箱地址替换下面的命令,并执行

$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
  • 执行这条命令时,系统会提示你输入要保存密钥的文件位置及安全 passphrase,请按 Enter 键就行了。

生成完之后,在 ~/.ssh/ 目录下会添加两个文件,默认情况下一个私钥 id_rsa, 一个公钥 id_rsa.pub.

配置无密码登录服务器

这一步是为了给 mina 发布提供方便,因为 mina 发布需要无密登录服务器,执行发布命令。

  • 将公钥 id_rsa.pub 文件上传到服务器上, 并添加到 authorized_keys
$ scp ~/.ssh/id_rsa.pub [email protected]:~/.ssh/

$ ssh [email protected]

$ cd ~/.ssh/

$ cat id_rsa.pub >> authorized_keys

$ rm id_rsa.pub

  • 重新开一个终端, 登录服务器,如果无需输入密码登录成功,那配置上面的配置就成功了。
ssh [email protected]

以上是配置 root 用户无密码登录, 同样给 newuser 用户配置成无密码登录。

  • 登录 root 账号,将服务器上的 ssh 配置关掉密码登录
vi /etc/ssh/sshd_config

更新:

PasswordAuthentication no

为了确保能顺利安装 Rails,先要安装 Node.js 和 Yarn 的系统依赖

以下操作使用 root 账号执行

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

sudo apt-get update
sudo apt-get 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 yarn

安装 rbenv

以下操作使用 newuser 账号执行

cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

rbenv install 2.4.3
rbenv global 2.4.3
ruby -v
gem install bundler
rbenv rehash

配置 git 账号

以下操作使用 newuser 账号执行

  • 用你的 github 账号信息,替换下面的用户名和邮箱,并执行
git config --global color.ui true
git config --global user.name "YOUR NAME"
git config --global user.email "[email protected]"
ssh-keygen -t rsa -b 4096 -C "[email protected]"

在服务器上生成了 ssh key 后, 将下面命令输入的内容,复制后粘贴到你 github 账号SSH and GPG keys下:https://github.com/settings/keys

cat ~/.ssh/id_rsa.pub
  • 测试 github 配置是否成功
ssh -T [email protected]

如果成功会输出以下信息:

Hi kfzyutc! You've successfully authenticated, but GitHub does not provide shell access.

安装 Rails

  • 使用 newuser 账号执行以下命令
$ gem install rails -v 5.1.4

$ rbenv rehash

$ rails -v
# Rails 5.1.4

安装 MySQL

  • 使用 root 账号执行以下命令
sudo apt-get install mysql-server mysql-client libmysqlclient-dev

安装 PostgreSQL

  • 使用 root 账号执行以下命令
sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-common
sudo apt-get install postgresql-9.5 libpq-dev

postgres 安装不会为你设置一个用户,所以你需要按照以下步骤创建一个有权创建数据库的用户。 随意用你的用户名替换 chris

sudo -u postgres createuser chris -s

# If you would like to set a password for the user, you can do the following
sudo -u postgres psql
postgres=# \password chris

配置 nginx

  • 使用 root 账号登录服务器

  • home/newuser/xxx, xxx 的目录改成你发布项目的目录,将 example.com 改成你的域名。

upstream RBlog {
  server unix:///home/newuser/xxx/shared/tmp/sockets/puma.sock fail_timeout=0; 
}

server {
  listen 80;
  server_name example.com;
  root /home/newuser/xxx/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  location /cable {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    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_pass http://xxx;
  }

  location ~ ^/(uploads)/  {
    expires max;
    break;
  }


  try_files $uri/index.html $uri @RBlog;
  location @xxx {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_redirect off;
    proxy_pass http://xxx;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 20M;
  keepalive_timeout 10;
}

  • 将以上更换后的配置信息,保存到 /etc/nginx/conf.d/ 目录下,命令为 xxx.conf xxx 用你想要的命名替换。

  • 测试配置信息是否 OK

nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
  • 重启 nginx
nginx -s restart

发布 Rails 项目

如果你是用这个 模板 创建的 Rails 项目, 以下配置信息都已经添加了,只需要更新目录信息就行。

如果没有需要在 Gemfile 里添加这些 gem 包。

gem 'mina', '~>0.3.8', require: false
gem 'mina-puma', '~>0.3.2', require: false
gem 'mina-multistage', '~> 1.0', '>= 1.0.2', require: false
gem 'mina-sidekiq', '~> 0.3.1', require: false
gem 'mina-logs', '>= 0.1.0', require: false
gem 'whenever', :require => false

在你的 Rails 项目下配置 puma,config/puma.rb, 替换 /home/newuser/xxx 目录.

if ENV['RAILS_ENV'] == 'production'
  app_root = "/home/newuser/xxx/shared"
  pidfile "#{app_root}/tmp/pids/puma.pid"
  state_path "#{app_root}/tmp/pids/puma.state"
  bind "unix://#{app_root}/tmp/sockets/puma.sock"
  activate_control_app "unix://#{app_root}/tmp/sockets/pumactl.sock"
  daemonize true
  workers 4
  threads 8, 16
  preload_app!

  on_worker_boot do
    ActiveSupport.on_load(:active_record) do
      ActiveRecord::Base.establish_connection
    end
  end

  before_fork do
    ActiveRecord::Base.connection_pool.disconnect!
  end
else
  plugin :tmp_restart
end

配置 deploy, config/deploy.rb

set :stages, %w(production staging utcprod)
set :default_stage, 'staging'

require 'mina/multistage'
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rbenv'
require 'mina/puma'
require "mina_sidekiq/tasks"
require 'mina/logs'
require 'mina/whenever'

set :shared_paths, ['config/database.yml', 'config/newrelic.yml', 'config/application.yml', 'log', 'public/uploads']

task :environment do
  invoke :'rbenv:load'
end

task :setup => :environment do
  queue! %[mkdir -p "#{deploy_to}/shared/tmp/sockets"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/sockets"]

  queue! %[mkdir -p "#{deploy_to}/shared/pids"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/pids"]

  queue! %[mkdir -p "#{deploy_to}/shared/tmp/pids"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/pids"]

  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/log"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/log"]

  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/public/uploads"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/public/uploads"]

  queue! %[mkdir -p "#{deploy_to}/#{shared_path}/config"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/config"]

  queue! %[touch "#{deploy_to}/#{shared_path}/config/application.yml"]
  queue  %[echo "-----> Be sure to edit '#{deploy_to}/#{shared_path}/config/application.yml'"]

  queue! %[touch "#{deploy_to}/#{shared_path}/config/database.yml"]
  queue  %[echo "-----> Be sure to edit '#{deploy_to}/#{shared_path}/config/database.yml'"]
end

desc "Deploys the current version to the server."
task :deploy => :environment do
  queue  %[echo "-----> Server: #{domain}"]
  queue  %[echo "-----> Path: #{deploy_to}"]
  queue  %[echo "-----> Branch: #{branch}"]

  deploy do
    invoke :'sidekiq:quiet'
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'
    invoke :'deploy:cleanup'

    to :launch do
      invoke :'puma:hard_restart'
      invoke :'sidekiq:restart'
      invoke :'whenever:update'
    end
  end
end

desc "Deploys the current version to the server."
task :first_deploy => :environment do
  queue  %[echo "-----> Server: #{domain}"]
  queue  %[echo "-----> Path: #{deploy_to}"]
  queue  %[echo "-----> Branch: #{branch}"]

  deploy do
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'deploy:cleanup'

    to :launch do
      invoke :'rails:db_create'
    end
  end
end

config/deploy/production.rb,将 domain, deploy_to, repository, branch, user 替换成你自己的信息。

set :domain, 'your.domain.com'
set :deploy_to, '/home/newuser/xxx'
set :repository,  '[email protected]:xxx/xxx.git'
set :branch, 'master'
set :user, 'newuser'
set :puma_config, ->{ "#{deploy_to}/#{current_path}/config/puma/production.rb" }

这些配置信息更新好之后,提交到 github

首次发布

mina setup
mina first_deploy

后续发布

mina deploy

本地查看生产环境日志

mina log

本地登录生产环境数据库控制台

mina console

本地登录服务器

mina ssh

参考文档

Ubuntu 安装 Rails 环境: https://gorails.com/setup/ubuntu/16.04

Mina 配置: https://github.com/80percent/rails-template

原文: http://liuzhen.me/articles/17