23

I have followed this link to configure nginx with puma but when I start the server with
bundle exec puma -e development -b unix:///var/run/my_app.sock
it throws Permission denied - "/var/run/my_app.sock" (Errno::EACCES) error.

but when I start the server with bundle exec puma -e development
it is started with tcp://0.0.0.0:9292

my_app.sock file does not exist in /var/run/

how do I start the server with unix socket and access the application through the domain name given in the my_app.conf file.

Can you please anyone help me?.

AATHITH RAJENDRAN
  • 4,689
  • 8
  • 34
  • 58
Thrikal Samy
  • 302
  • 1
  • 3
  • 12

1 Answers1

35

To start puma with socket binding just use /tmp directory:

bundle exec puma -e development -b unix:///tmp/my_app.sock

To access application through domain name you should use something like nginx and do configuration for it.

To install nginx in Ubuntu just run next command:

sudo apt-get install nginx

Run sudo nano /etc/nginx/sites-available/my_app.conf and place configuration below into this file (Ctrl + X, Y - to save changes):

upstream my_app {
  server              unix:///tmp/my_app.sock;
}

server {
  listen              *:80;
  server_name         my_app.com;

  access_log          /var/log/nginx/my_app-access.log;

  location /favicon.ico {
    root              /var/www/my_app/public/assets/favicon.ico;
    gzip_static       on;
    expires           max;
    add_header        Cache-Control public;
  }

  location / {
    root              /var/www/my_app/public;
    try_files         $uri @app;
    gzip_static       on;
    expires           max;
    add_header        Cache-Control public;
  }

  location @app {
    proxy_pass        http://my_app;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto http;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    proxy_next_upstream error timeout invalid_header http_502;
  }   
}

You should change /var/www/my_app and my_app.com to appropriate values.

Add symlink into enabled sites sudo ln -fns /etc/nginx/sites-available/my_app.conf /etc/nginx/sites-enabled/

Restart nginx: sudo service nginx restart.

Link your domain name to server IP (via hosts-file or DNS-provider).

Viacheslav Molokov
  • 2,534
  • 21
  • 20
  • Lol, my edit was rejected. There is wrong protocol (in `request` object will be port '443', protocol 'https://' and `.ssl?` true) needs to be changed `proxy_set_header X-Forwarded-Proto https;` to `proxy_set_header X-Forwarded-Proto $scheme;`. – Artem P Jul 09 '14 at 11:24
  • @2nd Thank you for your comment. Just fixed it. – Viacheslav Molokov Jul 30 '14 at 15:22
  • Also, in some distros (like Centos8), `systemd units` have a service setup option that instructs the system to create `private temporary files`, which means, only accessible by the same process. It may also be an issue. To prevent that, set `PrivateTmp=No` in the `[Service]` block of the service setup file. – Fernando Vieira Mar 04 '20 at 02:22