Server Setup for Rails 7 app with nginx and puma

152 Views Asked by At

I am really new to server configuration. I have a rails application locally and I have access to an amazon EC2 server instance. I am required to use nginx for web server and puma for application server. Can someone kindly explain to me in detail (file creation, directory, command etc) how I can make that application up and running for public access via the domain?

My rails app directories are:

$~ server/user/home/apps/myapp/shared/...
$~ server/user/home/apps/myapp/current/...

I have tried capistrano. But no matter which ever tutorial I follow, I always end up having .../shared/tmp/sockets/puma.myapp.sock failed (2: No such file or directory) while connecting to upstream... error.

2

There are 2 best solutions below

0
Fede Gratti On

here are the steps that worked for me:

Install Nginx

  1. sudo apt install nginx
  2. sudo ufw app list
  3. sudo ufw allow 'Nginx HTTP'

Setup Server

  1. sudo nano /etc/nginx/sites-available/appname.
  2. Copy the following template into the file and replace appname with the name of your app:
server {
    listen 80;

    root /home/apps/myapp/public;
    server_name appname.com www.appname.com;
    index index.htm index.html;

        location ~ /.well-known {
            allow all;
        }

        location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        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 $scheme;
    }
}
  1. sudo ln -s /etc/nginx/sites-available/repuestosgonnet /etc/nginx/sites-enabled/
  2. sudo nano /etc/nginx/nginx.conf
  3. Uncomment this line: server_names_hash_bucket_size 64;
  4. Optional: Uncomment this line: server_tokens off;
  5. Test syntax: sudo nginx -t
  6. Restart Nginx: sudo systemctl restart nginx

I hope that works for you!

Let me know if you have any questions.

0
gdurelle On

You need to create 2 systemd service files so that it launches puma for you:

appname.service:

[Unit]
Description=Puma HTTP Server for appname
After=network.target
Requires=appname.socket

[Service]
Type=simple
Environment=RAILS_ENV=production
WorkingDirectory=/user/home/apps/myapp/current/
ExecStart=/user/home/apps/.rbenv/shims/puma -e production -C /user/home/apps/myapp/shared/puma.rb /user/home/apps/myapp/current/config.ru
ExecStop=/user/home/apps/.rbenv/shims/puma -e production -C /user/home/apps/myapp/shared/puma.rb stop
ExecReload=/user/home/apps/.rbenv/shims/puma -e production -C /user/home/apps/myapp/shared/puma.rb phased-restart

PIDFile=/user/home/apps/myapp/shared/tmp/pids/puma.pid

Restart=always
RestartSec=8
KillMode=process
SyslogIdentifier=puma

[Install]
WantedBy=multi-user.target

appname.socket:

[Unit]
Description=Puma HTTP Server Accept Sockets

[Socket]
ListenStream=0.0.0.0:9294
ListenStream=0.0.0.0:9295

NoDelay=true
ReusePort=true
Backlog=1024

[Install]
WantedBy=sockets.target

Then you can relaunch systemd loader

systemctl daemon-reload

And enable the files

systemctl enable myappli_puma_env.service myappli_puma_env.socket

And finally:

cap puma:start