(轉(zhuǎn))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. 最后請(qǐng)點(diǎn)擊查看原文

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末艰垂,一起剝皮案震驚了整個(gè)濱河市衰猛,隨后出現(xiàn)的幾起案子财忽,更是在濱河造成了極大的恐慌罢艾,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件流椒,死亡現(xiàn)場(chǎng)離奇詭異蓝翰,居然都是意外死亡锣吼,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門杖玲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來顿仇,“玉大人,你說我怎么就攤上這事摆马【饰牛” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵今膊,是天一觀的道長(zhǎng)些阅。 經(jīng)常有香客問我,道長(zhǎng)斑唬,這世上最難降的妖魔是什么市埋? 我笑而不...
    開封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮恕刘,結(jié)果婚禮上缤谎,老公的妹妹穿的比我還像新娘。我一直安慰自己褐着,他們只是感情好坷澡,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著含蓉,像睡著了一般频敛。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上馅扣,一...
    開封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天斟赚,我揣著相機(jī)與錄音,去河邊找鬼差油。 笑死拗军,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蓄喇。 我是一名探鬼主播发侵,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼妆偏!你這毒婦竟也來了刃鳄?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤楼眷,失蹤者是張志新(化名)和其女友劉穎铲汪,沒想到半個(gè)月后熊尉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡掌腰,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年狰住,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片齿梁。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡催植,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出勺择,到底是詐尸還是另有隱情创南,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布省核,位于F島的核電站稿辙,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏气忠。R本人自食惡果不足惜邻储,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望旧噪。 院中可真熱鬧吨娜,春花似錦、人聲如沸淘钟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽米母。三九已至勾扭,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間铁瞒,已是汗流浹背尺借。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留精拟,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓虱歪,卻偏偏與公主長(zhǎng)得像蜂绎,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子笋鄙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,471評(píng)論 5 6
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,495評(píng)論 0 23
  • 昨天吃飯時(shí)师枣,跟兒子談我畫畫的感受,他一語驚醒了我萧落,他說你只需用心畫践美,不必去想畫成什么樣子洗贰,重要的是要開心。他說小時(shí)...
    M有如果閱讀 336評(píng)論 3 1
  • 斷——斷絕不需要的事和物進(jìn)入我們的生活 舍——從生活舍棄多余的事和物 離——脫離對(duì)“擁有”和“恒吃沙”的執(zhí)著 斷舍離...
    alabiubiubiu閱讀 326評(píng)論 0 1