0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

EC2のWebサーバーの設定をしてみた

Posted at

初めまして

今回オリアプのデプロイをEC2にて行いましたので、その際のWebサーバー設定を備忘録として残りたいと思います。

使用サーバー

Nginx(エンジン・エックス)
※ユーザーのリクエストに対して静的コンテンツのみ取り出し処理を行い、動的コンテンツの生成はアプリケーションサーバに依頼します。

目的

EC2内にWebサーバーを用意して起動させます。
※Webサーバーは静的コンテンツ(リクエストを送っても内容が変わらない物)を表示します。

Nginxを導入します。

ターミナル
[ec2-user@ip-ご自身のIP ~]$ sudo amazon-linux-extras install nginx1
コマンドが出てきたら「Yes」で

次に、設定ファイルを編集します。

iを押してインサートモードにしてください。

編集箇所
①アプリケーション名をご自身のアプリ名へ
②ElasticIPをご自身のIPへ

編集後、「esc」「:wq」を押して保存終了します。

[ec2-user@ip-ご自身のIP ~]$ sudo vim /etc/nginx/conf.d/rails.conf

コマンドを実行すると、下記が表示されます。
ファイルの詳細を表示されます。

/etc/nginx/conf.d/rails.conf
upstream app_server {
  # Unicornと連携させるための設定
  server unix:/var/www/アプリケーション名/tmp/sockets/unicorn.sock;
}

# {}で囲った部分をブロックと呼ぶ。サーバの設定ができる
server {
  # このプログラムが接続を受け付けるポート番号
  listen 80;
  # 接続を受け付けるリクエストURL ここに書いていないURLではアクセスできない
  server_name Elastic IP;

  # クライアントからアップロードされてくるファイルの容量の上限を2ギガに設定。デフォルトは1メガなので大きめにしておく
  client_max_body_size 2g;

# 接続が来た際のrootディレクトリ
  root /var/www/アプリケーション名/public;

# assetsファイル(CSSやJavaScriptのファイルなど)にアクセスが来た際に適用される設定
  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;

  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
  }

  error_page 500 502 503 504 /500.html;
}

次に「Nginx」の権限を変更します。

[ec2-user@ip-ご自身のIP ~]$ cd /var/lib
[ec2-user@ip-ご自身のIP lib]$ sudo chmod -R 775 nginx
# postメソッドを使用してもエラーが出ないようにする

[ec2-user@ip-ご自身のIP lib]$ cd ~
[ec2-user@ip-ご自身のIP ~]$ sudo systemctl reload nginx
[ec2-user@ip-ご自身のIP ~]$ sudo systemctl start nginx
# Nginx設定ファイルを再読み込みして起動します。

Unicornの設定を変更します。

アプリケーションサーバーの設定変更になります。

railsのconfig/unicorn.rbにて



listen 3000

↓のように変更します。ポート番号からアプリのパスにします。

listen "#{app_path}/tmp/sockets/unicorn.sock"

変更後は、Githubをしている場合は、push、本番環境にも上げておきます。

# 開発中のアプリケーションに移動
[ec2-user@ip-ご自身のIP ~]$ cd /var/www/開発中のアプリケーション

# GitHubの内容をEC2に反映させる
[ec2-user@ip-ご自身のIP <レポジトリ名>]$ git pull origin master

Unicornを再起動します。

再起動の手順は
①プロセスの確認
②プロセスをkill
③unicorn_railsコマンドを実行
です。

# プロセスを確認します
[ec2-user@ip-ご自身のIP <リポジトリ名>]$ ps aux | grep unicorn

ec2-user 17877  0.4 18.1 588472 182840 ?       Sl   01:55   0:02 unicorn_rails master -c config/unicorn.rb -E production -D
# ↑ unicorn_rails masterの「17877」がプロセスidです。これをkillで止めます。
ec2-user 17881  0.0 17.3 589088 175164 ?       Sl   01:55   0:00 unicorn_rails worker[0] -c config/unicorn.rb -E production -D
ec2-user 17911  0.0  0.2 110532  2180 pts/0    S+   02:05   0:00 grep --color=auto unicorn

[ec2-user@ip-ご自身のIP <リポジトリ名>]$ kill 「unicorn_rails masterのご自身のプロセスid」

# 最後にUnicornを起動します。
[ec2-user@ip-ご自身のIP <リポジトリ名>]$ RAILS_SERVE_STATIC_FILES=1 unicorn_rails -c config/unicorn.rb -E production -D

以上でWebサーバーの設定が完了できました。

最後に

実際に、作業をして文字として残すことによって徐々にAWSの理解が追いついてきました。
また、Webサーバーとアプリケーションサーバーの違いも理解しておくことで、作業自体の意味も理解しやすくなりますね!

ここまで読んでいただきありがとうございました!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?