Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save omgreenfield/673ed5958a463e11bb190b5b49d9f245 to your computer and use it in GitHub Desktop.
Save omgreenfield/673ed5958a463e11bb190b5b49d9f245 to your computer and use it in GitHub Desktop.
Install Rails, Nginx, Puma, and an SSL cert with Let's Encrypt

Pre-requisites

  • Ubuntu 16.04
  • Custom DNS name
  • Rails app working in RAILS_ENV=production on ubuntu box (i.e. can run RAILS_ENV=production rails s and go to the app)

Puma

Put in file /etc/systemd/system/puma.service

[Unit]
Description=Puma HTTP Server
After=network.target
[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu/my-cool-app
Environment=PUMA_DEBUG=1
ExecStart=/bin/bash -lc 'RAILS_ENV=production bundle exec puma -C /home/ubuntu/my-cool-app/config/puma.rb -b unix:/home/ubuntu/my-cool-app/tmp/sockets/puma-my-cool-app.sock /home/ubuntu/my-cool-app/config.ru'
TimeoutSec=15
[Install]
WantedBy=multi-user.target

Run

sudo systemctl daemon-reload
sudo systemctl enable puma.service
sudo systemctl start puma.service

Nginx

Run

sudo apt-get update
sudo apt-get install curl git-core nginx -y

Put in file /etc/nginx/sites-enabled/my-cool-app

upstream puma {
  server unix:///home/ubuntu/my-cool-app/tmp/sockets/puma-my-cool-app.sock;
}

server {
  listen 80 default_server deferred;
  # server_name example.com;

  root /home/ubuntu/my-cool-app/public;
  access_log /home/ubuntu/my-cool-app/log/nginx.access.log;
  error_log /home/ubuntu/my-cool-app/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}

Run sudo service nginx restart

Let's Encrypt

Run

sudo git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt
/opt/letsencrypt/letsencrypt-auto --nginx -d www.my-cool-dns-name.com

Using force_ssl

https://seaneshbaugh.com/posts/configuring-nginx-and-unicorn-for-force_ssl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment