I should be able to run a Rails app on a Raspberry Pi automatically on startup with minimal configuration, muss, or fuss.
- 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
orgem 'whenever', require: false
in your Gemfile - Install Nginx.
sudo apt-get install nginx
- 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. - 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
- Reboot and watch your server automatically become available on port 80! No need to directly deal with the root user or runlevels.