Skip to content

Commit

Permalink
Committing changes I had to make to get puma working on production.
Browse files Browse the repository at this point in the history
  • Loading branch information
pellaea committed Feb 25, 2024
1 parent 219b2e3 commit 644f312
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 34 deletions.
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ gem("jbuilder")
# Use ActiveModel has_secure_password
gem("bcrypt", "~> 3.1.7")

# Use unicorn as the app server
# gem("unicorn")
# Use puma as the app server
gem("puma")

Expand Down
2 changes: 1 addition & 1 deletion config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
# personally identifiable information (PII). If you want to log everything,
# set the level to "debug".
# config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info")
config.log_level = :debug
config.log_level = :info

# Disable automatic flushing of the log to improve performance.
# config.autoflush_log = false
Expand Down
5 changes: 1 addition & 4 deletions config/etc/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,14 @@ http {
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;

# enable this if you forward HTTPS traffic to unicorn
# proxy_set_header X-Forwarded-Proto $scheme;

# we don't want nginx trying to mess with redirects
proxy_redirect off;

# enforce limit on rate of requests for all requests processed by rails
limit_req zone=one burst=10;

# this is where unicorn is listening
# proxy_pass http://unicorn;
proxy_pass http://puma;

# track progress of all POST requests at this location, MUST BE LAST!
# track_uploads upload 10s;
Expand Down
15 changes: 7 additions & 8 deletions config/etc/puma.service
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ After=network.target
# monitoring, as of Puma 5.1 or later.
# On earlier versions of Puma or JRuby, change this to `Type=simple` and remove
# the `WatchdogSec` line.
Type=notify
# Type=notify
Type=simple

# If your Puma process locks up, systemd's watchdog will restart it within seconds.
WatchdogSec=10
# WatchdogSec=10

# Preferably configure a non-privileged user
# User=
User=mo

# The path to your application code root directory.
# Also replace the "<YOUR_APP_PATH>" placeholders below with this path.
# Example /home/username/myapp
WorkingDirectory=/var/web/mo

PIDFile=/var/web/mo/puma.pid
PIDFile=/var/web/mo/tmp/puma.pid
Environment=RAILS_ENV=production
# Helpful for debugging socket activation, etc.
# Environment=PUMA_DEBUG=1
Environment=PUMA_DEBUG=1

# SystemD will not run puma even if it is in your path. You must specify
# an absolute URL to puma. For example /usr/local/bin/puma
Expand All @@ -40,9 +41,7 @@ Environment=RAILS_ENV=production
# Variant: Specify directives inline.
# ExecStart=/<FULLPATH>/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem

# Working example for Rails from the web:
ExecStart=/var/web/mo/bin/bundle exec puma -e production -C ./config/puma.rb config.ru
# For switching to Puma with a rolling restart, replace <USR1> with a different user than the one running Unicorn.
ExecStart=/var/web/mo/bin/puma
ExecReload=/bin/kill -s $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID

Expand Down
34 changes: 16 additions & 18 deletions config/puma.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
# frozen_string_literal: true

workers Integer(ENV["WEB_CONCURRENCY"] || 3)
threads_count = Integer(ENV["MAX_THREADS"] || 1)
threads threads_count, threads_count
workers 3
threads 1, 1

# preload_app!
prune_bundler
app_path = "/var/web/mushroom-observer"
rails_env = "production"

rackup DefaultRackup if defined?(DefaultRackup)
port ENV["PORT"] || 3000
environment ENV["RAILS_ENV"] || "production"

# directory "/var/web/mo"
# redirect_stderr "/var/web/mo/log/puma.stderr.log"
# redirect_stdout "/var/web/mo/log/puma.stdout.log"
# bind "/var/web/mo/tmp/sockets/puma.sock"
# pidfile "/var/web/mo/tmp/pids/puma.pid"

# on_worker_boot do
# ActiveRecord::Base.establish_connection
# end
environment rails_env
bind "unix://#{app_path}/tmp/sockets/puma.sock"
stdout_redirect "#{app_path}/log/puma.stdout.log",
"#{app_path}/log/puma.stderr.log", true
pidfile "#{app_path}/tmp/pids/puma.pid"
state_path "#{app_path}/tmp/pids/puma.state"
activate_control_app

on_worker_boot do
require "active_record"
ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env])
end
2 changes: 1 addition & 1 deletion script/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ echo Installing bundle... && bundle install && \
echo Checking for migrations... && rake db:migrate && \
echo Updating translations... && rake lang:update && \
echo Precompiling assets... && rake assets:precompile && \
# echo Reloading unicorn... && /etc/init.d/unicorn reload && \
echo Reloading puma... && sudo system puma restart && \
echo Tagging repo with $tag... && git tag $tag && \
echo Pushing new tag... && git push --tags && \
echo SUCCESS\!

0 comments on commit 644f312

Please sign in to comment.