Skip to content

Instantly share code, notes, and snippets.

@Ravenstine
Last active March 6, 2016 22:07
Show Gist options
  • Save Ravenstine/223262659c52d8b387ec to your computer and use it in GitHub Desktop.
Save Ravenstine/223262659c52d8b387ec to your computer and use it in GitHub Desktop.
Run a Rails app on startup in Raspbian/Debian Linux

Judgment

I should be able to run a Rails app on a Raspberry Pi automatically on startup with minimal configuration, muss, or fuss.

Instructions

  1. Install the Whenever gem for writing a schedule to Crontab. (this is actually optional since you can edit crontab manually, but Whenever is convenient for having the schedule stored in the Rails project) gem install whenever or gem 'whenever', require: false in your Gemfile
  2. Install Nginx. sudo apt-get install nginx
  3. In your Nginx configuration(probably /etc/nginx/nginx.conf), make sure the http section includes other configuration files from the sites-enabled folder. Add the line include /etc/nginx/sites-enabled/*; to the http section if it isn't there.
  4. Create /etc/nginx/sites-enabled/default and add server configuration for your Rails app. Here is an example file:
    upstream rails_server {
      server 0.0.0.0:3000;
    }
    
    server {
      listen 80;
    
      location / {
        root /home/pi/blog/public;
        try_files $uri @missing;
      }
    
      location @missing {
        proxy_pass http://rails_server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
      }
    }
    
5. Create the `config/schedule.rb` file in your Rails project and add a line like this:
 ```ruby
every '@reboot' do
   command "source /home/pi/.rvm/scripts/rvm && cd /home/pi/blog && bundle exec rails s -b 0.0.0.0 -p 3000 -e production -d &"
end

^^^ This is assuming that you are using RVM. You might not need to include that first source command, but I included it for the heck of it.

Then run whenever -w

  1. Reboot and watch your server automatically become available on port 80! No need to directly deal with the root user or runlevels.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment