(转)Puma (with Nginx as reverse proxy server)

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 rails as user name and server as group name, also 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 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:

#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;
    }
}

Start nginx service with

# systemctl start nginx

There are several ways to start puma server, two ways are recommended below:
In common create file config/puma.rb, copy codes below and modify as you like:

# 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
Option A: With config file

Start server with

$ bundle exec puma -C config/puma.rb

You can also run it in background with parameter -d and check with

$ ps aux| grep puma

when you want to kill it.
If you want to keep it after you log out, you can use

$ nohup bundle exec puma -C config/puma.rb &

But if the system reboot, the process will still get lost.

Option 2: by systemd

Create a new systemd unit puma.service under ~/.config/systemd/user/ and copy codes below

[Unit]
Description=Puma application server
After=network.target

[Service]
WorkingDirectory=/var/www/my_app
#Environment=RAILS_ENV=production
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 \
     -C /var/www/my_app/config/puma.rb

[Install]
WantedBy=default.target

Hint: For ExecStart, if you've installed gem globally, you can change routes to /usr/local/bin/ in ExecStart.
Then start puma with

$ systemctl --user start puma

To enable puma system-widely: You need to store puma.service in /etc/systemd/system/ and modify it as below:

[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 \
     -C /var/www/my_app/config/puma.rb

[Install]
WantedBy=multi-user.target

For further reading take a look at #References. Also, for easily deploying app in production mode, you can try capistrano. 最后请点击查看原文

你可能感兴趣的:((转)Puma (with Nginx as reverse proxy server))