慕凡(@ryudoawaru)'s blog

目前還沒想到

Install Ruby / Rails / Passenger / PostgreSQL Stack in RHEL and Variant OS (後篇)

| Comments

接續前篇,本篇內容包含:

  • Nginx 設定檔
  • 設定 systemd 啟動檔
  • Logrotate & Iptables 設定

Nginx 設定檔

由於編譯 Nginx 時把 prefix 設定在 /usr/local/nginx 下,所以設定檔就在 /usr/local/nginx/conf 下,先編輯 /usr/local/nginx/conf/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
user  nobody; #這邊的 user 和 Rails App 的 user 是不一樣的設定,但是要考慮到 Asset File 的存取,也就是這個 user 必需要可以唯讀 Rails App 的 public 目錄
worker_processes  4;
error_log  logs/error.log  info;
pid       /var/run/nginx.pid; #設定 pid file 的位置,必需和後面 systemd 設定檔一致
worker_rlimit_nofile 40960;
events {
    worker_connections  10240;
}#worker_rlimit_nofile 和 worker_connections:為了突破系統開啟檔案上限的設定,必需一起設定,一般來說 worker_rlimit_nofile 為 worker_connections x worker_processes

http {
  include       mime.types;
  default_type  application/octet-stream;
  access_log logs/access.log;
  sendfile        on;
  keepalive_timeout  65;
  gzip  on;
  send_timeout 600;
  gzip_min_length  1000;
  gzip_buffers     4 8k;
  gzip_types       text/plain application/x-javascript text/css application/xml text/javascript;
  client_max_body_size 100M; #關係到上傳檔案的上限
  passenger_root /usr/local/ruby23/lib/ruby/gems/2.3.0/gems/passenger-5.1.1; #Passenger Gem 的根目錄,會因為使用的 Ruby 版本與 Passenger 版本變化
  passenger_max_pool_size 16; # 最大同時可以存在的 Rails App 行程數,請依照系統的記憶體大小計算,一般應該是 "(總記憶體 x 0.75) / 單一 Rails App 使用記憶體",例如 4GB 的機器,單一 Rails App 記憶體約 150 就是 ``` 4096 * 0.75 / 150  = 20 ```
  server{
    server_name _;
    location /{
      return 404;
    }
  }# 對應不屬於任何 Virtual Host 的請求,在這裡返回 404 Not Found 的頁面。
  include valid-vhosts/*.conf; #在其它檔案中設定 Virtual Host
}

以下是 Virtual Host 的設定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server{
  server_name 5xruby.tw; #Virtual Host 的主機名稱
  root /home/5xruby/5xruby.tw/current/public; #務必設定為 App 的 Public 目錄
  passenger_enabled on; #開啟 Passenger
  rails_env production; # 設定 Rails App Stage
  passenger_ruby /usr/local/ruby23/bin/ruby; #如果這個 App 和 Passenger 使用的 Ruby 版本不同的話就需要指定
  access_log logs/5xruby-access.log;
  error_log logs/5xruby-err.log;
  location ~ ^/assets/ {
    # 讓瀏覽器快取靜態 Asset Pipeline 檔案的設定
    expires 1y;
    add_header Cache-Control public;
    add_header ETag "";
    break;
  }
}

設定 systemd 啟動檔

  • 編輯 /lib/systemd/system/nginx.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=  /usr/local/nginx/sbin/nginx -t
ExecStart=  /usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 以 root 身份輸入 systemctl enable nginx 設定開機自動啟動
  • 輸入 /usr/local/nginx/sbin/nginx -t 測試設定檔正確與否
  • systemctl start nginx 啟動,可以再透過 systemctl status nginx 觀察啟動情形

Logrotate 設定

如果在前面的 Virtual Host 設定中有設定 access_log 或 error_log 的話,就必需要設定 Logrotate 來定期輪替 Nginx log,輪替的設定如下。

1
2
3
4
5
6
7
8
9
10
11
/usr/local/nginx/logs/*.log {
    missingok
    size=20M #單檔超過 20M 為條件
    compress #壓縮舊檔案
    rotate 10
    notifempty
    sharedscripts
    postrotate
    test ! -f /var/run/nginx.pid || kill -s USR1 `cat /var/run/nginx.pid` #讓 Nginx 重新開啟 Log 檔
    endscript
}

對 Rails 產生的 Log 也可以比照辦理

Iptables 設定

現在的 RHEL 系 OS 通常都預設使用 Firewalld 做為系統防火牆,在此先改回筆者比較熟悉的 Iptables

  • yum remove firewalld
  • yum install iptables-services
  • 編輯 /etc/sysconfig/iptables
1
2
3
4
5
6
7
8
9
10
11
12
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -s 127.0.0.1 -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport ssh -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport http -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport https -j ACCEPT
COMMIT
  • systemctl start iptables

到此為止就完成了基本 Rails Stack 的設定。

Comments