Option 2: by systemd
← Older revision
Revision as of 09:37, 17 August 2015
(3 intermediate revisions by the same user not shown)
Line 188:
Line 188:
}
}
</pre>
</pre>
+
+
=== Puma (with Nginx as reverse proxy server) ===
+
+
[http://puma.io/ Puma] ([https://github.com/puma/puma Github Page]) is a simple, fast, threaded, and highly concurrent HTTP 1.1 server for Ruby/Rack applications, and is considered the replacement for Webrick and Mongrel. It was designed to be the go-to server for Rubinius, but also works well with JRuby and MRI. While reverse proxy server would acts as a load balancer that routes all external requests to a pool of web apps.
+
+
For a webserver it is better to use a server user and group, check [[Users_and_groups#Example_adding_a_user]], below use {{ic|rails}} as user name and {{ic|server}} as group name, also {{ic|my_app}} as rails app name.
+
+
Start by copying your app to /var/www/my_app. And set new ownership with
+
# cd /var/www/
+
# chown -R rails:server my_app
+
+
and permission for user with
+
# chmod -R 775 my_app
+
+
Then add puma gem in the Gemfile and install with
+
$ cd my_app
+
$ bundle install
+
+
Also install {{ic|nginx}} by pacman.
+
+
Under your app folder, create sockets, pid and log folder with
+
$ mkdir -p shared/pids shared/sockets shared/log
+
+
Backup nginx.conf with
+
# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
+
+
Then create a new nginx.conf file with your favorite editor, copy codes below and modify as you like:
+
<pre>
+
#user html;
+
worker_processes 1; # this may connect with the worker numbers puma can use.
+
+
#error_log logs/error.log;
+
#error_log logs/error.log notice;
+
#error_log logs/error.log info;
+
+
#pid logs/nginx.pid;
+
+
+
events {
+
worker_connections 1024;
+
}
+
+
http {
+
upstream app {
+
# Path to Puma SOCK file, as defined previously
+
server unix:/var/www/my_app/shared/sockets/puma.sock;
+
}
+
+
server {
+
listen 80;
+
server_name localhost; # or your server name
+
+
root /var/www/my_app/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;
+
}
+
}</pre>
+
+
Start nginx service with
+
# systemctl start nginx
+
+
+
There are several ways to start puma server, two ways are recommended below:
+
+
==== Option A: With config file ====
+
+
Create file {{ic|config/puma.rb}}, copy codes below and modify as you like:
+
<pre>
+
# Change to match your CPU core count
+
# You can check available worker numbers with $ grep -c processor /proc/cpuinfo
+
# also see the comment in the nginx.conf
+
workers 2
+
+
# Min and Max threads per worker
+
#threads 1, 6
+
+
app_dir = File.expand_path("../..", __FILE__)
+
shared_dir = "#{app_dir}/shared"
+
+
# Default to production
+
#rails_env = ENV['RAILS_ENV'] || "production"
+
#environment rails_env
+
+
# 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</pre>
+
+
and start server with
+
$ bundle exec puma -C config/puma.rb
+
+
You can also run it in background with parameter {{ic|-d}} and check with
+
$ ps aux| grep puma
+
when you want to {{ic|kill}} it.
+
+
==== Option 2: by systemd ====
+
+
Create a new systemd unit {{ic|puma.service}} under /usr/lib/systemd/system/ and copy codes below
+
<pre>
+
[Unit]
+
Description=Puma application server
+
After=network.target
+
+
[Service]
+
WorkingDirectory=/var/www/my_app
+
#Environment=RAILS_ENV=production
+
User=rails
+
PIDFile=/var/www/my_app/shared/pids/puma.pid
+
ExecStart=/home/rails/.gem/ruby/2.2.0/bin/bundle exec \
+
/home/rails/.gem/ruby/2.2.0/bin/puma \
+
-b unix:///var/www/my_app/shared/sockets/puma.sock \
+
--pidfile /var/www/my_app/shared/pids/puma.pid
+
+
[Install]
+
WantedBy=multi-user.target</pre>
+
+
Then start puma with
+
# systemctl start puma
+
+
+
For further reading take a look at [[Ruby_on_Rails#References]]
== Databases ==
== Databases ==
Line 468:
Line 612:
* http://beginrescueend.com/integration/passenger
* http://beginrescueend.com/integration/passenger
* http://blog.phusion.nl/2010/09/21/phusion-passenger-running-multiple-ruby-versions
* http://blog.phusion.nl/2010/09/21/phusion-passenger-running-multiple-ruby-versions
+
* http://www.ruby-journal.com/how-to-setup-rails-app-with-puma-and-nginx/
+
* https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04
== See also ==
== See also ==