Rails application root not working - Rails One-Click droplet

July 6, 2018 283 views
Ruby on Rails One-Click Install Apps Nginx Deployment Ubuntu 16.04

I have been trying to deploy my rails application for a good part of this week now and have tried many, many, many times to try and get this working. I either hit a 502 bad gateway, a 504 gateway timeout or the page simply cannot be reached.

I have done the following already:

  • cloned my application from BitBucket into /home/rails/my-app-name
  • changed my DNS records so that my domain points to my droplet IP (using my domain name works for the default "rails_project"
  • ran nano /etc/unicorn.conf and changed the value to working_directory "/home/rails/my-app-name"
  • ran nano /etc/nginx/sites-enabled/rails and changed the value to root /home/rails/my-app-name/public;
  • ran service nginx reload && service unicorn restart

My favicon loads and I see that but the rails application just doesn't load.
My rails app works in production on a free Heroku dyno so I'm confident it's not that

Thank you for any help you can provide, it is greatly appreciated!

Running nginx -t outputs this:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

This is my error log: [error] 13744#13744: *19 upstream timed out (110: Connection timed out) while reading response header from upstream, client: [IP Address], server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/run/DigitalOceanOneClick/unicorn.sock/", host: "[My Domain Name]"

1 Answer

Hello friend!

That is a tough one. Based on the steps you've described, the software stack should function. I just copied your steps with a sample app and confirmed that it did operate, just to be sure. While that doesn't necessarily say there is something wrong with your app, that is the direction you are going to want to look in as we narrow it down. It could be something environmental that is different in this server than another, and that being something that your app requires to function properly, for example. Such a thing like that would be more likely to reveal itself on the app side rather than the web server side.

My first thought is check /var/log/unicorn/unicorn.log and see if you see any errors which may correlate to this. I suspect that you will. If not, however, you may want to build some kind of error logging into the application's launch that might expose any errors that are not being seen elsewhere. I realize that's a pretty vague and high level ask, but that is where my instinct is going.

Feel free to share any findings here. If I cannot interpret them in a helpful way, perhaps someone else can. I'm always happy to try :)

Kind Regards,
Jarland
Platform Support Lead

  • Thanks for getting back to me on this, I've checked the logs there and I've noticed the following few errors:

    Exception `Bundler::GemNotFound' at /home/rails/my-app-name/config/environment.rb:2 - Could not find rake-12.3.0 in any of the sources
    

    I noticed it was a recurring theme of rake-12.3.0 not being found in many different instances.

    I ran gem install bundler and gem install rake but it installed rake-12.3.1

    I also ran bundle update but it still doesn't run

    How would I go about handling this?
    Thank you so much for the help so far!

    • Hey there,

      The help is no problem at all. We're always happy to assist in any way we can. :)

      With regards to the issue at hand, I think there are a few possible culprits/solutions. I'll go ahead and lay out the information I've been able to collect to help you make an informed decision on what to do moving forward:

      1. It sounds like the gemfile is pointing to an older version of rake. Theoretically, this should be updatable with bundle update rake. https://stackoverflow.com/a/6080131

      2. If the above command is not updating the gemfile, it may be best to open the gemfile manually and point to the newer version of rake. You can either point to the specific version, or just modify your gemfile to have something like this gem 'rake', ' >12.0'.

      3. If updating the gemfile isn't an option or is not working, you can uninstall and re-install a specific version of rake. https://stackoverflow.com/a/49522469

      One of those methods should hopefully help get the installed rake version and the version ruby is looking for in-order. If not, would you mind sending back the outputs of these commands:

      ruby -v
      bundle -v
      gem env

      As well as the contents of your gemfile via a paste: paste.ubuntu.com. That way we can review the additional information and try to offer more specific troubleshooting advice. :)

      Regards,
      Ethan Fox
      Developer Advocate - DigitalOcean

      • Hey, thanks. I tried these out and it said Using rake 12.3.1.

        Here is the pastebin with all requested: https://paste.ubuntu.com/p/KFnnhh3MVY/

        After seeing it was using the rails_project directory, I deleted it, ran bundle install and gem update again but that really didn't do anything it seems.

        I don't know how to change the directory for gems if that is the problem.

        Thanks again!

        • Hey there,

          The gemfile doesn't have a specific rake version mentioned. Would you mind trying to add gem 'rake', ' >12.0' to the file to see if that helps it find the proper version?

          If not, you'll want to uninstall rake again and reinstall it pointing to a specific version :

          gem uninstall rake 12.3.1
          gem install rake 12.3.0

          I should mention in the original error I saw:

          /home/rails/my-app-name/config/environment.rb:2

          This file may be where 12.3.0 is defined if not in your gemfile. Updating that to 12.3.1 could help as well if installing the older version fails for some reason.

          Regards,
          Ethan Fox
          Developer Advocate - DigitalOcean

          • Thank you, Ethan. I did all of this and it seems to have gotten me past the 504 Time-out error but now onto a 502 bad gateway error. I ran cat /var/log/nginx/error.log and it said:

            [crit] 1484#1484: *9 connect() to unix:/run/DigitalOceanOneClick/unicorn.sock failed (2: No such file or directory) while connecting to upstream, client: [IP Address], server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/run/DigitalOceanOneClick/unicorn.sock:/", host: "[My Domain Name]"
            

            I ran into this problem before and followed some advice:
            As root I ran mkdir /run/DigitalOceanOneClick then chown rails /run/DigitalOceanOneClick but it said "No such file or directory".

            I made the directory again then ran ls -la /run/DigitalOceanOneClick and got total 0
            drwxr-xr-x 2 root root 40 Jul 6 19:10 .
            drwxr-xr-x 25 root root 920 Jul 6 19:10 ..
            but it seems to be deleted moments after I do this.

            As for your update on the error you spotted, I checked that and it was just a mailer with the following code:

            # Load the Rails application.
            require_relative 'application'
            
            # Initialize the Rails application.
            Rails.application.initialize!
            
            ActionMailer::Base.smtp_settings = {
              :address        => 'smtp.sendgrid.net',
              :port           => '578',
              :authentication => :plain,
              :user_name      => ENV['SENDGRID_USERNAME'],
              :password       => ENV['SENDGRID_PASSWORD'],
              :domain         => '[My Domain Name]',
              :enable_startstls_auto => true
            }
            
          • Hey there,

            It was no problem at all! I'm glad to hear that issue is now resolved, now we can work on this new error!

            It looks like Unicorn may be misconfigured, would you mind checking the configuration file /etc/unicorn.conf and configuring it as needed? Once that is done just restart the service service unicorn restart.

            I say this because in the error it is showing things like client: [IP Address] and host: "[My Domain Name] instead of the actual IP / domain. This makes me think defaults may have been left in place.

            If that still fails could you check the log file /var/log/unicorn/unicorn.log to see if it provides any additional insights into what is going on when the sock file fails to create?

            Regards,
            Ethan Fox
            Developer Advocate - DigitalOcean

          • I can't reply to your last message for some reason, so I'm here. I checked the configuration of my /etc/unicorn.conf file and it reads the following:

            listen "unix:/run/DigitalOceanOneClick/unicorn.sock"
            worker_processes 4
            user "rails"
            working_directory "/home/rails/transverse-audio"
            pid "/run/DigitalOceanOneClick/unicorn.pid"
            stderr_path "/var/log/unicorn/unicorn.log"
            stdout_path "/var/log/unicorn/unicorn.log"
            

            as for the [IP Address] and [My domain name], I just replaced the ones on file for privacy.

            The real name of the app directory is transverse-audio instead of my-app-name and my domain name is transverseaudio.com. I have been using transverse-audio and transverseaudio.com throughout my droplet.

            Here is my /etc/nginx/sites-enabled/rails file, just in case it is of interest.

            upstream app_server {
                server unix:/run/DigitalOceanOneClick/unicorn.sock fail_timeout=0;
            }
            
            server {
                listen   80;
                root /home/rails/transverse-audio/public;
                server_name _;
                index index.htm index.html;
            
                location / {
                        try_files $uri/index.html $uri.html $uri @app;
                }
            
                location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
                                try_files $uri @app;
                        }
            
                 location @app {
                        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;
                }
            }
            

            I have tried using my droplet's IP, transverseaudio.com, and transverseaudio.com www.transverseaudio.com for the value of server_name _; in addition to trying the underscore.

            Still, the page is giving me a 502 error.

            Any clue as to why all of this would be happening?

            I truly do appreciate you sticking with me on this, Ethan.

          • Hey there,

            It's been no problem. I'm happy to help in any way I can!

            Could you restart the Unicorn service: service unicorn restart then check the log: tail -20 /var/log/unicorn/unicorn.log to see why the socket file wouldn't be created? You can send that output back again similarly on paste.ubuntu.com if you aren't sure what an error means. If there is no error there, could you check /var/log/syslog and /var/log/nginx/error.log for any additional hints as to why the socket file would be having issues coming up?

            Regards,
            Ethan Fox
            Developer Advocate - DigitalOcean

          • As per request, here is the pastebin with all of the code you've requested from logs after calling service unicorn restart: https://paste.ubuntu.com/p/YSXRzkxhHW/

            The nginx errors are the same, although the host: changes from "transverseaudio.com" to my droplet IP with :80 at the end sometimes.

            The only other things I think might be important is that I developed this rails app on a windows computer and I'm using git-bash to interact with the droplet. I also noticed that after deleting the "railsproject" it came back with only "vendor" remaining, probably since I ran bundle install. Comparing the two vendor directories I noticed the railsproject one has a folder called "x86_64-linux" but the one I have in my rails app doesn't, it only has "assets".

          • Hey there,

            Per this community post answer's instruction: https://www.digitalocean.com/community/questions/rails-one-click-times-out-after-a-git-pull-containing-four-files-accessing-my-site-is-a-504-gateway-timeout?answer=36300 would you mind changing the logging level of Unicorn? The issue presented there actually seems quite similar to yours, which is interesting but I wasn't able to find a full solution there.

            Then, just restart the service again (service unicorn restart) and check the logs tail -20 /var/log/unicorn/unicorn.log or, if you have a custom log in the project tail -20 /project/directory/log/unicorn.log.

            You can send back those outputs again via paste, or simply here in the community if they do not feel too large and cumbersome. :)

            Regards,
            Ethan Fox
            Developer Advocate - DigitalOcean

          • Hey, thanks for taking the time to look for those other questions. I followed it and went into my production.rb file and made a few changes to my logging:

            config.consider_all_requests_local       = true  #was false
            
            if ENV["RAILS_LOG_TO_STDOUT"].present?
                logger           = ActiveSupport::Logger.new(STDOUT)
                logger.formatter = config.log_formatter
            # config.logger = ActiveSupport::TaggedLogging.new(logger) #commented this out
            end
            
            
            config.logger = Logger.new(STDOUT)
            config.logger.level = Logger.const_get('INFO')
            config.log_level = :info  #was :debug (i commented the other one out)
            

            (I tried :debug too but that didn't seem to change anything)
            Here is my unicorn.log - https://paste.ubuntu.com/p/C63JQ5QvRK/

            I tried tail -20 /transverse-audio/log/unicorn.log but No such file or directory.
            (I also tried the exact command you gave me at first, not realizing it was a placeholder but that didn't work.)

            I went through with updating the rack etc. hoping that may have done good, but it seems like it didn't do anything either.

            I even tried to gem install unicorn thinking that's what the error was caught up on but that didn't seem to do anything.

          • Hey there,

            Apologies for the reply time on this! I'm doing my best to keep up with this thread while staying on top of other things at the job. :)

            With regards to the issue, is there any log in /transverse-audio/log/? You can run ls -lah /traverse-audio/log/ to find out. If so, would you mind sending back the recent contents of that log (perhaps called production.log?).

            If you aren't able to find that log file, is there any other error further up in the primary log that would indicate any missing gems, etc. similar to the error seen in this community post: https://www.digitalocean.com/community/questions/rails-one-click-times-out-after-a-git-pull-containing-four-files-accessing-my-site-is-a-504-gateway-timeout?comment=60640?

            Regards,
            Ethan Fox
            Developer Advocate - DigitalOcean

          • Hey Ethan, don't worry at all, the fact that you're helping me is appreciated so much.

            After running ls -lah /traverse-audio/log/ it said there was no such file or dir. I navigated there manually and typed in ls -lah and got this:

            total 8.0K
            drwxrwxr-x  2 rails rails 4.0K Jul  6 14:27 .
            drwxrwxr-x 13 rails rails 4.0K Jul  6 18:54 ..
            -rw-rw-r--  1 rails rails    0 Jul  6 14:27 .keep
            

            I went through and tried to do tail -20 /transverse-audio/log/unicorn.log but now it's saying no such file or directory either.

            Is there any chance that someone at DigitalOcean can go into my droplet and see the code themselves and try to get this all sorted out? I know it's a lot to ask for but I really don't know what else to do.

            If you think you may have any more ideas for solving this, I am happy to keep trying. I just don't want to take up all of your time.

Have another answer? Share your knowledge.