★動いた AWS,rails,apache,passengerでアプリを動かす、自宅でやってみた続き2018年1月24日
会社の環境でも動かないので、自宅でAWSに無料契約して試してみた。
朝6時頃目が覚めて、インストール作業の続きをやり始めた。
railsを初心者がPRODUCTIONモードで動かすのは大変難しい。
よほど根気が無いと無理だ。ここまで約2週間かかった。誰もそばに設定した人がいない。
Developmentモードならローカル環境ですぐだろうけど、実際に公開するためのProductionモードは経験者が教えてくれないとさっぱり動かないと思った。経験者がいなかったのでひたすらネット情報を頼った。しかも色々のパターンがあって時間がかかる。RAILSが廃れる理由がわかった気がする。
こんな設定だけでわけがわからないなら、誰も使おうとしない。
単に、サンプルチュートリアルを実行するだけなのに、こんなに時間がかかると手軽とは言えない。
(以下のIP、パスワードは別の物にしています。)
内容
Incomplete response received from applicationブラウザのエラーを改善する
ブラウザ表示するとIncomplete response received from applicationエラーが表示された
・httpd.confにシークレットキーを入れた
会社の環境でも発生したブラウザのエラーは同じ「Incomplete response received from application」というエラーだった。そこでsecerts.ymlにシークレットキーを入れる
またログを見る。調べる
1 |
$ bundle exec rake secret |
qnaiglfmugm82487yfam7y4qixm8734pt7bfdac6622(サンプルなので実際のものとは違う)
そして環境変数に
1 2 |
$export SECRET_KEY_BASE= qnaiglfmugm82487yfam7y4qixm8734pt7bfdac662 |
与える
以下の書き方で直接変数に与える書き方もある
1 |
$export SECRET_KEY_BASE=`bundle exec rake secret` |
しかし、だめ。環境変数の設定の仕方がよくわかっていない。
1 |
RAILS_ENV=production rake secret |
を実行してみる
ed48dd0c73fe69371ac2d854257ad4a538118b4475404fecc810b72fb4ed7153a857888b6dd8c8eaf0f623800ccc859ec3b4193d86ee30abe7511dfbe2d92d96
だめ
こっちに入れてみることにした。これで良さそう
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
(サンプル) <VirtualHost *:80> ServerName f***.jp:80 DocumentRoot /var/lib/rails/fapp/public <Directory /var/lib/rails/fapp/public/> SetEnv SECRET_KEY_BASE acdfg...z #←ココに置くことでOK Options FollowSymLinks AllowOverride None Require all granted </Directory> PassengerEnabled on </VirtualHost> ***↓こんな感じで設定した <VirtualHost *:80> ServerName 18.218.231.** DocumentRoot /var/www/html/hiyoko/public <Directory /var/www/html/hiyoko/public/> SetEnv SECRET_KEY_BASE ed48dd0c73fe6937(りゃく)e30abe7511dfbe2d92d96 Options FollowSymLinks AllowOverride None #Require all granted ここを削除するかコメント </Directory> PassengerEnabled on </VirtualHost> |
とりあえず、バーチャルで次の段階へ進むが、今度はインターナル500エラーになった。
AM8:27 今日は有給休み
・.rbenvまでをROOTにしてみたchown root:root .rbenv
今度はパッセンジャーpassengerの権限がROOTではだめらしいが、他のファイルやパスはjhonがルート権限でいい場合と悪い場合があるらしい
error_logを調べると
[ W 2018-01-24 08:26:20.8109 13901/T1 age/Cor/CoreMain.cpp:897 ]: WARNING: potential privilege escalation vulnerability. Passenger is running as root, and part(s) of the passenger root path (/home/jhon/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/passenger-5.1.12) can be changed by non-root user(s):
「/home/jhon/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/passenger-5.1.12」:non-root user(s)
jhonがルート権限でいい場合と悪い場合があるらしい。じゃなくて、ROOT権限にしろということか。
apacheUserにしてみるといいのか?と思ったらROOTユーザにしろって意味かな?結果はROOTにした
[ W 2018-01-24 08:26:20.8109 13901/T1 age/Cor/CoreMain.cpp:897 ]: WARNING: potential privilege escalation vulnerability. Passenger is running as root, and part(s) of the passenger root path
/home/jhon/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/passenger-5.1.12) can be changed by non-root user(s):
The path ”
/home/jhon/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/passenger-5.1.12″ can be modified by user “jhon” (or applications running as that user).
Change the owner of the path to root, or avoid running Passenger as root.
The path ”
/home/jhon/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems” can be modified by user “jhon” (or applications running as that user).
Change the owner of the path to root, or avoid running Passenger as root.
The path ”
/home/jhon/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0″ can be modified by user “jhon” (or applications running as that user).
Change the owner of the path to root, or avoid running Passenger as root.
The path ”
/home/jhon/.rbenv/versions/2.4.3/lib/ruby/gems” can be modified by user “jhon” (or applications running as that user).
Change the owner of the path to root, or avoid running Passenger as root.
The path ”
/home/jhon/.rbenv/versions/2.4.3/lib/ruby” can be modified by user “jhon” (or applications running as that user).
Change the owner of the path to root, or avoid running Passenger as root.
The path ”
/home/jhon/.rbenv/versions/2.4.3/lib” can be modified by user “jhon” (or applications running as that user).
Change the owner of the path to root, or avoid running Passenger as root.
The path ”
/home/jhon/.rbenv/versions/2.4.3″ can be modified by user “jhon” (or applications running as that user).
Change the owner of the path to root, or avoid running Passenger as root.
The path ”
/home/jhon/.rbenv/versions” can be modified by user “jhon” (or applications running as that user).
Change the owner of the path to root, or avoid running Passenger as root.
The path ”
/home/jhon/.rbenv” can be modified by user “jhon” (or applications running as that user).
Change the owner of the path to root, or avoid running Passenger as root.
The path ”
/home/jhon” can be modified by user “jhon” (or applications running as that user).
Change the owner of the path to root, or avoid running Passenger as root.
よくみるとJhonまでROOTにしろと言っているのはおかしい
なので、まずは.rbenvまでをROOTにしてみた
[root@ip-172-31-33-76 jhon]# chown root:root .rbenv
[root@ip-172-31-33-76 jhon]# ls -al
一応これも入れておく
・httpd.confに
PassengerUserSwitching off
PassengerDefaultUser apache
だめ
だが、以前のようなROOT権限のエラーは消えた
・Require GET POST <<<これを削除すればよいらしい
次は
configuration error: couldn’t perform authentication. AuthType not set!
のエラーになった。
1 2 3 4 5 |
<Directory "/var/www/html"> ... ... Require GET POST <<<これを削除すればよいらしい************* </Directory> |
でも今のAWSにその設定がない
と思ったら、自分でバーチャル設定をしたところに
あった
Require all granted <<<これをコメントにする*************
別のエラーがブラウザで出たが多分これは、一歩前進したほうだ。
多分パスの設定か何かがおかしい
「We’re sorry, but something went wrong.」
If you are the application owner check the logs for more information.
AM9時44分
次はここからだ
エラーログを見ると
Rails Error: Unable to access log fileのアクセス権の問題があるみたい
[ N 2018-01-24 09:41:36.7566 14397/T7 age/Cor/SecurityUpdateChecker.h:374 ]: Security update check: no update found (next check in 24 hours)
App 14425 stdout:
App 14425 stderr: Rails Error: Unable to access log file. Please ensure that /home/jhon/hiyoko/log/production.log exists and is writable (ie, make it writable for user and group: chmod 0664 /home/jhon/hiyoko/log/production.log). The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
App 14441 stdout:
App 14425 stderr: Started GET “/users/show” for 110.3.248.146 at 2018-01-24 09:43:01 +0900
App 14425 stderr:
App 14425 stderr: Mysql2::Error (Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)):
・まず、production.logの権限を666にしておく
*************
[jhon@ip-172-31-33-76 log]$ chmod 666 production.log
[jhon@ip-172-31-33-76 log]$ ls -al
ログを見るとこのエラーは解消したみたい。
エラーがなかったので。
次は、mysql2のソケットエラーのようです。
そういえばMYSQLをインストールしたけど接続をしていない。
接続すれば、このソケットファイルが作成されるらしい。
だからだ。
まずはMYSQLに接続して、ちゃんとMYSQLのUserを作成しよう。
mysqlをインストールしたけれど、AWS設定でポートを開けなナイトいけないらしい
セキュリティグループでMYSQLのポートがあるのでそこを開ける
どうやらAWSのRDSでMYSQLを使う方法と、MYSQLをインストールして使う方法があるらしい。
今回は、MYSQLをインストールして使用する方法で進める
しかし
[jhon@ip-172-31-33-76 hiyoko]$ mysql -u root
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)
[jhon@ip-172-31-33-76 hiyoko]$
単純に接続できてない。
・MySQL を起動する
**************************
sudo service mysqld start
起動設定も入れる
***************************
[jhon@ip-172-31-33-76 hiyoko]$ sudo chkconfig –add mysqld
[jhon@ip-172-31-33-76 hiyoko]$ sudo chkconfig mysqld on
[jhon@ip-172-31-33-76 hiyoko]$
ポートを確認
netstat で 3306 ポートが開放されているか確認。
sudo netstat -antu
その後再度
[jhon@ip-172-31-33-76 hiyoko]$ mysql -u root
とすると ちゃんと接続できた。
なんのことはない。MYSQLが起動していない。
だけだった。
次に
ROOTにパスワードを設定する
**********************
# mysql -u root
mysql> update mysql.user set password=password(‘root用の任意パスワード’) where user = ‘root’;
mysql> flush privileges; ← 変更を反映
mysql> exit;
mysql -u rootで接続する
mysql> update mysql.user set password=password(‘Mimata1**’) where user=’root’;
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
[jhon@ip-172-31-33-76 hiyoko]$ mysql -u root -p
これで先程のパスワードを入れる
今度はUserを設定してみる。ひよこと自分のUserを追加した(これだけではログインはできないので、
下の@localhostにも与える必要があることを忘れない)
GRANT ALL PRIVILEGES ON *.* TO hiyoko IDENTIFIED BY ‘Mimata1**’ WITH GRANT OPTION
GRANT ALL PRIVILEGES ON *.* TO maseda IDENTIFIED BY ‘Mimata1**’ WITH GRANT OPTION
しかし、
mysql -u maseda -p
でログインできない。
アクセス拒否のエラーになる
[jhon@ip-172-31-33-76 hiyoko]$ mysql -u hiyoko -p
Enter password:
ERROR 1045 (28000): Access denied for user ‘hiyoko’@’localhost’ (using password: YES)
[jhon@ip-172-31-33-76 hiyoko]$
・権限設定
***************************************
こっちも実行しないといけないらしい
grant select on *.* to hiyoko@’localhost’ identified by ‘Mimata1****’;
grant select on *.* to maseda@’localhost’ identified by ‘Mimata1*’;
これで入れた。
MYSQLのUserも追加できた。
これでもう一度ブラウザにアクセスする。
次は
エラーログを見るとブラウザのエラーは同じで
エラーログは
今度は別のアクセス拒否になった
App 14425 stderr: Started GET “/users/show” for 110.3.248.146 at 2018-01-24 12:46:37 +0900
App 14425 stderr:
App 14425 stderr: Mysql2::Error (Access denied for user ‘hiyoko’@’localhost’ (using password: NO)):
App 14425 stderr: mysql2 (0.4.10) lib/mysql2/client.rb:89:in connect'
initialize’
App 14425 stderr: mysql2 (0.4.10) lib/mysql2/client.rb:89:in
ちなみにmysql -u hiyoko -p
では接続可能
もしかして、RAILSのバージョンが上がってDBの作成まで進めないとだめなのかな?
config/database.yml
**************************
にパスワードが設定されていないから
1 2 3 4 5 6 |
production: <<: *default database: hiyoko_production username: hiyoko >>あとでahiruにした password: <%= ENV['HIYOKO_DATABASE_PASSWORD'] %> 見たらこんな感じで環境変数だった |
defaultとproductionにパスワードを設定する。できれば違うパスワードが望ましいが、、、
export HIYOKO_DATABASE_PASSWORD=Mimata1***
実行する
これでまた実行してみる
これでもまだだめ。
またログを見る
同じエラーでアクセス拒否になる
rake db:create RAILS_ENV=production 実行すると同じアクセス拒否になった。
[jhon@ip-172-31-33-76 hiyoko]$ rake db:create RAILS_ENV=production
Mysql2::Error: Access denied for user ‘hiyoko’@’localhost’ to database ‘hiyoko_production’: CREATE DATABASE hiyoko_production
DEFAULT CHARACTER SET utf8
COLLATE utf8_unicode_ci
ということは、DBの設定がおかしいのか?
ちゃんとproductionを見ていることは正しい。
もしかして、MYSQLですでにhiyokoを作ったことがいけないのか?
ということで、database.ymlに別のUserを設定してみる。
username:ahiruにしてから
rake db:create RAILS_ENV=production
**************************
を実行するとちょっと違う感じがでてきたが、これで良かったみたい
[jhon@ip-172-31-33-76 hiyoko]$ rake db:create RAILS_ENV=production
Access denied for user ‘ahiru’@’localhost’ (using password: YES)Please provide the root password for your MySQL installation
>Mimata1** <<<パスワードを求められた。これってパスワードだよね
[jhon@ip-172-31-33-76 hiyoko]$ mysql -u root -p
Enter password:
これでDBを見ると
1 2 3 4 5 6 7 8 9 10 11 12 13 |
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hiyoko_production | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) mysql> |
これでhiyoko_productionができたってことか?
mysql> use hiyoko_production;
Database changed
mysql> show tables;
Empty set (0.00 sec)
まだテーブルはない。確かにまだテーブルを作っていないから。
ということはアクセスができるようになったということか?
でもまだ改善されない。
エラーログを見ると
hiyokoがいる
App 14425 stderr: Started GET “/users/show” for 110.3.248.146 at 2018-01-24 13:27:32 +0900
App 14425 stderr:
App 14425 stderr: Mysql2::Error (Access denied for user ‘hiyoko’@’localhost’ (using password: NO)):
App 14425 stderr: mysql2 (0.4.10) lib/mysql2/client.rb:89:in connect'
MYSQLを見るとちゃんとアヒルもひよこもいる。これをみると別にRAILSで使うときは事前にUserを作る必要はないみたい
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
mysql> select host,user,password from mysql.user; +-----------------+--------+-------------------------------------------+ | host | user | password | +-----------------+--------+-------------------------------------------+ | localhost | root | *ADD84(略)04 | | ip-172-31-33-76 | root | *ADD8404 | | 127.0.0.1 | root | *ADD8404 | | ::1 | root | *ADD8404 | | localhost | | | | ip-172-31-33-76 | | | | % | hiyoko | *ADD8404 | | % | maseda | *ADD8404 | | localhost | hiyoko | *ADD8404 | | localhost | maseda | *ADD8404 | | localhost | ahiru | *ADD8404 | +-----------------+--------+-------------------------------------------+ 11 rows in set (0.00 sec) |
困ったときの再起動
[jhon@ip-172-31-33-76 config]$ sudo su
[sudo] password for jhon:
[root@ip-172-31-33-76 config]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@ip-172-31-33-76 config]#
これで念のためにアクセスする
あれ。アクセス拒否のエラーがない
/etc/httpd/logs/error_log
[ N 2018-01-24 13:42:45.4718 21251/T7 age/Cor/SecurityUpdateChecker.h:374 ]: Security update check: no update found (next check in 24 hours)
App 21282 stdout:
App 21298 stdout:
でも、画面上の表示はだめ。
ままMYSQL周りはHTTPDの再起動でよくなったということだね。
なんかうまく行っているが、結果的な部分であともう少しかな
アクセスログでは
500エラー
500 1447は変わらない。
110.3.248.146 - - [24/Jan/2018:13:43:15 +0900] "GET /users/show HTTP/1.1" 500 1477 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
他のエラーログを探して見るか。
hiyoko folderのproduction.logだ
で見ると
今度は
ahiruユーザでアクセス拒否だ
Mysql2::Error (Access denied for user 'ahiru'@'localhost' (using password: NO)): mysql2 (0.4.10) lib/mysql2/client.rb:89:in connect’
もしかして、チュートリアルを進めればいいのかな。
ということで勧めてみる
[jhon@ip-172-31-33-76 hiyoko]$ rails g model user name:string username:string location:string about:text
invoke active_record
create db/migrate/20180124050132_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
rake db:migrate RAILS_ENV=production
これでUserテーブルが作成されたみたい
[jhon@ip-172-31-33-76 hiyoko]$ rake db:migrate RAILS_ENV=production
== 20180124050132 CreateUsers: migrating ======================================
— create_table(:users)
-> 0.0047s
== 20180124050132 CreateUsers: migrated (0.0049s) =============================
[jhon@ip-172-31-33-76 hiyoko]$
MYSQLで作成されたかをahiruユーザで調べてみます
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
[jhon@ip-172-31-33-76 hiyoko]$ mysql -u ahiru -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 26 Server version: 5.5.58 MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hiyoko_production | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> use hiyoko_production; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-----------------------------+ | Tables_in_hiyoko_production | +-----------------------------+ | schema_migrations | | users | +-----------------------------+ 2 rows in set (0.00 sec) mysql> select * from users; Empty set (0.00 sec) mysql> |
ちゃんとUsersテーブルが作成されていました。
hiyoko/log/production.logでエラーがわかります。
やっぱり同じエラーですね。あひるさんがアクセスできない。
今度はここだ。なにか設定がおかしい。時刻14時13分
I, [2018-01-24T14:09:31.475359 #21298] INFO — : Started GET “/users/show” for 110.3.248.146 at 2018-01-24 14:09:31 +0900
F, [2018-01-24T14:09:31.476865 #21298] FATAL — :
Mysql2::Error (Access denied for user ‘ahiru’@’localhost’ (using password: NO)):
もうちょっとだと思うんだけど、ユーザ設定だけだし。
もう一度Apache再起動とする
だめ
次は
config/database.ymlに以下のSOCKETを入れてみて(PRODUCTIONに)
1 2 3 4 5 6 7 8 |
>>>これはダメだった。しかし、エラーは参考になった development: adapter: mysq2 encoding: utf8 username: ahiru(or hiyoko) database: myapp_development socket: /var/lib/mysql/mysql.sockこっちかも。こっちだね >>だめ または socket: '/tmp/mysql.sock' >>だめ |
変更した後に
service httpd restartをすると
今まで見なかったエラーになる。
make_sockでパーミッションのエラーだ。
[jhon@ip-172-31-33-76 config]$ service httpd restart
Stopping httpd: [FAILED]
Starting httpd: (13)Permission denied: make_sock: could not bind to address [::]:80
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
[FAILED]
[jhon@ip-172-31-33-76 config]$
ソケットを書かなければ別のエラーになるので、接続自体はOKのはずなので、
この部分は削除して元に戻す。
それがだめなら、
MYSQLにROOTでログインしてから
SET PASSWORD FOR ‘username’@’localhost’ = PASSWORD(‘[password]’);
↓
SET PASSWORD FOR ‘ahiru’@’localhost’ = PASSWORD(‘Mima***’);
としてやってみる>>>これもダメだった
mysql> SET password for ahiru@localhost = password(‘Mimata1**’);(あえてシングルクォーテーションは入れていない)
Query OK, 0 rows affected (0.00 sec)
mysql>
これでまたHTTPDを再起動した。
だめ
なぜだ???
ahiruじゃなくてhiyokoにしてみる
またHTTPDを再起動してみる
だめ
I, [2018-01-24T16:12:59.777289 #25618] INFO — : Started GET “/users/show” for 110.3.248.146 at 2018-01-24 16:12:59 +0900
F, [2018-01-24T16:12:59.780950 #25618] FATAL — :
Mysql2::Error (Access denied for user ‘hiyoko’@’localhost’ (using password: NO)):
今度はhiyokoになっているので、ちゃんとdatabase.ymlの記述をチェックしているのは確か
ちゃんとMYSQLでログインできるのに、なぜかアクセスできない。
次は
パスワードを直打ちにしてみる。
**************************
パスワードを直打ちにしてみたら、さっきソケットをdatabase.ymlに入力したときの
エラーと同じだ。>>結果はSUDOでHTTPDを再起動しなかっただけで、パスワードの直打ちが必要。
ということは、いままで環境変数のパスワードが消えた?だからログインできなかったんだ。直打ちが効いた。
それで今度はパスワードが設定されたので次のエラーとなった。
TIME時刻午後4時32分
1 2 3 4 5 6 7 8 9 |
[jhon@ip-172-31-33-76 config]$ vi database.yml [jhon@ip-172-31-33-76 config]$ service httpd restart Stopping httpd: [FAILED] Starting httpd: (13)Permission denied: make_sock: could not bind to address [::]:80 (13)Permission denied: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down Unable to open logs [FAILED] [jhon@ip-172-31-33-76 config]$ |
と思ったらSUDOでHTTPDを起動していないだけだった。
SUDO SERVICE HTTPD restartしたら起動した。
16:39
だめなら
ユーザをROOTに変えてみる
と思ったが、パスワードの直打ちでOKだった。
出たー!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
やっと
http://18.218.231.**/users/show
で
Users#show
Find me in app/views/users/show.html.erb」
が、画面に出た。
画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。
画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。画面に出た。
やっほーい。
窓から顔を出してやった−!と叫びたい気分
朝から頑張って、やっとここまできました
今日お休みしたかいがありました。