使用passenger在Centos7部署Puma+Nginx+Ruby on Rails

安裝ruby環境

RVM(ruby版本管理工具)安裝nginx

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh

列出全部可用的ruby版本centos

rvm list known

安裝一個ruby版本ruby

rvm install 2.2

或者,安裝最新版本bash

rvm install ruby

設置新版本ruby爲默認版本網絡

rvm use 2.2 --default

檢查當前ruby版本app

ruby -v

安裝Nginx

建立nginx源文件curl

# vi /etc/yum.repos.d/nginx.repo工具

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

安裝nginxthis

yum -y install nginx

啓動nginx並設爲自啓動服務url

chkconfig nginx on
service nginx start

安裝Rails和Puma

因爲國內網絡緣由,致使 rubygems.org 存放在 Amazon S3 上面的資源文件間歇性鏈接失敗。

解決辦法:

修改Ruby的gem源地址爲淘寶的源

gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/

檢查Ruby的gem源

gem sources -l

安裝rails

gem install rails

查看rails版本

rails -v

安裝puma

gem install puma

查看puma版本

puma --version

啓動puma

首先,確保你把Puma加入了你的ruby應用的Gemfile文件中。

gem 'puma'

後用Rails命令啓動Puma

rails s Puma

配置Nginx

刪除默認站點配置文件

rm /etc/nginx/conf.d/default.conf

爲你的ruby應用建立一個站點配置文件

# vi /etc/nginx/conf.d/my_app.conf

upstream my_app {
  server unix:///var/run/my_app.sock;
}

server {
  listen 80;
  server_name my_app_url.com; # change to match your URL
  root /var/www/my_app/public; # I assume your app is located at this location

  location / {
    proxy_pass http://my_app; # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location ~* ^/assets/ {
    # Per RFC2616 - 1 year maximum expiry
    expires 1y;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }
}

重啓nginx

service nginx restart
相關文章
相關標籤/搜索