Ruby nginx,mina 与 puma 实现 rails 项目自动化部署,如何实现部署后 puma 自动启动。

fengfans · 2023年10月29日 · 最后由 xifengzhu 回复于 2023年10月30日 · 293 次阅读

目前部署到 ubuntu 正常,本地开发主机 macos 执行 rails s 正常,但是部署到 ubuntu 后,puma 就不启动。即使在/var/www/html/current/目录下执行 bundle exec puma -C /var/www/html/current/config/puma.rb命令,通过访问rails项目也打不开默认页面,提示超时访问。

cat nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    upstream puma{
        server unix:/var/www/html/shared/tmp/sockets/puma.sock fail_timeout=0;
    }
    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
#
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}
cat default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html/current/public;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://unix:/var/www/html/shared/tmp/sockets/puma.sock;

    }
    try_files $uri/index.html $uri @puma;

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
    #   fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}

cat puma.rb
environment "production"

app_name = "cool"
application_path = "/var/www/html/"
directory "/var/www/html/current"

workers 2
threads 1,4



pidfile "/var/www/html/shared/tmp/pids/puma.pid"
state_path "/var/www/html/shared/tmp/sockets/puma.state"
stdout_redirect "/var/www/html/shared/log/puma.stdout.log", "/var/www/html/shared/log/puma.stderr.log"
bind "unix:/var/www/html/shared/tmp/sockets/puma.sock"
activate_control_app 'unix:/var/www/html/shared/tmp/sockets/pumactl.sock'


on_restart do
  puts 'On restart....'
end
preload_app!

我使用的 ruby 3.0.0 与 rails6.1.7.6,实现本地 macos 开发,ubuntu 服务器部署,用哪个组合更适合?

是不是 selinux 权限问题?我现在的项目都不用 nginx 了,Caddy 真的很好用。

你看下 puma.access.log 的日志看下是不是 production 环境 puma 启动有问题。 如果有问题,就在本地试试 rails s -e production

需要 登录 后方可回复, 如果你还没有账号请 注册新账号