Option 2: by systemd

这篇是要详细说明一下此前一篇中怎么用 Systemd 启动 Puma,以 2.1.4@in4 为例:
先记录一下最近由于我系统升级导致rvm安装从前版本的Ruby编译报错的解决办法:

$ rvm pkg install openssl
$ rvm install 2.1.4 --with-openssl-dir=$rvm_path/usr

还有貌似开发环境 Solr 根本都不用配置什么东西我发现,直接下述这样项目路径下两句命令就可以用了:

$ rails g sunspot_rails:install
$ bundle exec rake sunspot:solr:start 

正文

# /srv/http/in4/.ruby-version
2.1.4
# /srv/http/in4/.ruby-gemset
in4
$ env | grep '^PATH\|GEM_HOME\|GEM_PATH' > /srv/http/in4/config/puma.conf

此篇的核心就是用上述命令获取到需要的环境变量,以及下述这个单元文件应该怎样写!

# ~/.config/systemd/user/in4.service
[Unit]
Description=Puma application server
After=network.target

[Service]
WorkingDirectory=/srv/http/in4
EnvironmentFile=/srv/http/in4/config/puma.conf
PIDFile=/srv/http/in4/shared/pids/puma.pid
ExecStart=/home/aaron/.rvm/gems/'ruby-2.1.4@in4'/bin/bundle exec \
     /home/aaron/.rvm/gems/'ruby-2.1.4@in4'/bin/puma \
     -C /srv/http/in4/config/puma.rb

[Install]
WantedBy=default.target
$ systemctl --user start in4 # 启动服务
$ systemctl --user status in4 # 查看是否启动成功
● in4.service - Puma application server
   Loaded: loaded (/home/aaron/.config/systemd/user/in4.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2016-03-11 22:15:05 CST; 1s ago
 Main PID: 16259 (ruby)
   CGroup: /user.slice/user-1000.slice/[email protected]/in4.service
           ├─16259 puma 3.0.2 (unix:///srv/http/in4/shared/sockets/puma.sock) [in4]                  
           ├─16264 puma: cluster worker 0: 16259 [in4]                                               
           ├─16268 puma: cluster worker 1: 16259 [in4]                                               
           ├─16272 puma: cluster worker 2: 16259 [in4]                                               
           └─16276 puma: cluster worker 3: 16259 [in4]                                               

Mar 11 22:15:05 rcc systemd[653]: Started Puma application server.
Mar 11 22:15:05 rcc bundle[16259]: [16259] Puma starting in cluster mode...
Mar 11 22:15:05 rcc bundle[16259]: [16259] * Version 3.0.2 (ruby 2.1.4-p265), codename: Plethora of Penguin Pinatas
Mar 11 22:15:05 rcc bundle[16259]: [16259] * Min threads: 1, max threads: 6
Mar 11 22:15:05 rcc bundle[16259]: [16259] * Environment: development
Mar 11 22:15:05 rcc bundle[16259]: [16259] * Process workers: 4
Mar 11 22:15:05 rcc bundle[16259]: [16259] * Phased restart available
Mar 11 22:15:05 rcc bundle[16259]: [16259] * Listening on unix:///srv/http/in4/shared/sockets/puma.sock
Mar 11 22:15:05 rcc bundle[16259]: [16259] Use Ctrl-C to stop

上述显示已成功启动,随后在简单设置一下 nginx。

# /etc/nginx/conf.d/in4.conf
upstream in4 {
    # Path to Puma SOCK file, as defined previously
    server unix:/srv/http/in4/shared/sockets/puma.sock;
    server 0.0.0.0:3002;
}

server {
    listen 80;
    server_name development.in4; # or your server name

    root /srv/http/in4/public;

    try_files $uri/index.html $uri @in4;

    location @in4 {
    proxy_pass http://in4;
    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;
}
# 假设你在 /etc/nginx/nginx.conf 中也有一行 include /etc/nginx/conf.d/*.conf;
# 假设你在 /etc/hosts 中也有一行  127.0.0.1    development.in4

好处是可以直接地址栏打 http://development.in4 访问,也可以脱离 IDE 运行,但是弊端也不是没有,毕竟开发环境时要看 log,没有在 IDE 中运行方便!

你可能感兴趣的:(Option 2: by systemd)