Rails app on AWS Elastic Beanstalk: Bad nginx config - ruby-on-rails

So I have a rails application that I built and deployed via AWS Elastic Beanstalk a few months ago. The project was put on hold so I terminated the environment, expecting to be able to re-deploy when we returned to this project.
Despite my app still running just fine on my local dev environment, I cannot get it to deploy. The error from my eb-activty.log:
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
The database is a standalone AWS RDS instance that I can successfully test the connection to, so I know its running. I have added the requisite environment variables and configured my database.yml accordingly. To be clear, this is an application that used to work. I hadn't made any changes between the time I terminated the environment and when I went to re-deploy.
The root problem seems to be that nginx isn't being configured properly, as trying to access the server returns:
502 Bad Gateway
nginx/1.12.1
and when I check the nginx error.log its filled with errrors like this:
2018/09/19 14:12:35 [crit] 3069#0: *653 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.47.147, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/", host: "172.31.47.147"
Naturally, I googled my error, and found this stackoverflow post.
I've tried adding these suggested lines from the top-rated answer to my puma.rb
bind "unix:///var/run/puma/my_app.sock"
pidfile "/var/run/puma/my_app.sock"
Which caused no change at all.
I made sure to try the other suggestions, including having a direct look at the nginx configuration file. I did find that there's no upstream set up in the config. As best as I can see, the nginx aspect of the deployment pipeline is automated by Elastic Beanstalk so clearly something else I've set must be incorrect.
I've found that under no circumstances can I get the app to deploy using eb deploy I can only make changes by creating a new environment each time. I've recreated the app countless time, experimenting with different settings, versions of gems and packages, different ruby versions...etc. All in all, I still can't affect change on the error, I can't even get a new error! just the same PG:ConnectionBad or 502 bad gateway depending on if I look from console or browser.
From my googling I've come under the impression that this is related to puma in some regard but puma is a bit of a black box for me.
I'm feeling pretty lost here, I'd really appreciate any guidance you'd be willing to share. Feel free to ask for more info from any log or file, I'm happy to provide more detail. Thanks in advance!

Could be an RDS security group, is it configured to reach your elb?
You could also try cloning the db, to make sure its not sure weird database issue with old one, and try connecting to that.

So this wont be a very helpful answer as I never did resolve the problem. I didn't want to just leave this thread hanging though.
I ended up just creating a new rails environment, re-adding all the gems and porting my controllers/views/models/routes. Once I did that I was able to deploy without issue.
I can confirm that the issue wasn't with the security groups or the database itself. The fresh rails app was able to access the RDS instance without issue.
Thank you all for your comments and attemtps to help, it is much appreciated!

Related

How to edit bad gateway page on traefik

I want to edit the Bad Gateway page from traefik to issue a command like
docker restart redis
Does anyone have an idea on how to do this?
A bit of background:
I have a somewhat broken setup of Traefik v2.5 and Authelia on my development server, where sometimes I get a Bad Gateway Error when accessing a page. Usually this is fixed by clearing all sessions from redis. I tried to locate the bug, but the error logs aren't helpful and I don't have the time and skills to make the bug reproduceable or find the broken configuration. So instead I always use ssh into the maschine and reset redis manually

How to access phpmyadmin on DDEV Windows 10 pro localhost with SSL record too long error

I am using DDEv and Docker with Windows 10 pro to set up a localhost install of drupal 8.8 using Composer. I have set up and configured the local drupal installation (it is a fresh install) and it appears to be running correctly, but in the admin section of the drupal site I receive a warning to change write permissions of sites/default/settings.php.
I tried to change settings using Filezilla, but it appears that local files in Filezilla do not provide access to write permissions? When I right-click the file in Filezilla, no permissions option appears.
Following troubleshooting tips from ddev, I tried to access phpmyadmin at https://mysitename.ddev.site:8036
Instead of loading phpmyadmin, I got the following error message:
Secure Connection Failed
An error occurred during a connection to dmckimep.ddev.site:8036. SSL received a record that exceeded the maximum permissible length.
Error code: SSL_ERROR_RX_RECORD_TOO_LONG
The page you are trying to view cannot be shown because the authenticity of the received data could not be verified.
Please contact the website owners to inform them of this problem.
I've been searching around for a couple of hours now and do not find a solution to this. I ran ddev describe and all seems fine with the installation. The drupal site in the container seems to run okay. There are no port conflicts present so far as I have found, so I am not sure why I cannot get access to phpmyadmin.
I am a relative newbie in terms of skills, but have successfully maintained drupal 4-7 on localhost with XAMPP and my web host. Now I am wrestling with the move to drupal 8/composer/docker/ddev. Any suggestions would be much appreciated.
Thank you!
Update 2022-09-14: DDEV has had https support fpr PHPMyAdmin and MailHog for years now, ddev describe will show you the URL.
(Original answer) ddev's PHPMyAdmin connection doesn't support https, just http. You can find the links for both PHPMyAdmin and MailHog using ddev describe; both are http-only, as in your example, http://mysitename.ddev.site:8036. It would be possible to provide https URLs for PHPMyAdmin and MailHog, but nobody has ever asked for them, and there's no security reason to do so.
Note that the key reason for https on the actual project URL is because real projects run behind https and people need to see problems like mixed content during the development phase. But there's no such need for PHPMyAdmin. However, I'm sure if people ever want it, we'll do it, it's not hard to do.
Just as a general add on, after ddev start you can run ddev launch -p in order to open PHPMyAdmin for the current project database in the browser.

