Rails + Webpacker + Puma + Nginx 部署
准备
ssh 登录
首先 ssh 登录服务器,免密码登录可以参考 ssh 免密码登录服务器
创建部署用户
$ sudo adduser deploy
安装依赖
Ruby
这里使用 RVM 安装和管理 Ruby
$ gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
$ curl -sSL https://get.rvm.io | bash
等待安装完成
# 显示可用的 Ruby 版本
$ rvm list known
# 安装
$ rvm install 2.5.3
更换 Gem 源(使用 ruby-china 源)
$ gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
$ gem sources -l
$ bundle config mirror.https://rubygems.org https://gems.ruby-china.com
Node
$ curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
$ sudo apt update
$ sudo apt install -y nodejs
$ node -v
$ npm -v
更换 npm 源
$ npm config set registry http://registry.npm.taobao.org/
$ npm config set disturl https://npm.taobao.org/dist
$ npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass
Yarn
$ curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
$ sudo apt update && sudo apt-get install yarn
更换 Yarn 源
$ yarn config set registry http://registry.npm.taobao.org/
$ yarn config set disturl https://npm.taobao.org/dist
$ yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass
Nginx
Nginx 安装
$ sudo apt install nginx
如果遇到类似的问题:
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
invoke-rc.d: initscript nginx, action "start" failed.
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since 三 2019-03-06 11:50:31 CST; 11ms ago
Process: 32601 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Process: 32596 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
可能是 80 端口已经被占用了,可以编辑 /etc/nginx/sites-enabled/default 更换端口
Nginx 配置
编辑文件 /etc/nginx/sites-enabled/default
我们把 Nginx 作为反向代理,将收到的 Http 请求转发给 Rails 的 Puma 服务器,这里的 Puma 服务器就作为Nginx 的上游(upstream),Nginx 和 Puma 之间通过 Unix 套接字连接,所以首先配置 Nginx 的上游,假设用户为 deploy,项目名为 sample_app,项目位于 deploy 用户主目录
upstream app {
server unix:/home/deploy/sample_app/shared/sockets/puma.sock;
}
然后配置 Nginx 服务器
server {
listen 80;
server_name 192.168.1.2; # 服务器域名或 IP
# public 静态文件
root /home/deploy/sample_app/public;
# 找不到文件发给上游 Rails 应用
try_files $uri/index.html $uri @app;
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
# 改为使用自定义的错误页面
error_page 500 502 503 504 /500.html;
error_page 404 /404.html;
keepalive_timeout 10;
}
PostgreSQL
安装
$ sudo apt update
$ sudo apt-get install postgresql postgresql-contrib libpq-dev
创建数据库账号
# 切换到 postgres 账户
$ sudo su postgres
# 创建数据库用户
$ createuser -s deploy
# 连接数据库 psql 客户端
$ psql
# 修改密码
postgres=# \password deploy
# 退出
postgres=# \q
远程连接配置
pg_hba.conf
$ sudo vi sudo vi /etc/postgresql/9.5/main/pg_hba.conf
找到 # IPv4 local connections:添加:
host all all 10.0.0.1/8(根据需要配置) md5
postgresql.conf
$ sudo vi /etc/postgresql/9.5/main/postgresql.conf
找到 #listen_addresses = 'localhost',改为
listen_addresses = '*'
Rails 配置
Gemfile
gem 'pg'
database.yml
production:
<<: *default
adapter: postgresql
encoding: unicode
database: appname
username: deploy
password: <%= ENV['APPNAME_DATABASE_PASSWORD'] %>
Puma
Puma 配置
编辑项目 config/puma.rb
添加:
# workers 设置为 CPU 核心数
workers 1
threads 1, 6
# 后台运行
daemonize true
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
bind "unix://#{shared_dir}/sockets/puma.sock"
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
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
新建 log,pids,sockets 文件夹
$ mkdir -p shared/log shared/pids shared/sockets
部署项目
上传项目到服务器
$ scp -r /path/to/project deploy@1.2.3.4:~/
# 也可以使用 Git
安装项目依赖
$ bundle install
安装 Webpacker
$ bundle exec rails webpacker:install
配置生产环境密钥
生成密钥
$ RAILS_ENV=production rake secret
设置环境变量
编辑 ~/.bashrc,添加:
export SECRET_KEY_BASE='刚才生成的密钥'
使设置生效
$ source ~/.bashrc
编译 Assets
$ RAILS_ENV=production rake assets:precompile
创建数据库
$ bundle exec rake db:create
数据库迁移
$ RAILS_ENV=production rails db:migrate
启动服务器
$ puma
# 重启
$ pumactl restart
# 停止
$ pumactl stop
Rails + Webpacker + Puma + Nginx 部署的更多相关文章
- 使用passenger在Centos7部署Puma+Nginx+Ruby on Rails
安装ruby环境 RVM(ruby版本管理工具)安装 gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A170311 ...
- CentOS Mono Nginx 部署 MVC4+WebApi
CentOS Mono Nginx 部署 MVC4+WebApi 经过几天的折磨,终于在CentOS上成功部署了MVC4+WebApi.Mono上的服务器推荐两种:Jexus(国产高人写的一款很牛的服 ...
- Ubuntu上通过nginx部署Django笔记
Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式.今天在Ubuntu上使用Nginx部署Django服务,虽然不是第一次搞这个了,但是发现还是跳进了好多坑,g ...
- Ubuntu 14.04 上使用 Nginx 部署 Laravel
本教程将会涉及以下工具: Ubuntu 14.04 LTS PHP 5.5 MySQL Laravel 5.0 Nginx 参考文章:Ubuntu 14.04 上使用 Nginx 部署 Laravel ...
- 使用uWSGI+nginx部署Django项目
最近使用django写了一些项目,不过部署到服务器上碰到一些问题,还有静态文件什么的一堆问题,这里总结一下碰到的问题和解决方案,总体思路是按照官方文档走的. 原文地址:http://uwsgi-doc ...
- FastDFS+Nginx部署详细教程
本例使用到的所有tar和zip包地址:http://download.csdn.net/detail/corey_jk/9758664 本例中使用CentOS1.CentOS2两台机器实现. 1 GC ...
- Centos6.5中Nginx部署基于IP的虚拟…
Centos6.5 中Nginx 部署基于IP 的虚拟主机 王尚2014.11.18 一.介绍虚拟主机 虚拟主机是使用特殊的软硬件技术,把一台真实的物理电脑主机 分割成多个逻辑存储单元,每个单元都没有 ...
- Ubuntu 下使用 Nginx 部署 .NET Core 2.0 网站
前言 本文介绍如何在 Ubuntu 16.04 服务器上安装 .NET Core 2.0 SDK.创建项目与发布,并使用 Nginx 部署 .NET Core 2.0 Web 项目. 安装 .NET ...
- CentOS7上Docker简单安装及nginx部署
安装 如果原来安装过docker,先把原来的删掉,再安装(如果是首次安装docker忽略第一步,直接在第二步看起) 1.1先查看下已经安装了那些docker yum list installed | ...
随机推荐
- vue render函数解析
一.render 函数的作用: 写一些vue.js的template太繁琐,利用render,可以使用js来生成模板,更加灵活和简便. 二.使用render前提: 官网也说了.在深入渲染函数之前推荐阅 ...
- Cesium专栏-淹没分析(附源码下载)
Cesium 是一款面向三维地球和地图的,世界级的JavaScript开源产品.它提供了基于JavaScript语言的开发包,方便用户快速搭建一款零插件的虚拟地球Web应用,并在性能,精度,渲染质量以 ...
- solo升级以及自动化更新的方法
使用solo过程总涉及到更新问题,所以就在这里把solo更新的方法总结一下.希望能给小伙伴们一些帮助.如何选择更新方法主要是跟你的部署方式有关,如果你是通过 docker方式进行部署,那么你可以还可以 ...
- MySQL Online DDL与DML并发阻塞关系总结
MySQL DDL操作执行的三种方式 1,INPLACE,在进行DDL操作时,不影响表的读&写,可以正常执行表上的DML操作,避免与COPY方法相关的磁盘I/O和CPU周期,从而最小化数据库的 ...
- ERROR 1366 (HY000): Incorrect string value: '\xE9\x83\x91\xE5\xB7\x9E' for column 'aa' at row 1 MySQL 字符集
ERROR 1366 (HY000): Incorrect string value: '\xE9\x83\x91\xE5\xB7\x9E' for column 'aa' at row 1创建表之后 ...
- 关于thymeleaf中th:if的使用
运用于判断表达式中时,关系判断使用 gt / ge / eq / lt / le / ne (即:使用缩写) gt: great than(大于)> ge: great equal(大于等于)& ...
- appium---模拟点击事件
在做自动化的过程中都会遇到一些无法定位到的地方,或者通过元素怎么都定位不成功的地方,这个时候我们可以使用必杀技,通过坐标定位.具体的怎么操作呢? swipe点击事件 前面安静写过一篇关于swipe的滑 ...
- 记录python上传文件的坑(2)
描述: 1.之前在写项目mock代码时,碰到一个上传文件的接口,但项目接口本身有token保护机制,碰到token失效时,需要重新获取一次token后,再次对上传文件发起请求,在实际调用中发现,第一次 ...
- Linux中ps -elf和ps aux的区别
一.前言 Linux下输入命令man ps查看: 加横线是 standard syntax -- 比如ps -elf 不加横线是 BSD syntax -- 比如ps aux To see ...
- Java描述设计模式(10):组合模式
本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景 1.文件系统 下图是常见的计算机文件系统的一部分. 文件系统是一个树结构,树上长有节点.树的节点有两种: 树枝节点 即文件夹,有 ...