Ruby on Rails - 使用puma部署Rails项目

1 篇文章 0 订阅

Puma是一个基于Ruby的Web Server服务器,主要基于多线程,所以内存占用较少
使用Puma方法如下:

1. 在Gemfile中添加:
gem 'puma'
2. 配置config/puma.rb文件,当前具体配置如下

其中端口以及sock名字需要对应更改

module Rails
  class << self
    def root
      File.expand_path("../..", __FILE__)
    end
  end
end
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
# threads threads_count, threads_count

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port        ENV.fetch("PORT") { 3040 }

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
rails_env = ENV.fetch("RAILS_ENV") || "development"

# 计算机核数
if rails_env == "development"
  workers ENV.fetch("WEB_CONCURRENCY") { 2 }
else
  workers 2
end

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory. If you use this option
# you need to make sure to reconnect any threads in the `on_worker_boot`
# block.
#
pidfile "#{Rails.root}/tmp/pids/puma.pid"
state_path "#{Rails.root}/tmp/pids/puma.state"
stdout_redirect "#{Rails.root}/log/puma.stdout.log", "#{Rails.root}/log/puma.stderr.log", true
bind "unix://#{Rails.root}/tmp/navy_monitor.sock"
daemonize
threads 0,4
preload_app!

# If you are preloading your application and using Active Record, it's
# recommended that you close any connections to the database before workers
# are forked to prevent connection leakage.
#
# before_fork do
#   ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
# end

# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted, this block will be run. If you are using the `preload_app!`
# option, you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, as Ruby
# cannot share connections between processes.
#
# on_worker_boot do
#   ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
# end
#
# on_worker_boot do
#   ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
# end

before_fork do
  ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
  # PumaWorkerKiller.config do |config|
  #   config.ram           = 16*1024 # mb  服务器内存
  #  # config.frequency     = 5    # seconds
  #   config.percent_usage = 0.7 # 内存使用上限
  #   config.rolling_restart_frequency = 12 * 3600 # 12 hours in seconds
  # end
  # PumaWorkerKiller.start
end
# Allow puma to be restarted by `rails restart` command.
# plugin :tmp_restart

3. 添加start.sh以及restart.sh

start.sh文件

rails s puma -e development -b 0.0.0.0 -p 8088

restart.sh文件

kill -s USR2 `cat ./tmp/pids/puma.pid`

保存以后即可直接运行

4. 执行启动以及重启操作

如需启动项目,执行./start.sh即可

./start.sh

在执行操作后,提示以下错误:
bash: ./start.sh: Permission denied
对当前文件进行赋值即可

chmod 777 *.sh

接下来启动项目成功
在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从心、Sunshine

赞助点植发的money,球球了

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值