RailsDeployment

来自公开记录
跳到导航 跳到搜索

Ruby On Rails Deployment

I have a very hard time on learning how to deploy a rails app on my VPS. So I write some notes on this page.

Install RVM

I use RVM to manage the Ruby packages. The Ruby package provided by Ubuntu is too old. I'd like to use the latest ruby version.

Install RVM using root account (execute the install command by root or use sudo) can do a system-wide install. In this case, RVM will be installed in /usr/local/rvm instead of $HOME/.rvm:

# curl -L get.rvm.io | bash -s stable
or 
$ curl -L get.rvm.io | sudo bash -s stable


Then add the non-root user into the rvm group, so that user can execute rvm commands without sudo:

sudo usermod -a -G rvm liufeng

We can use this command to check:

id liufeng

My output is:

uid=1000(liufeng) gid=1000(liufeng) groups=1000(liufeng),1001(rvm)


Install Ruby

  • Run rvm requirements and follow its instructions. Install the dependencies.
  • rvm list known will give you a list of available packages.
  • Install:
rvm install 1.9.3
Since it's the only Ruby package on the system, we don't need to setup the default ruby interpreter.

Install Rails

gem i rails

Install Dependencies for Rails

It is very different on my VPS and my Macbook. I installed some extra gems to make the WEBrick run.

  • gem i execjs
  • gem i therubyracer

Edit the Gemfile in the app, uncomment this line:

gem 'therubyracer', :platform => :ruby

Otherwise ruby will complain "Could not find a JavaScript runtime" when you execute rails server.

Install MySQL Driver

You need to install libmysql-ruby and libmysqlclient-dev.

Apache + Passenger

Let Rails run on Apache is my first try. I spent hours to struggle to understand how it working. Phew~ Here are the instructions:

First, install Apache:

apt-get install apache2


Install Passenger. I use gem:

gem i passenger


Now install the Apache module, this step will let you install some Apache dependencies, do it:

passenger-install-apache2-module


In last step, the command will give you three lines, tells Apache the location of passenger module. Add them into your Apache config file. For cleanness, I created a /etc/apache2/conf.d/passenger.conf file:

LoadModule passenger_module /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12/ext/apache2/mod_passenger.so                                           
PassengerRoot /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.12
PassengerRuby /usr/local/rvm/wrappers/ruby-1.9.3-p194/ruby


Then create a site config file for your rails app:

# /etc/apache2/sites-available/rails-blog

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /srv/rails/blog/public
        RailsEnv development

        <Directory /srv/rails/blog/public>
                Allow from all
                Options -MultiViews
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


/!\ Note: [[RailsEnv]] development is important. The default environment passenger uses is production. If your environment is not production, rails will say it cannot find /srv/rails/blog/public/xxx directory. It wasted me a lot of time on this thing.

/!\ If you are using SQLite database, you have to set the permission of db directory to 777, otherwise rails cannot write to the database.