1
votes

Hi i followed this thread of digitalocean to deploy my RubyOnRails app on digitalocean vps https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04

My Config: 512 MB RAM(with 1 gb swap) ubuntu 16.04 Ruby2.33(with rbenv)

This tutorial lists usage of upstart but as i searched i found ubuntu 16.04 uses systemd. I found this thread but still could start Puma Server https://github.com/puma/puma/issues/1211

when i run which puma it gives /home/ashish/.rbenv/shims/puma

Also here is my puma.service file

[Unit]
Description=Puma HTTP Server
After=network.target

# Uncomment for socket activation (see below)
# Requires=puma.socket

[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple

# Preferably configure a non-privileged user
User=www-data

# The path to the puma application root
# Also replace the "<WD>" place holders below with this path.
WorkingDirectory=/var/www/mystore.rentcallcenter.com

# Helpful for debugging socket activation, etc.
Environment=PUMA_DEBUG=1

# The command to start Puma. This variant uses a binstub generated via
# `bundle binstubs puma --path ./sbin` in the WorkingDirectory
# (replace "<WD>" below)
ExecStart=/home/ashish/.rbenv/shims/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem

# Variant: Use config file with `bind` directives instead:
# ExecStart=<WD>/sbin/puma -C config.rb
# Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub

Restart=always

[Install]
WantedBy=multi-user.target

and here is my puma.rb in rails config folder

# 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 1, 6

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

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

# 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).
#
workers ENV.fetch("WEB_CONCURRENCY") { 1 }



# 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.
#
# 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
#

# Allow puma to be restarted by `rails restart` command.
#plugin :tmp_restart

app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"

# Set up socket location
bind "unix://#{shared_dir}/sockets/puma.sock"

# Logging
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true

# Set master PID and state locations
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/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

I want to start using systemd since its preferred for ubuntu 16.04

I Managed To start Puma but

hi i managed to make puma server run. but now i am getting nginx upstream connection timed out. here is out put from systemctl status puma

/system.slice/puma.service
       ├─ 1258 puma: cluster worker 0: 15954 [mystore2.rentcallcenter.com]
       └─15954 puma 3.10.0 (unix:///var/www/mystore2.rentcallcenter.com/shared/sockets/puma.sock)

here is my nginx server block file

    upstream app {
    # Path to Puma SOCK file, as defined previously
    server unix:///var/www/mystore2.rentcallcenter.com/shared/sockets/puma.sock fail_timeout=0;
}

server {
    listen 80;
    listen [::]:80;
    server_name mystore2.rentcallcenter.com;

    root /var/www/mystore2.rentcallcenter.com/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}`
1
First of all you have to investigate systemd logs to watch what happened. Obvious issue: you have no -C config/<puma_env_cfg>.rbPavel Mikhailyuk
yes it was the issue. now i added config/puma.rb. but now he nginx server has connection timed out issueashishcompwiz
It seems like you don't understand how it works :) You have to read some docs/articles. Puma needs no serving ssl or even tcp binding. If you have nginx(front web server) + puma(app server) setup, you should configure Puma to listen unix socket(or standard 3000 tcp port) and then configure nginx to proxy pass requests to that socket or 3000tcp. SSL is also configured in nginxPavel Mikhailyuk
hi i managed to make puma server run. but now i am getting nginx upstream connection timed out. here is out put from systemctl status puma /system.slice/puma.service ├─ 1258 puma: cluster worker 0: 15954 [mystore2.rentcallcenter.com] └─15954 puma 3.10.0 (unix:///var/www/mystore2.rentcallcenter.com/shared/sockets/puma.sock) [mysashishcompwiz
Configure your Puma startup(ExecStart) to write Puma log somewhere. Then watch Puma log and Rails log for issues. If needed, reconfigure Puma back from socket to tcp port and use curl to run http requests to your app locally.Pavel Mikhailyuk

1 Answers

1
votes

i managed to connect to make my rails app run with puma and nginx final config

nginx server block file
 upstream app {
    # Path to Puma SOCK file, as defined previously
    server unix:///var/www/mystore2.rentcallcenter.com/shared/sockets/puma.sock fail_timeout=0;
}

server {
    listen 80;
    listen [::]:80;
    server_name mystore2.rentcallcenter.com;

    root /var/www/mystore2.rentcallcenter.com/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

and puma.service

[Unit]
Description=Puma HTTP Server
After=network.target

# Uncomment for socket activation (see below)
# Requires=puma.socket

[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple

# Preferably configure a non-privileged user
User=www-data

# The path to the puma application root
# Also replace the "<WD>" place holders below with this path.
WorkingDirectory=/var/www/mystore2.rentcallcenter.com

# Helpful for debugging socket activation, etc.
# Environment=PUMA_DEBUG=1

# The command to start Puma. This variant uses a binstub generated via
# `bundle binstubs puma --path ./sbin` in the WorkingDirectory
# (replace "<WD>" below)
# ExecStart=/home/ashish/.rbenv/shims/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem

# Variant: Use config file with `bind` directives instead:
ExecStart= /home/ashish/.rbenv/shims/puma -C /var/www/mystore2.rentcallcenter.com/config/puma.rb
# Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub

Restart=always

[Install]
WantedBy=multi-user.target