rails应用的部署

Posted yxi-liu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rails应用的部署相关的知识,希望对你有一定的参考价值。

简单部署

RAILS_ENV=production rake secret

/etc/profile
export SECRET_KEY_BASE=刚才生成的密钥
source /etc/profile

 

这种简单的把端口暴露出去, 可能出现服务得不到应答的问题,tcp被各种close_wait阻塞。解决的办法:架设nginx

rails 的web服务主要有puma, unicorn, passenger 

 

Puma设置

threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads threads_count, threads_count

environment ENV.fetch("RAILS_ENV") { "prodution" }

daemonize trueworkers 2
application_path = "/home/apphome"
worker_timeout 60
stdout_redirect "#{application_path}/shared/log/puma.stdout.log"
state_path "#{application_path}/shared/tmp/sockets/puma.state"
pidfile "#{application_path}/shared/tmp/pids/puma.pid"
bind "unix://#{application_path}/shared/sockets/app.sock"
activate_control_app "unix://#{application_path}/shared/sockets/pumactl.sock"
# directory "#{application_path}/current"
preload_app!

rails s -e production 就可以启动了

# 通过ps -aux| grep 查看一下是否启动,如果没有启动成功,把daemonzie注释掉,就能看到报错信息。

 

Nginx配置

/etc/nginx/nginx.conf

 

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
                      ‘$status $body_bytes_sent "$http_referer" ‘
                      ‘"$http_user_agent" "$http_x_forwarded_for"‘;

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    
    include /etc/nginx/conf.d/*.conf;
    upstream app {
        server unix://home/上面puma文件中填的bind路径.sock; }

    server {
        listen       4000 default_server;
        server_name  123.207.68.151;
        root         app的根目录;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real_IP $remote_addr;
                proxy_redirect off;
                proxy_pass http://app; # 这里填写upstream的名称
} keepalive_timeout 10; error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }

 

重启nginx: systemctl restart nginx  


以上是关于rails应用的部署的主要内容,如果未能解决你的问题,请参考以下文章

Rail片段缓存如何使您的应用受益,即阻止数据库调用?

在heroku上部署的rails应用程序和html代码出现在我的页脚中

删除 Rails 应用程序并将其重新部署到 heroku

Rails应用部署

部署 Rails 应用程序 aws elastic beanstalk 时出错

使用webpack将Rails应用程序部署到heroku