Question

Can't get unicorn to run - Ubuntu 20.04 Ruby on rails

Posted September 25, 2020 219 views
Nginx

I’m trying to get a simple ruby on rails app running. I created a droplet with

  • Ununtu 20.04
  • Trying to use NGINX/UNICORN
  • getting a 403/nginx/1.18.0 (Ubuntu) error
  • I set up /config/unicorn.rb
  • Permissions seem to be right for the shared/log shared/pid shared/socket folders
  • I disabled the UFW firewall for now

My unicorn.rb is a mess

# require 'rvm'
# set path to the application
app_dir = File.expand_path("../..", __FILE__)
app_path = File.expand_path(File.dirname(__FILE__) + '/..')
shared_dir = "#{app_dir}/shared"
working_directory app_dir
# require 'rvm'
# RVM.use_from_path! File.dirname(File.dirname(__FILE__))
gem_path = '/home/cgmadmin/.rvm/rubies/ruby-2.7.1/bin/ruby'
gem_home = '/home/cgmadmin/.rvm/rubies'
# Set unicorn options

shared_dir = "#{app_dir}/shared"
working_directory app_dir

# Set unicorn options
worker_processes 2
preload_app false
timeout 30

# Path for the Unicorn socket
listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64

# Set path for logging
stderr_path "#{shared_dir}/log/unicorn.stderr.log"
stdout_path "#{shared_dir}/log/unicorn.stdout.log"

# Set proccess id path
pid "#{shared_dir}/pids/unicorn.pid"

Some output from StdError

, [2020-09-25T03:55:20.548399 #107271]  INFO -- : unlinking existing socket=/home/site/christopherg/shared/sockets/unicorn.sock
I, [2020-09-25T03:55:20.549976 #107271]  INFO -- : listening on addr=/home/site/christopherg/shared/sockets/unicorn.sock fd=7
F, [2020-09-25T03:55:20.550193 #107271] FATAL -- : error adding listener addr=0.0.0.0:80
/home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/socket_helper.rb:165:in `bind': Permission denied - bind(2) for 0.0.0.0:80 (Errno::EACCES)
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/socket_helper.rb:165:in `new_tcp_server'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/socket_helper.rb:145:in `bind_listen'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/http_server.rb:243:in `listen'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/http_server.rb:878:in `block in bind_new_listeners!'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/http_server.rb:878:in `each'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/http_server.rb:878:in `bind_new_listeners!'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/http_server.rb:142:in `start'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/bin/unicorn:128:in `<top (required)>'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/bin/unicorn:23:in `load'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/bin/unicorn:23:in `<main>'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/bin/ruby_executable_hooks:24:in `eval'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/bin/ruby_executable_hooks:24:in `<main>'
I, [2020-09-25T04:19:01.549786 #107699]  INFO -- : unlinking existing socket=/home/site/christopherg/shared/sockets/unicorn.sock
I, [2020-09-25T04:19:01.551237 #107699]  INFO -- : listening on addr=/home/site/christopherg/shared/sockets/unicorn.sock fd=7
F, [2020-09-25T04:19:01.551475 #107699] FATAL -- : error adding listener addr=0.0.0.0:80
/home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/socket_helper.rb:165:in `bind': Permission denied - bind(2) for 0.0.0.0:80 (Errno::EACCES)
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/socket_helper.rb:165:in `new_tcp_server'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/socket_helper.rb:145:in `bind_listen'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/http_server.rb:243:in `listen'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/http_server.rb:878:in `block in bind_new_listeners!'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/http_server.rb:878:in `each'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/http_server.rb:878:in `bind_new_listeners!'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/lib/unicorn/http_server.rb:142:in `start'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/gems/unicorn-5.7.0/bin/unicorn:128:in `<top (required)>'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/bin/unicorn:23:in `load'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/bin/unicorn:23:in `<main>'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/bin/ruby_executable_hooks:24:in `eval'
    from /home/cgmadmin/.rvm/gems/ruby-2.7.1/bin/ruby_executable_hooks:24:in `<main>'

I do not have this tied to a domain yet.

etc/nginx/sites-enabled

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /home/site/christopherg;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;



I’m not sure if I need to start unicorn manually with something like

bundle exec unicorn -c config/unicorn.rb -E  production -p 80

The following looks wrong to me.Sometimes I see unicorn processes running

 ps aux | grep unicorn
cgmadmin  107780  0.0  0.0   8160   728 pts/0    S+   04:31   0:00 grep --color=auto unicorn

I think I need to generate the secret key base but I didn’t think that would cause this issue. If I try to run webbrick with the following command I get an error about the secret keybase

RAILS_ENV=production rails server --binding=167.71.190.163
Internal Server Error
Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`
WEBrick/1.6.0 (Ruby/2.7.1/2020-03-31) at 167.71.190.163:3000
1 comment
  • added the secret key so I can get the server to run with webbrick with

    RAILS_ENV=development rails server –binding=167.71.190.163. So the rails server will run in development with webbrick but I can’t get it to run with nginx/unicorn..

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hey there @cgmendla,

Happy to hear that you’ve got the Rails server running by adding RAILS_ENV=development rails server –binding=167.71.190.163.

What is the exact error that you get when you try to run Nginx?

Note that you can’t have the two services running on the same port, for example, you should have Nginx running on ports 80 and 443 and Rails on a different one.

One more thing that I could suggest is going through this answer here on how to troubleshoot common Nginx problems:

https://www.digitalocean.com/community/questions/how-to-troubleshoot-common-nginx-issues-on-linux-server

Hope that this helps!
Regards,
Bobby

Submit an Answer