LoginSignup
0
0

Ec2でサーバ立ち上げてみた(忘備録)

Last updated at Posted at 2024-06-30

背景

採用の時にスキルテストがあってその中でサーバ立ち上げてみてくださいと言われたので、サーバを立ち上げてみた

手順

  1. VPCやサブネットの構成する(デフォルトで)
  2. セキュリティーグループAを作成する
  3. Ec2を作成する
    • セキュリテイーグループはAで指定する
  4. ロードバランサーを作成する
    • セキュリテイーグループはAで指定する
  5. セキュリティグループAはHTTP、 HTTPS 、 アプリケーションで使うポートを指定
  6. Route53でホストを買う
  7. ACMの認証書を作成する
  8. さっき買ったホストC_NAMEをACMの認証情報で合わせる
  9. Route53でAレコードの設定をする(トラフィックをロードバランサーに流すように設定する)
  10. ELBのリスナーに443ポートの設定を行う(さっきのACMを設定する)80番ポートになったら443ポートに行くように設定する
  11. Ec2にNginxをダウンロードする
  12. Nginxを下記のように設定する
  13. Ec2にサーバーのコード作成
  14. サーバ実行(アプリケーションで開放するポートは8080)
  15. ターゲットグループを作成する(HTTP、HTTPS 、アプリケーションで使うポートを指定)

ハマってたところ

  • GoのEchoフレームワークでアプリを作成し、 Nginxなしでサーバを実行しようとしてたが
    502 Bad Gatewayになってしまった
  • アプリまでリクエスト来るように見えるが、acme/autocert: missing server nameエラーになっていた

解決方法

  • Nginxをダウンロードし、ターゲットグループと設定を合わせて解決した

Nginxの設定

  • nginxダウンロード
yum install nginx -y
  • nginx設定フィアるの確認
sudo vi /etc/nginx/nginx.conf
  • nginx.confファイルを下記のように設定
/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 notice;
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;
    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       80;
        listen       [::]:80;
        
        # Aレコードで指定する値を利用する
        server_name  XXX.com www.XXX.com;
        root         /usr/share/nginx/html;

        # httpでリクエストされたら、403で返す
        if ($http_x_forwarded_proto = 'http'){
           return 301 https://$host$request_uri;
        }


        location / {
                # 下記のように設定する
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header HOST $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

                # アプリケーションのポートと合わせる 
                proxy_pass http://127.0.0.1:8080;
        }
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.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  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#        location = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }

}

終わってたら再度設定する

sudo systemctl restart nginx

ターゲットグループの設定

스크린샷 2024-06-30 23.06.54.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0