Skip to content

Instantly share code, notes, and snippets.

@ZephiroRB
Last active December 22, 2017 23:50
Show Gist options
  • Save ZephiroRB/826a27fe7ab6f3b9c358a4f9911be796 to your computer and use it in GitHub Desktop.
Save ZephiroRB/826a27fe7ab6f3b9c358a4f9911be796 to your computer and use it in GitHub Desktop.
Deploy Rails + Puma Service Ubuntu 16
# store /etc/nginx/site-enable/app.conf
# systemctl restart nginx
upstream app {
server unix:/var/run/puma_app.sock fail_timeout=0;
}
server {
listen 80;
server_name _;
root /root/app/public;
client_max_body_size 4G;
keepalive_timeout 10;
large_client_header_buffers 4 32k;
access_log off;
error_log off;
add_header X-Powered-By "Carlos Montalvo (http://www.zetanova.com)";
if ($request_method !~ ^(GET|HEAD|PUT|POST|DELETE|OPTIONS)$ ){
return 405;
}
location = /favicon.ico {
expires max;
add_header Cache-Control public;
}
# disable web task
if (-f $document_root/maintenance.html) {
rewrite ^(.*)$ /maintenance.html last;
break;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
error_page 500 504 /500.html;
error_page 502 /502.html;
error_page 503 /503.html;
if (-f $request_filename) {
expires max;
break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://app;
break;
}
}
location /cable {
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location ~ \.php$ {
deny all;
}
}
# store config/puma.rb
# 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") { 3 }.to_i
threads threads_count, threads_count
# 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") { "development" }
app = "app"
app_path = "/var/run"
pidfile "#{app_path}/puma_#{app}.pid"
state_path "#{app_path}/puma_#{app}.state"
stdout_redirect "/root/#{app}/log/puma_#{app}.log", "/root/#{app}/log/puma_err_#{app}.log"
bind "unix:#{app_path}/puma_#{app}.sock"
workers 2
activate_control_app "unix:#{app_path}/pumactl_#{app}.sock"
# 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") { 2 }
# 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!
# 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 `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, 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
#store /lib/systemd/system/puma.service
# systemctl enable puma.service
# systemctl start puma.service
# systemctl status puma.service
[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple
# Preferably configure a non-privileged user
User=root
Group=root
# Specify the path to your puma application root
WorkingDirectory=/root/app
# Helpful for debugging socket activation, etc.
#Environment=PUMA_DEBUG=1
#EnvironmentFile=/var/www/my-website.com/.env
##/root/.nvm/versions/node/v4.8.7/bin/node
# The command to start Puma with RVM
ExecStart=/usr/local/rvm/wrappers/ruby-2.3.5/bundle exec puma -C /root/app/config/puma.rb
Restart=always
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment