hasyrails / calendar-vue-original

0 stars 0 forks source link

ポート80でアクセスできない #369

Open hasyrails opened 3 years ago

hasyrails commented 3 years ago

ポート3000 表示成功

$ bundle exec rails s -e production

でポート3000で表示はできる

ポート80 表記できず

http://[Elastic IP] 

でアクセス(ポート 80)→表示できない??

hasyrails commented 3 years ago

試しにport 80 をしてRails起動

 bundle exec rails s -e production -p 80
=> Booting Puma
=> Rails 5.2.4.4 application starting in production 
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.6 (ruby 2.6.4-p104), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: production
Exiting
Traceback (most recent call last):
    40: from bin/rails:3:in `<main>'
    39: from bin/rails:3:in `load'
    38: from /var/www/rails/calendar-vue-original/bin/spring:15:in `<top (required)>'
    37: from /var/www/rails/calendar-vue-original/bin/spring:15:in `require'
    36: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>'
    35: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load'
    34: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/spring-2.1.1/bin/spring:49:in `<top (required)>'
    33: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/client.rb:30:in `run'
    32: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call'
    31: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call'
    30: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load'
    29: from /var/www/rails/calendar-vue-original/bin/rails:9:in `<top (required)>'
    28: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/activesupport-5.2.4.4/lib/active_support/dependencies.rb:291:in `require'
    27: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/activesupport-5.2.4.4/lib/active_support/dependencies.rb:257:in `load_dependency'
    26: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/activesupport-5.2.4.4/lib/active_support/dependencies.rb:291:in `block in require'
    25: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
    24: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
    23: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
    22: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
    21: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
    20: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/railties-5.2.4.4/lib/rails/commands.rb:18:in `<main>'
    19: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/railties-5.2.4.4/lib/rails/command.rb:46:in `invoke'
    18: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/railties-5.2.4.4/lib/rails/command/base.rb:69:in `perform'
    17: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/thor-1.0.1/lib/thor.rb:392:in `dispatch'
    16: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/thor-1.0.1/lib/thor/invocation.rb:127:in `invoke_command'
    15: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/thor-1.0.1/lib/thor/command.rb:27:in `run'
    14: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/railties-5.2.4.4/lib/rails/commands/server/server_command.rb:142:in `perform'
    13: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/railties-5.2.4.4/lib/rails/commands/server/server_command.rb:142:in `tap'
    12: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/railties-5.2.4.4/lib/rails/commands/server/server_command.rb:147:in `block in perform'
    11: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/railties-5.2.4.4/lib/rails/commands/server/server_command.rb:53:in `start'
    10: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/rack-2.2.3/lib/rack/server.rb:327:in `start'
     9: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/puma-3.12.6/lib/rack/handler/puma.rb:73:in `run'
     8: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/puma-3.12.6/lib/puma/launcher.rb:186:in `run'
     7: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/puma-3.12.6/lib/puma/single.rb:98:in `run'
     6: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/puma-3.12.6/lib/puma/runner.rb:153:in `load_and_bind'
     5: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/puma-3.12.6/lib/puma/binder.rb:90:in `parse'
     4: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/puma-3.12.6/lib/puma/binder.rb:90:in `each'
     3: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/puma-3.12.6/lib/puma/binder.rb:106:in `block in parse'
     2: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/puma-3.12.6/lib/puma/binder.rb:273:in `add_tcp_listener'
     1: from /home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/puma-3.12.6/lib/puma/binder.rb:273:in `new'
/home/hasyrails/.rbenv/versions/2.6.4/lib/ruby/gems/2.6.0/gems/puma-3.12.6/lib/puma/binder.rb:273:in `initialize': Permission denied - bind(2) for "0.0.0.0" port 80 (Errno::EACCES)

80や443など、1024より小さなポートを指定するにはroot権限が必要です。

hasyrails commented 3 years ago

ポート80番で起動する方法は、 @chonan さんがあげてくださったようにたくさんありますが、手っ取り早くやるならpassengerとnginxがおすすめです。以下のコマンドを実行し、表示される手順に従ってください。

hasyrails commented 3 years ago

AWSにRailsアプリケーションを実行する環境作りの手順メモ

ポート3000 → ポート80の順に起動している

構成例がいろいろ示されている。 pumaで構成したほうがいいか?

PumaとUnicornの選択ですが前者はマルチスレッド、後者はマルチプロセスの違いがあるようです。Pumaの方がより多くのリクエストを効率的に捌ける利点があるようですが、通常使用ではさほど変わりはないという意見もあります。私はRailsで推奨されているPumaを選択しました。

【Rails】Nginx, Puma な環境によるデプロイ&サーバーの勉強【AWS EC2】 - Qiita

hasyrails commented 3 years ago

変更前

# 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/doc/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 4096;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       3000;
        server_name  [Elastic IP];
        root         /usr/share/nginx/html;

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

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;

変更後

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

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;

    index   index.html index.htm;

    upstream puma {
        server unix:///var/www/rails/calendar-vue-original/shared/tmp/sockets/puma.sock;
    }
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name [Elastic IP];
        root         /var/www/rails/calendar-vue-original/current/public;

        location / {
            try_files $uri $uri/index.html $uri.html @webapp;
        }

        location @webapp {
            proxy_read_timeout 300;
            proxy_connect_timeout 300;
            proxy_redirect off;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://puma;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    server {
        listen 443 ssl;
        server_name [Elastic IP];
        location @webapp {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_pass http://puma;
      }
    }
hasyrails commented 3 years ago

calendar-vue-original.confを変更

 error_log  /var/www/rails/calendar-vue-original/shared/log/nginx.error.log;
  access_log /var/www/rails/calendar-vue-original/shared/nginx.access.log;
  upstream app_server {
    # for UNIX domain socket setups
    server unix:/var/www/rails/calendar-vue-original/shared/tmp/sockets/calendar-vue-original-puma.sock fail_timeout=0;
  }
  server {
    listen 80;
    server_name ;
    # nginx so increasing this is generally safe...
    # path for static files
    root /var/www/rails/calendar-vue-original/current/public;
    # page cache loading
    try_files $uri/index.html $uri @app_server;
    location / {
      # HTTP headers
      proxy_pass http://app_server;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
    }
    # Rails error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
      root /var/www/rails/calendar-vue-original/current/public;
    }
    client_max_body_size 4G;
    keepalive_timeout 5;
  }
hasyrails commented 3 years ago

[ドメイン設定 ポート80以外](Amazon EC2 - ドメイン設定 ポート80以外|teratail)

$ sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 3000

→ ポート3000ではなく起動成功

hasyrails commented 3 years ago

Rails5+Puma+Nginxな環境をCapistrano3でEC2にデプロイする(後編)

$ bundle exec cap production deploy

nginxとunicornが起動しなくなった?

masterへpushしていないため?? → branch 「aws_deploy_18_puma」でブランチ作成、pushして試す

hasyrails commented 3 years ago

アクセスできなくなった‥

hasyrails commented 3 years ago

unicorn + nginxの設定をリセット → puma + nginx の設定へ移行

Ubuntu16.04 + Ruby on Rails 5.1 + Puma + Nginx + Capistranoでstaging環境を構築

nginxの設定ファイルを削除

メインの設定ファイル cd /etc/nginx/ 各サーバー用設定ファイル cd /etc/nginx/conf.d/

nginx コマンド超シンプル早見表 - Qiita