Unicorn service not starting

Posted August 5, 2017 10.3k views
Ruby on RailsNginxGetting StartedUbuntu 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/ (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 file:

# This file is meant to be executed via systemd.
source /usr/local/rvm/scripts/rvm
source /etc/profile.d/
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/"
stderr_path "/var/log/unicorn/unicorn.error.log"
stdout_path "/var/log/unicorn/"

/etc/default/unicorn file:






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

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


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;

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.

Submit an Answer
1 answer

For me the solution was to bundle install –deployment so the unicorn server is installed in the vendor folder of the new rails project.
And then you need to point the paths in the file to the correct place. In my case there where to extra folders added to the path. /ruby/2.4.0 was created inside vendor/bundle so in change to:
export GEM_HOME=“/home/rails/project/vendor/bundle/ruby/2.4.0” and so on for the other paths too.

Hope it helps!