How to debug ElasticBeanstalk error "X% of the requests are failing with HTTP 5xx"

My problem is similar to AWS: None of the Instances are sending data but has a slightly different error message.
I have a Rails application running on ElasticBeanstalk, and it appears to be running correctly. Periodically, Enhanced Health Monitoring sends me error messages such as:
Environment health has transitioned from Ok to Degraded. 20.0 % of the
requests are failing with HTTP 5xx.
where the percentage varies up to 100%. Even though I've made no changes, a minute later I get a followup message telling me that everything is back to normal:
Environment health has transitioned from Degraded to Ok.
I've downloaded the full logs from ElasticBeanstalk but I don't know exactly where to look (there are around 20 different log files in various directories).
I'm currently using the free AWS tier with the smallest instances of database, server, etc. Could this be the cause? Which of the log files should I be looking in, and what should I be looking for?
I run rails apps on Elastic Beanstalk and have found it helpful to think about Beanstalk as a computer (in this case an Amazon EC2 instance) running your rails app and a web server (either Passenger or Puma). When you get a 500 error, it could be because your rails app didn't properly deploy–in which case Passenger or Puma will return an error—or your app is deployed properly but encountered an error just like it might on your local machine.
In either case, to diagnose an error, download the full logs from your AWS console (open the correct app environment and then choose Logs > Request Logs > Full logs > Download). Deployment errors are harder to diagnose, but I recommend starting by looking in var-XX/logs/log/eb-activity.log. I suspect your error is coming from your rails app itself, in which case I recommend looking in var-XX/app/support/logs/passenger.log and production.log. To find a 500 error, search for "500 Internal" and then treat the error like you would any other rails error.
You can go to the EC2 instance and run the application just like you would run on your local machine and see the logs.
You can ssh into your EC2 instance using the command eb ssh and go to /opt/python/ directory (It will be different for Ruby or other programming languages).
/opt/python/run is the directory where you will find the version of your application which is run from the EC2 instance. Look for the directory venv and app inside run directory.
Note: The above folder structure is for Python but a similar folder structure post deployment can be found for any other programming language. Just look for the standard directory structure for the deployment environment for your programming language.
For Python:
/opt/python: Root of where you application will end up.
/opt/python/current/app: The current application that is hosted in the environment.
/opt/python/on-deck/app: The app is initially put in on-deck and then, after all the deployment is complete, it will be moved to current. If you are getting failures in your container_commands, check out out the on-deck folder and not the current folder.
/opt/python/current/env: All the env variables that eb will set up for you. If you are trying to reproduce an error, you may first need to source /opt/python/current/env to get things set up as they would be when eb deploy is running.
/opt/python/run/venv: The virtual env used by your application; you will also need to run source /opt/python/run/venv/bin/activate if you are trying to reproduce an error.
I know it is a little late but I wanted to comment the trick I use to find the error, I use to connect via ssh and then, once in the app I try to enter "rails console" It uses to fail, but it shows normally the error you´re making. This little trick saved my life several times. Hope it helps!

Pulling down from github, unable to work locally with postgreSQL

I cloned a repository in a RoR app using postgres but I'm unable to work locally. When I run the server I get this error:
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
I've tried many things: the password reset doesn't work because it's not recognizing as a user...I've tried adding the underscore before postgres user...sudo su doesn't work...help!
Ok, so after shuffling through the google return links for the original error message, I came across this: http://www.brentmc79.com/posts/psql-could-not-connect-oh-fuck-you
Very straightforward list of commands that shut down the database, force a reset, and then a manual restart...it will also return prompts that you'll get. Very helpful. I'm getting a CSS error now, but it looks like the database setup is ok. Thanks!

AWS OpsWorks: Successful Ruby on Rails default layer deployment shows 404 error in browser

Didn't deployed Rails application in AWS OpsWorks. Totally confused how to do this! Could anyone tell me how many instance required to deploy a RoR application?
I have created "Rails App Server" instance and it's looks fine. And here I used nginx and Unicorn as Rails Stack.
After that I've added a application from a git repo. Then deployed that application. And logs showing it's deployed successfully.
But in Public IP it's showing 404 Not Found Error - nginx!
Any help would be appreciable.
I had this issue, and it turned out that the default virtual host /etc/nginx/sites-enabled/000-default symbolic link was not getting removed during deployment.
The 404 was actually nginx looking for a public root of whatever was in the default virtual host, as seen in /var/logs/nginx/error.log
2015/12/23 21:46:05 [error] 3839#0: *15 "/var/www/nginx-default/index.html" is not found (2: No such file or directory), client: 127.0.0.1, server: rails-app1, request: "GET / HTTP/1.1", host: "localhost"
Worse, it comes back after every OpsWorks deployment. Removing that link fixed the problem for me.
To make the fix sustainable and automated, I added a custom recipe to the Deploy lifecycle event for the Rails App Server layer, called custom-cookbook::delete-default-site. The recipe looks like this:
file '/etc/nginx/sites-enabled/000-default' do
action :delete
end
service 'nginx' do
action :reload
end
Check process list for unicorn and its log files. Deployment doesn't fail for some reason if unicorn master process doesn't start.

Resources