How to deploy a Rails app to Digital Ocean’s One-Click App with Ubuntu 18

  • Create the droplet.
  • Log in through SSH as root.
  • Change the ‘rails’ user password: passwd rails
  • Give it root privileges: usermod -aG sudo rails
  • Switch to that user: sudo -i -u rails
  • Clone your repo right there: git clone URL
  • bundle install
  • Export DB pass: export APP_DATABASE_PASSWORD=”your_pass” (This only for running the server in the shell.
    (If Rails 5.2 be sure to copy the master.key, then edit the credentials with EDITOR=nano rails credentials:edit and put your db pass there)
  • Create DB: RAILS_ENV=production rake db:create
  • Migrations: RAILS_ENV=production rake db:migrate
  • Pre-compile assets if necessary: RAILS_ENV=production rake assets:precompile
    (Install Yarn as it tells you and re-run)
  • Configure nginx to point to your project changing the root variable here: sudo nano /etc/nginx/sites-available/rails
  • Restart it: sudo service nginx restart
  • Add puma to Gemfile and install if it’s not there.
  • Add config/puma.rb with this:
    threads_count = ENV.fetch(“RAILS_MAX_THREADS”) { 5 }
    threads threads_count, threads_count
    port ENV.fetch(“PORT”) { 3000 }
    environment ENV.fetch(“RAILS_ENV”) { “production” }
    plugin :tmp_restart
  • Edit the WorkingDirectory for the systemd puma service: nano /etc/systemd/system/rails.service
  • Generate secret: RAILS_ENV=production rake secret
    (Skip if Rails 5.2)
  • Add ENV variables to puma service: sudo systemctl edit rails
    [Service]
    Environment=”APP_DATABASE_PASSWORD=your_pass"
    Environment=”RAILS_SERVE_STATIC_FILES=false”
    Environment=”SECRET_KEY_BASE=your_secret” (Skip if Rails 5.2)
  • Restart Puma: sudo systemctl restart rails

Checking logs

  • tail -f log/production.log -n 90
  • tail -f /var/log/nginx/error.log -n 90
  • sudo journalctl -u rails -b

Domain

  • Register domain at freenom.com
  • Put DO’s nameservers.
  • Go to DO, your droplet, Networking, add the domain and the A records @ and www.

SSL

  • Install Certbot: sudo add-apt-repository ppa:certbot/certbot
  • Certbot’s Nginx package: sudo apt install python-certbot-nginx
  • Adding domain to Nginx conf: sudo nano /etc/nginx/sites-available/YOUR_SITE:
    Where it says server_name add both yourdomain.com and www.yourdomain.com
  • Restart nginx: sudo systemctl reload nginx
  • Allow HTTPS through firewall: 
    sudo ufw allow “Nginx Full”
    sudo ufw delete allow “Nginx HTTP”
  • Obtain certificate: 
    sudo certbot — nginx -d yourdomain.com -d www.yourdomain.com
  • Verify Certbot Auto-renewal: sudo certbot renew — dry-run