hungle00 / hungle00.github.io

My Personal Blog
https://hungle00.github.io/
1 stars 0 forks source link

Nginx and Sinatra app on AWS Lightsail #9

Open hungle00 opened 1 year ago

hungle00 commented 1 year ago

1. Config nginx to serve static file

Create the directory for your_domain as follows, and create file index.html in this path.

sudo mkdir -p /var/www/my_way/html  

Next, let’s enable the file by creating a link from it to the sites-enabled directory

sudo ln -s /etc/nginx/sites-available/my_way /etc/nginx/sites-enabled/

In order for Nginx to serve this content, we must modify the configuration file. Instead of modifying /etc/nginx/nginx.conf directly, we can make a new path at /etc/nginx/sites-available/

# /etc/nginx/sites-available/my_way
server {
        listen 80;
        listen [::]:80;

        root /var/www/my_way/html;
        index index.html index.htm index.nginx-debian.html;

        server_name your_ip;

        location / {
                try_files $uri $uri/ =404;
        }
}

Test nginx config and restart:

sudo nginx -t
sudo systemctl restart nginx

Check log nginx:

tail -f /var/log/nginx/error.log  # error log
tail -f /var/log/nginx/access.log # success log

2. Sinatra app with puma and nginx

First, let's build simple Sinatra app. Create a directory for your app, maybe named sinatra-my-way

# app.rb
require 'sinatra'

class App < Sinatra::Base
  get '/' do
    'Hello Frank!'
  end
end

# config.ru
require_relative 'app'

run App

Next, create Puma config file puma.rb and add below content:

root = "#{Dir.getwd}"

activate_control_app "tcp://127.0.0.1:9293"
bind "unix://#{root}/tmp/puma.sock"
pidfile "#{root}/tmp/pids/puma.pid"
rackup "#{root}/config.ru"
state_path "#{root}/tmp/pids/puma.state"

Create some supporting directories:

mkdir -p tmp/pids public

Run Sinatra app with puma config by this command:

bundle exec puma --config puma.rb

Finally, we still need to integrate it with NginX to make it live. Modify /etc/nginx/sites-available/my_way

upstream sinatra {
        server unix:///home/ubuntu/sinatra-my-way/tmp/puma.sock;
}

server {
        listen 80;
        root /home/ubuntu/sinatra-my-way/public;  # path to your app
        server_name 13.250.4.60;  # your domain or ip

        location / {
                try_files $uri @puma;
        }

        location @puma {
                include proxy_params;
                proxy_pass http://sinatra;
        }
}

Restart Nginx sudo systemctl restart nginx. Now your Sinatra app is running on your domain.

References

hungle00 commented 1 year ago

https://devpress.csdn.net/cicd/62ec803b19c509286f41727d.html