Unicorn service not starting

August 5, 2017 110 views
Nginx Ruby on Rails Getting Started Ubuntu 16.04

I am following this tutorial of DigitalOcean. I have somehow managed to setup everything, but I am still getting 502 Bad Gateway error when I access the server IP. So, I dug through the logs and found that an "(111: Connection Refused) occurred while connecting to upstream" in nginx/error.log file. After that I ran

systemctl status unicorn

I got the following:

● unicorn.service - DigitalOcean Rails One-Click Application
   Loaded: loaded (/etc/systemd/system/unicorn.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Sat 2017-08-05 01:52:18 UTC; 8s ago
  Process: 22871 ExecStart=/bin/bash /home/rails/project/.unicorn.sh (code=exited, status=1/FAILURE)
 Main PID: 22871 (code=exited, status=1/FAILURE)

Aug 05 01:52:18 paxsafe systemd[1]: unicorn.service: Main process exited, code=exited, status=1/FAILURE
Aug 05 01:52:18 paxsafe systemd[1]: unicorn.service: Unit entered failed state.
Aug 05 01:52:18 paxsafe systemd[1]: unicorn.service: Failed with result 'exit-code'.

The code in .unicorn.sh file:

#!/bin/bash
# This file is meant to be executed via systemd.
source /usr/local/rvm/scripts/rvm
source /etc/profile.d/rvm.sh
export ruby_ver=$(rvm list default string)

export CONFIGURED=yes
export TIMEOUT=50
export APP_ROOT=/home/rails/project
export RAILS_ENV="production"
export GEM_HOME="/home/rails/project/vendor/bundle"
export GEM_PATH="/home/rails/project/vendor/bundle:/usr/local/rvm/gems/${ruby_ver}:/usr/local/rvm/gems/${ruby_ver}@global"
export PATH="/home/rails/project/vendor/bundle/bin:/usr/local/rvm/gems/${ruby_ver}/bin:${PATH}"

# Passwords
export SECRET_KEY_BASE=[random_secret]
export APP_DATABASE_PASSWORD=[random_db_password]

# Execute the unicorn process
/home/rails/project/vendor/bundle/ruby/2.4.0/bin/unicorn \
        -c /etc/unicorn.conf -E production --debug

/etc/unicorn.conf file:

listen "unix:/var/run/unicorn.project.sock"
worker_processes 4
user "rails"
working_directory "/home/rails/project"
pid "/var/run/unicorn.project.pid"
stderr_path "/var/log/unicorn/unicorn.error.log"
stdout_path "/var/log/unicorn/unicorn.info.log"

/etc/default/unicorn file:

CONFIGURED=yes

TIMEOUT=60

APP_ROOT=/home/rails/project

CONFIG_RB=/etc/unicorn.conf

PID=/var/run/unicorn.project.pid
RAILS_ENV="production"
UNICORN_OPTS="-D -c $CONFIG_RB -E $RAILS_ENV"

PATH=/usr/local/rvm/rubies/ruby-2.4.0/bin:/usr/local/sbin:/usr/bin:/bin:/sbin:/usr/local/rvm/bin:/usr/local/rvm/gems/ruby-2.4.0@global/bin:/usr/local$
export GEM_HOME=/usr/local/rvm/gems/ruby-2.4.0
export GEM_PATH=/usr/local/rvm/gems/ruby-2.4.0:/usr/local/rvm/gems/ruby-2.4.0@global
DAEMON=/usr/local/rvm/gems/ruby-2.4.0/bin/unicorn

export SECRET_KEY_BASE=[random_secret]$
export APP_DATABASE_PASSWORD=[random_db_password]

/etc/nginx/sites-enabled/rails

upstream app_server {
    server unix:/var/run/unicorn.project.sock fail_timeout=0;
}

server {
    listen   80;
    root /home/rails/project/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;
    }
}
Be the first one to answer this question.