0

Every time when I restart a linux box, I have to run rails s to start a rails app in dev env.

Is there a way to start a given rails app automatically when linux restarted?

I assume the solution is close to running an app in a production env.

I have never run an app in a production site.

UPDATE 1

I've googled more about the issue and found something close to me How can I make “rc.local” run on startup?.

My /etc/rc/local :

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/home/askar/.rvm/rubies/ruby-2.1.2/bin/ruby /home/askar/work/rails/smn/bin/rails s

exit 0

$ ls -al /etc/rc.local

-rwxr-xr-x 1 root root 423 Jul 23 12:41 /etc/rc.local

I'm able to run by:

$/etc/rc.local &

But when I try to run so that I would startup automatically on next boot:

sudo /etc/init.d/rc.local start

It's giving me the error:

Could not find rake-10.2.1 in any of the sources
Run `bundle install` to install missing gems.
Community
  • 1
  • 1
Askar
  • 5,784
  • 10
  • 53
  • 96
  • This tutorial is very nice: [How To Deploy Node.js Applications Using Systemd and Nginx](https://www.digitalocean.com/community/tutorials/how-to-deploy-node-js-applications-using-systemd-and-nginx). The article specifically focuses on Node.js, but the same concept is very easily applied to Ruby on Rails. However, it requires `systemctl`, which is not installed on many Linux distros by default. – Piccolo Jul 23 '14 at 04:54
  • When you `sudo` a command it will run as root and no longer have access to environment variables and things needed by `rvm` to load the proper `gemset` or `ruby` you have configured under your user `askar`. – DiegoSalazar Jul 23 '14 at 14:28
  • Trying to figure out how to start rc.local on boot... – Askar Jul 24 '14 at 03:33

3 Answers3

1

You need to run a real web server like Apache or Nginx and a ruby connector like Passenger. It's much easier to just do rail s. As you suspected it's like setting up a staging or production server.

There are plenty of good tutorials available on how to do this, especially for Linux.

Peter Wooster
  • 6,009
  • 2
  • 27
  • 39
  • The type of server is irrelevant in this case. What this user is asking is how to start the server, whether apache, nginx or webrick, whenevr they login to their computer. It's just a matter of adding the rails s command to a login script. – DiegoSalazar Jul 22 '14 at 01:37
  • I agree you can run the test server from your login profile or the server startup scripts. The OP did ask about something like a production server. Running `rails server` at startup limits you to one app as far as I know. – Peter Wooster Jul 22 '14 at 01:45
  • To run more apps just specify the port: `rails server -p 3001` and so on. – DiegoSalazar Jul 23 '14 at 14:30
0

You just need to add the bash commands:

cd path/to/rails/project; rails s

To a login script such as your ~/.bashrc file. The login script will start your rails server every time you login to your linux box. In OSX you'd add a login item to the LaunchAgents.

DiegoSalazar
  • 13,361
  • 2
  • 38
  • 55
0

You can also look at something like Prax https://github.com/ysbaddaden/prax which is a pure ruby alternative to Pow for OSX, that allows you to run it as a rack proxy and access your site at sitename.dev. I like this better than rails s since you can easily have multiple sites linked to it access them by name and not worry about ports etc..

Doon
  • 19,719
  • 3
  • 40
  • 44
  • I'm using Linux, not Mac OS X ;) – Askar Jul 22 '14 at 03:58
  • Yes prax works on linux. It doesn't work on OSX. Pow is the OSX version. sorry guess my wording was bit off. Prax is a pure ruby version. you can set it to run at startup, removes the :3000. works on linux, etc – Doon Jul 22 '14 at 12:52
  • But yours will work upon login, correct? I want to start an app without login. – Askar Jul 24 '14 at 04:59
  • you would need to start the prax service inside either an RC script or the like, but your current error with rc.local is that it is running as root, root doesn't have RVM installed and therefore it cannot find the gems, etc.. you can try installing RVM Systemwide, or you can look at making your startup command run as your user, but you will need to make sure that your rvm sourcing line isn't only included with interactive shells. – Doon Jul 24 '14 at 05:18