ローカルで作成したRailsのアプリをデプロイするために、
AWSのサービスである「EC2」と「VPC」を利用しています。
▼(デプロイ編②)世界一丁寧なAWS解説。EC2を利用して、RailsアプリをAWSにあげるまで
https://qiita.com/naoki_mochizuki/items/5a1757d222806cbe0cd1#nginx%E3%81%AE%E8%B5%B7%E5%8B%95
上記記事に沿ってデプロイを進めていきましたが、
エラーがなかなか解決できずに息詰まりました・・・。
まだ、ブラウザの画面でアプリを表示させることは実現できていませんが、
一つエラー解決できたので、コマンドなど忘れないためにも記録として残しておきます。
また、この記事がどなたかのお役に立てたなら幸いです。
$ sudo service nginx startを実行した結果
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
特に問題なさそうです。
$ systemctl status firewalldを実行した結果
Unit firewalld.service could not be found.
$ ps -ef | grep unicorn | grep -v grepを実行した結果
asami 4290 1 0 5月08 ? 00:00:01 unicorn_rails master -c /var/www/rails/アプリ名/config/unicorn.conf.rb -D -E production
asami 4293 4290 0 5月08 ? 00:00:00 unicorn_rails worker[0] -c /var/www/rails/アプリ名/config/unicorn.conf.rb -D -E production
asami 4294 4290 0 5月08 ? 00:00:00 unicorn_rails worker[1] -c /var/www/rails/アプリ名/config/unicorn.conf.rb -D -E production
特に問題なさそうです。
$ curl http://127.0.0.1/の結果
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Welcome to Amazon Linux 2</title>
<style rel="stylesheet" type="text/css">
html { background-color: white; font-family: "DejaVu Sans", "Liberation Sans", sans-serif; margin: 10% 20%; }
body { margin:0; padding:0; background: white; }
a { color: #007eb9; }
</style>
</head>
<body>
<p>Thank you for using <a href="https://aws.amazon.com/amazon-linux-2/">Amazon Linux 2</a>.<p>
<p>Now that you have it installed, find announcements and discussion in <a href="https://forums.aws.amazon.com/forum.jspa?forumID=228">the AWS Discussion Forums</a>. Also try <a href="https://aws.amazon.com/documentation/">AWS documentation</a>.</p>
</body>
</html>
特に問題なさそうです。
http://xx.xxx.xx.xx/(Elastic IP アドレス) を実行した結果
AWSの状況
/etc/nginx/conf.d/アプリ名.confの中身
# log directory
error_log /var/www/rails/アプリ名/log/nginx.error.log;
access_log /var/www/rails/アプリ名/log/nginx.access.log;
# max body size
client_max_body_size 2G;
upstream app_server {
# for UNIX domain socket setups
server unix:/var/www/rails/アプリ名/tmp/sockets/unicorn.sock fail_timeout=0;
}
upstream unicorn {
server unix:/var/www/rails/アプリ名/tmp/sockets/unicorn.sock;
}
server {
listen 80;
server_name 18.180.71.74;
# nginx so increasing this is generally safe...
keepalive_timeout 5;
# path for static files
root /var/www/rails/アプリ名/public;
# page cache loading
try_files $uri/index.html $uri.html $uri @app;
location @app {
# HTTP headers
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;
}
location @unicorn {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://unicorn;
}
# Rails error pages
error_page 500 502 503 504 /500.html;
nginxのログの状況
①$ sudo nano /var/log/nginx/access.logを実行した結果
111.239.181.40 - - [09/May/2021:11:17:42 +0000] "GET / HTTP/1.1" 301 5 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36$
188.166.70.83 - - [09/May/2021:11:20:20 +0000] "GET / HTTP/1.0" 500 1635 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/2010010$
127.0.0.1 - - [09/May/2021:11:32:28 +0000] "GET / HTTP/1.1" 200 732 "-" "curl/7.61.1" "-"
188.166.70.83 - - [09/May/2021:11:48:07 +0000] "GET /_ignition/execute-solution HTTP/1.1" 301 5 "-" "python-requests/2.18.4" "-"
132.145.196.125 - - [09/May/2021:12:15:13 +0000] "POST / HTTP/1.1" 307 5 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like $
132.145.196.125 - - [09/May/2021:12:15:15 +0000] "GET /.env HTTP/1.1" 301 5 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, li$
124.219.132.185 - - [09/May/2021:12:35:52 +0000] "GET / HTTP/1.1" 301 5 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHT$
170.254.75.31 - - [09/May/2021:12:44:03 +0000] "GET / HTTP/1.1" 301 5 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like$
47.241.125.249 - - [09/May/2021:13:00:54 +0000] "\x05\x01\x00" 400 157 "-" "-" "-"
47.241.125.249 - - [09/May/2021:13:01:20 +0000] "GET http://passport.baidu.com/ HTTP/1.1" 200 732 "-" "-" "-"
47.241.125.249 - - [09/May/2021:13:02:16 +0000] "\x04\x01\x00Pg)\xA7\xEA\x00" 400 157 "-" "-" "-"
②sudo service nginx statusを実行した結果
Redirecting to /bin/systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)
5月 10 14:23:27 ip-10-0-0-248.ap-northeast-1.compute.internal systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
5月 10 14:23:27 ip-10-0-0-248.ap-northeast-1.compute.internal systemd[1]: Unit nginx.service entered failed state.
5月 10 14:23:27 ip-10-0-0-248.ap-northeast-1.compute.internal systemd[1]: nginx.service failed.
5月 10 14:26:24 ip-10-0-0-248.ap-northeast-1.compute.internal systemd[1]: Starting The nginx HTTP and reverse proxy server...
5月 10 14:26:24 ip-10-0-0-248.ap-northeast-1.compute.internal nginx[20542]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
5月 10 14:26:24 ip-10-0-0-248.ap-northeast-1.compute.internal nginx[20542]: nginx: configuration file /etc/nginx/nginx.conf test is su...sful
5月 10 14:26:24 ip-10-0-0-248.ap-northeast-1.compute.internal systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
5月 10 14:26:24 ip-10-0-0-248.ap-northeast-1.compute.internal systemd[1]: Started The nginx HTTP and reverse proxy server.
5月 10 15:29:29 ip-10-0-0-248.ap-northeast-1.compute.internal systemd[1]: Stopping The nginx HTTP and reverse proxy server...
5月 10 15:29:29 ip-10-0-0-248.ap-northeast-1.compute.internal systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Hint: Some lines were ellipsized, use -l to show in full.
上記のメッセージの中に、下記エラーメッセージ発見!
解決方法は後半で説明します。
Failed to start The nginx HTTP and reverse proxy server.
Unit nginx.service entered failed state.
nginx.service failed.
Failed to read PID from file /run/nginx.pid: Invalid argument
unicornのログの状況
/var/www/rails/アプリ名/log に移動し、
$ sudo tail unicorn.log(最新10行のログ確認コマンド)を実行した結果
I, [2021-05-08T17:43:30.984050 #4428] INFO -- : worker=0 ready
I, [2021-05-08T17:43:30.985252 #4429] INFO -- : worker=1 ready
I, [2021-05-08T18:20:29.354874 #4425] INFO -- : reaped #<Process::Status: pid 4428 exit 0> worker=0
I, [2021-05-08T18:20:29.354950 #4425] INFO -- : reaped #<Process::Status: pid 4429 exit 0> worker=1
I, [2021-05-08T18:20:29.355014 #4425] INFO -- : master complete
I, [2021-05-08T19:24:42.486318 #4290] INFO -- : Refreshing Gem list
I, [2021-05-08T19:24:44.942332 #4290] INFO -- : listening on addr=/var/www/rails/アプリ名/tmp/sockets/unicorn.sock fd=9
I, [2021-05-08T19:24:44.957852 #4290] INFO -- : master process ready
I, [2021-05-08T19:24:44.962589 #4293] INFO -- : worker=0 ready
I, [2021-05-08T19:24:44.964148 #4294] INFO -- : worker=1 ready
$ netstat -tlntを実行した結果
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::3306 :::* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
【補足】ツールのバージョン
macOs Catalina 10.15.7
Nginx 1.16.1
Unicorn 6.0.0
Rails 6.0.3
Ruby 2.7.2p137
【結論】SELinuxを「Permissive モード」に変更すれば、下記エラーに関しては解消される
先程のエラーメッセージ4列のうち、下記3列に関してはSELinuxのモード変更で簡単に解消できます。
Permissiveモードに変更してください。
Failed to start The nginx HTTP and reverse proxy server.
Unit nginx.service entered failed state.
nginx.service failed.
手順は下記参考記事の【手順4.2 Permissive モードへの変更】のとおりです。
以上です。
ただし、先程のエラーメッセージ4列のうち、下記1列に関してはまだ解決できていません。
Failed to read PID from file /run/nginx.pid: Invalid argument
解決できたらまた記事にしたいと思います。