start rails server automatically after reboot

2019-04-29 22:38发布

问题:

I'd like my rails server to start automatically after every reboot, so I added the following to my .bashrc file

rvm use 1.9.2
cd /home/user/myapp
rails server

With that, the server never starts automatically after a reboot, and I have to start it manually.

Also, when I login to start the server, I see the following message

Using /usr/local/rvm/gems/ruby-1.9.2-p290
/usr/local/rvm/rubies/ruby-1.9.2-p290/bin/ruby: symbol lookup error: /usr/local/rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.4/lib/sqlite3/sqlite3_native.so: undefined symbol: sqlite3_initialize

As a consequence, I need to install sqlite3 after every reboot using "gem install sqlite3" after I make myself superuser, and only then I can start the rails server without issues.

$ cat /etc/*-release
CentOS release 5.8 (Final)

$ rails -v
Rails 3.1.1

$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]

Anyone can please help me overcome this issue? Thanks

回答1:

Install Apache and Passenger

They will take care of starting your App with the server in a safer and more systematic way and what is now more or less a standard.

I had the same issue with Rails 4, Ruby 2.1 on CentOS 6. If you're not familiar with bash scripts and the rc, profiles system - it's much faster and easier to setup passenger.

Also, there are other reasons why you would go for passenger, including security and performance ( www.phusionpassenger.com )

Here is a quick how-to of what it took me to introduce the gem.

  1. Install Apache (html daemon) and dependency packages (if you don't have them already):

    yum install httpd curl-devel httpd-devel

  2. Get Apache to start on boot:

    chkconfig httpd on

  3. Install Phusion Passenger and dependency packages:

    gem install passenger
    yum install curl-devel httpd-devel

  4. Compile the environment:

    passenger-install-apache2-module

  5. Edit the Apache config file in etc/httpd/conf/httpd.conf

    • Uncomment the line containing NameVirtualHost *:80 towards the end

    • Paste the output from point 4) anywhere at the end of the file, eg:

    LoadModule passenger_module /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.41/buildout/apache2/mod_passenger.so

    PassengerRoot /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.41

    PassengerDefaultRuby /usr/local/rvm/gems/ruby-2.1.1/wrappers/ruby

    <VirtualHost *:80>
     ServerName 1.2.3.4 # www.whatever.com
     DocumentRoot /var/www/rails/public # the path to your rails app
     <Directory /var/www/rails/public>
     AllowOverride all
     Options -MultiViews
     </Directory>
    </VirtualHost>
    

Took me 30 minutes in total, including several trial-errors with the httpd.conf to get everything right.

Note that installation requires at least 1 GB RAM on your machine.