[Ruby] Error when building infrastructure for rails application with aws

5 minute read

This time, referring to this article, I tried building the infrastructure of the rails app I created for the first time using AWS. https://qiita.com/naoki_mochizuki/items/814e0979217b1a25aa3e

EC2 instance environment construction

$git make gcc-c++ patch git:’make’ is not a git command.See’git –help’. The most similar commands are blame, merge, stage

I got the error. This had to be uncommented and run as a one line command. $sudo yum install git make gcc-c++ patch openssl-devel libyaml-devel libffi-devel libicu-devel libxml2 libxslt libxml2-devel libxslt-devel zlib-devel readline-devel mysql mysql-server mysql-devel ImageMagick ImageMagick-devel epel- release

$sudo mkdir www

mkdir: unable to create directory `www’: File exists I got the error. It seems that it already exists, but I don’t remember creating it… I was wondering whether to use it as it was, but for the time being I created a file with another name (XYZ) and used it instead.

Cooperation with git, application clone

$cat aws_git_rsa.pub cat: aws_git_rsa.pub: No such file or directory

This was because the operation was different from the normal command input when creating the public key, so a half-width space was added after :aws_git_rsa. (Most mistake…) $ ssh-keygen -t rsa


Enter file in which to save the key ():aws_git_rsa (half-width space in this part) Once I recreated the key, I was able to successfully reveal the contents of the public key.

MySQL settings

When I try to start Mysql, I get the following error $sudo service mysqld start #start mysqld Redirecting to /bin/systemctl start mysqld.service Failed to start mysqld.service: Unit not found. I ran the following command referring to the following article and it worked. https://qiita.com/yuta-38/items/4074f5ada9e22088c8dd $ sudo yum install -y mariadb-server $ sudo systemctl enable mariadb $ sudo service mariadb start

Also, on Amazon Linux 2, when you try to install mysql with yum, it seems to try to install mariaDB. I didn’t practice it, but the following article may be helpful. https://qiita.com/hamham/items/fd77bb0bb167a150dc8e#mysql57%E3%81%AE%E5%B0%8E%E5%85%A5

Start Nginx

I entered the following command but Nginx does not start. $ sudo service nginx start Redirecting to /bin/systemctl start nginx.service Job for nginx.service failed because the control process exited with error code.See “systemctl status nginx.service” and “journalctl -xe” for details.

Run the following command as instructed in the error $ 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: failed (Result: exit-code) since Mon 2020-07-13 09:10:04 UTC; 57s ago Process: 11360 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE) Process: 11356 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 11355 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)

I did not understand the cause, so I also executed the following. $sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

I didn’t have any problems with the config file, so I was in trouble. While investigating various things, I found the following command. $ sudo service nginx status -l Redirecting to /bin/systemctl status -l nginx.service ● nginx.service-The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: failed (Result: exit-code) since Mon 2020-07-13 09:10:04 UTC; 27min ago Process: 11360 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE) Process: 11356 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 11355 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) July 13 09:10:02 ip-10-0-10-10.ap-northeast-1.compute.internal nginx[11360]: nginx: [emerg] bind() to [::]:80 failed (98 : Address already in use) July 13 09:10:03 ip-10-0-10-10.ap-northeast-1.compute.internal nginx[11360]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) July 13 09:10:03 ip-10-0-10-10.ap-northeast-1.compute.internal nginx[11360]: nginx: [emerg] bind() to [::]:80 failed (98 : Address already in use) July 13 09:10:03 ip-10-0-10-10.ap-northeast-1.compute.internal nginx[11360]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) July 13 09:10:03 ip-10-0-10-10.ap-northeast-1.compute.internal nginx[11360]: nginx: [emerg] bind() to [::]:80 failed (98 : Address already in use) July 13 09:10:04 ip-10-0-10-10.ap-northeast-1.compute.internal nginx[11360]: nginx: [emerg] still could not bind() July 13 09:10:04 ip-10-0-10-10.ap-northeast-1.compute.internal systemd[1]: nginx.service: control process exited, code=exited status=1 July 13 09:10:04 ip-10-0-10-10.ap-northeast-1.compute.internal systemd[1]: Failed to start The nginx HTTP and reverse proxy server. July 13 09:10:04 ip-10-0-10-10.ap-northeast-1.compute.internal systemd[1]: Unit nginx.service entered failed state. July 13 09:10:04 ip-10-0-10-10.ap-northeast-1.compute.internal systemd[1]: nginx.service failed.

According to the above, it seems that the configured port 80 is already running. As a result of considering the cause, I remembered that I had previously installed Apache and set up automatic startup in a web course. $ sudo systemctl status httpd.service ● httpd.service-The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled) Active: active (running) since Sun 2020-07-12 06:24:44 UTC; 2 days ago Docs: man:httpd.service(8) Main PID: 21760 (httpd) Status: “Total requests: 149; Idle/Busy workers 100/0; Requests/sec: 0.00084; Bytes served/sec: 2 B/sec” CGroup: /system.slice/httpd.service├─20766 /usr/sbin/httpd -DFOREGROUND ├─21049 /usr/sbin/httpd -DFOREGROUND ├─21055 /usr/sbin/httpd -DFOREGROUND ├─21056 /usr/sbin/httpd -DFOREGROUND ├─21760 /usr/sbin/httpd -DFOREGROUND ├─21761 /usr/sbin/httpd -DFOREGROUND ├─21762 /usr/sbin/httpd -DFOREGROUND ├─21763 /usr/sbin/httpd -DFOREGROUND ├─21764 /usr/sbin/httpd -DFOREGROUND ├─21765 /usr/sbin/httpd -DFOREGROUND └─21887 /usr/sbin/httpd -DFOREGROUND Sure enough Apache was already working. After disabling & stopping the automatic start, I could successfully start it by executing the NginX start command.

The final settings were completed, but the following was displayed on the web page.

``We’re sorry, but something went wrong. If you are the application owner check the logs for more information.

Let’s take a look at Unicorn and Nginx error logs. (Reference article There is a description of the path to the error in each setting) $ tail -f /var/xyz/rails/circle_app/log/unicorn.log I, [2020-07-14T08:37:26.186843 #19212] INFO –: listening on addr=/var/xyz/rails/circle_app/tmp/sockets/.unicorn.sock fd=9 I, [2020-07-14T08:37:26.203171 #19212] INFO –: master process ready I, [2020-07-14T08:37:26.207382 #19214] INFO –: worker=0 ready I, [2020-07-14T08:37:26.207617 #19215] INFO –: worker=1 ready Unicorn looks fine.

$ tail -f /var/xyz/rails/circle_app/log/nginx.error.log 2020/07/14 08:36:17 [crit] 19174#0: *1 connect() to unix:/var/xyz/rails/circle_app/tmp/sockets/.unicorn.sock failed (2: No such file or directory) while connecting to upstream, client: 103.5.140.158, server: 46.51.239.151, request: “GET / HTTP/1.1”, upstream: “http://unix:/var/xyz/rails/circle_app/tmp/sockets /.unicorn.sock:/”, host: “46.51.239.151”

There seems to be a problem with Nginx. It seems that you have to change the path writing from CentOS 7 as described in the next article. https://qiita.com/emahiro/items/b2762a26bca35bbb0cf9

I modified the Unicorn and Nginx files by referring to the following article. https://qiita.com/noraworld/items/c8acf4bb981c8d8535df Also check Unicorn running with the following command and stop it with the kill command $ ps aux | grep unicorn $ kill -9 pid ← Substitute the number displayed by the above command for the pid part Restarted with the following command $ unicorn_rails -c /var/www/rails/(app name)/config/unicorn.conf.rb -D -E production After all the above, reload the web page and it will be displayed safely! !! !!

The following course, which I purchased at Udemy before building the infrastructure, worked and I understood the tempo well. https://www.udemy.com/course/aws-and-infra/